3 * An ObjectRegistry template engine class for XML templates
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
10 * @todo This template engine does not make use of setTemplateType()
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class XmlObjectRegistryTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
27 * Instance for the object registry
29 private $objectRegistryInstance = null;
32 * Main nodes in the XML tree
34 private $mainNodes = array(
39 * Sub nodes in the XML tree
41 private $subNodes = array(
45 'object-recipient-limitation',
48 'object-recipient-type'
54 private $curr = array();
57 * Content from dependency
59 private $dependencyContent = array();
62 * Protected constructor
66 protected function __construct () {
67 // Call parent constructor
68 parent::__construct(__CLASS__);
70 // Init object type registry instance
71 $this->objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('node_object_type_registry_class');
75 * Creates an instance of the class TemplateEngine and prepares it for usage
77 * @return $templateInstance An instance of TemplateEngine
78 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
79 * @throws InvalidBasePathStringException If $templateBasePath is no string
80 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
81 * directory or not found
82 * @throws BasePathReadProtectedException If $templateBasePath is
85 public static final function createXmlObjectRegistryTemplateEngine () {
87 $templateInstance = new XmlObjectRegistryTemplateEngine();
89 // Get the application instance from registry
90 $applicationInstance = Registry::getRegistry()->getInstance('app');
92 // Determine base path
93 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
95 // Is the base path valid?
96 if (empty($templateBasePath)) {
98 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
99 } elseif (!is_string($templateBasePath)) {
101 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
102 } elseif (!is_dir($templateBasePath)) {
104 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
105 } elseif (!is_readable($templateBasePath)) {
107 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
111 $templateInstance->setTemplateBasePath($templateBasePath);
113 // Set template extensions
114 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
115 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_object_registry_template_extension'));
117 // Absolute output path for compiled templates
118 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
120 // Init a variable stacker
121 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_object_registry_stacker_class');
124 $templateInstance->setStackerInstance($stackerInstance);
126 // Return the prepared instance
127 return $templateInstance;
131 * Load a specified object_registry template into the engine
133 * @param $template The object_registry template we shall load which is
134 * located in 'object_registry' by default
137 public function loadObjectRegistryTemplate ($template) {
139 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_object_registry_template_type'));
141 // Load the special template
142 $this->loadTemplate($template);
146 * Getter for current main node
148 * @return $currMainNode Current main node
150 public final function getCurrMainNode () {
151 return $this->curr['main_node'];
155 * Setter for current main node
157 * @param $element Element name to set as current main node
158 * @return $currMainNode Current main node
160 private final function setCurrMainNode ($element) {
161 $this->curr['main_node'] = (string) $element;
165 * Getter for main node array
167 * @return $mainNodes Array with valid main node names
169 public final function getMainNodes () {
170 return $this->mainNodes;
174 * Getter for sub node array
176 * @return $subNodes Array with valid sub node names
178 public final function getSubNodes () {
179 return $this->subNodes;
183 * Handles the start element of an XML resource
185 * @param $resource XML parser resource (currently ignored)
186 * @param $element The element we shall handle
187 * @param $attributes All attributes
189 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
191 public function startElement ($resource, $element, array $attributes) {
192 // Initial method name which will never be called...
193 $methodName = 'initObjectRegistry';
195 // Make the element name lower-case
196 $element = strtolower($element);
198 // Is the element a main node?
199 //* DEBUG: */ echo "START: >".$element."<<br />\n";
200 if (in_array($element, $this->getMainNodes())) {
201 // Okay, main node found!
202 $methodName = 'start' . $this->convertToClassName($element);
205 $this->setCurrMainNode($element);
206 } elseif (in_array($element, $this->getSubNodes())) {
208 $methodName = 'start' . $this->convertToClassName($element);
210 // Invalid node name found
211 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
215 call_user_func_array(array($this, $methodName), $attributes);
219 * Ends the main or sub node by sending out the gathered data
221 * @param $resource An XML resource pointer (currently ignored)
222 * @param $nodeName Name of the node we want to finish
224 * @throws XmlNodeMismatchException If current main node mismatches the closing one
226 public function endElement ($resource, $nodeName) {
227 // Make all lower-case
228 $nodeName = strtolower($nodeName);
230 // Does this match with current main node?
231 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
232 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
234 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
237 // Construct method name
238 $methodName = 'finish' . $this->convertToClassName($nodeName);
240 // Call the corresponding method
241 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
242 call_user_func_array(array($this, $methodName), array());
248 * @param $resource XML parser resource (currently ignored)
249 * @param $characters Characters to handle
251 * @todo Find something useful with this!
253 public function characterHandler ($resource, $characters) {
254 // Trim all spaces away
255 $characters = trim($characters);
257 // Is this string empty?
258 if (empty($characters)) {
259 // Then skip it silently
263 // Get current XML node name as an array index
264 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
266 // Is the node name 'object-name'?
267 if ($nodeName == 'object-name') {
268 // Output debug message
269 $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
272 // Add it to the registry
273 $this->objectRegistryInstance->addEntry($nodeName, $characters);
277 * Handles the template dependency for given node
279 * @param $node The node we should load a dependency template
280 * @param $templateDependency A template to load to satisfy dependencies
283 private function handleTemplateDependency ($node, $templateDependency) {
284 // Is the template dependency set?
285 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
286 // Get a temporay template instance
287 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class');
290 $templateInstance->loadObjectRegistryTemplate($templateDependency);
292 // Parse the XML content
293 $templateInstance->renderXmlContent();
295 // Save the parsed raw content in our dependency array
296 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
301 * Getter for cache file (FQFN)
303 * @return $fqfn Full-qualified file name of the menu cache
305 public function getObjectRegistryCacheFqfn () {
306 $this->partialStub('Please implement this method.');
310 * Starts the object-registry
314 private function startObjectRegistry () {
315 // Push the node name on the stacker
316 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
320 * Starts the object-list
324 private function startObjectList () {
325 // Push the node name on the stacker
326 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
330 * Starts the object-list-entry
334 private function startObjectListEntry () {
335 // Push the node name on the stacker
336 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
340 * Starts the object-name
344 private function startObjectName () {
345 // Push the node name on the stacker
346 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
350 * Starts the object-recipient-limitation
354 private function startObjectRecipientLimitation () {
355 // Push the node name on the stacker
356 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
360 * Starts the object-max-spread
364 private function startObjectMaxSpread () {
365 // Push the node name on the stacker
366 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
370 * Starts the object-protocol
374 private function startObjectProtocol () {
375 // Push the node name on the stacker
376 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
380 * Starts the object-recipient-type
384 private function startObjectRecipientType () {
385 // Push the node name on the stacker
386 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-type');
390 * Finishes the object-recipient-type
394 private function finishObjectRecipientType () {
395 // Pop the last entry
396 $this->getStackerInstance()->popNamed('object_registry');
400 * Finishes the object-protocol
404 private function finishObjectProtocol () {
405 // Pop the last entry
406 $this->getStackerInstance()->popNamed('object_registry');
410 * Finishes the object-max-spread
414 private function finishObjectMaxSpread () {
415 // Pop the last entry
416 $this->getStackerInstance()->popNamed('object_registry');
420 * Finishes the object-recipient-limitation
424 private function finishObjectRecipientLimitation () {
425 // Pop the last entry
426 $this->getStackerInstance()->popNamed('object_registry');
430 * Finishes the object-name
434 private function finishObjectName () {
435 // Pop the last entry
436 $this->getStackerInstance()->popNamed('object_registry');
440 * Finishes the object-list-entry
444 private function finishObjectListEntry () {
445 // Pop the last entry
446 $this->getStackerInstance()->popNamed('object_registry');
450 * Finishes the object-list
454 private function finishObjectList () {
455 // Pop the last entry
456 $this->getStackerInstance()->popNamed('object_registry');
460 * Finishes the object-registry
464 private function finishObjectRegistry () {
465 // Pop the last entry
466 $this->getStackerInstance()->popNamed('object_registry');