]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/objects/class_XmlObjectRegistryTemplateEngine.php
438565cd481866c733aeac83dda70b84df11fe34
[hub.git] / application / hub / main / template / objects / class_XmlObjectRegistryTemplateEngine.php
1 <?php
2 /**
3  * An ObjectRegistry template engine class for XML templates
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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()
11  *
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.
16  *
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.
21  *
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/>.
24  */
25 class XmlObjectRegistryTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Instance for the object registry
28          */
29         private $objectRegistryInstance = null;
30
31         /**
32          * Main nodes in the XML tree
33          */
34         private $mainNodes = array(
35                 'object-registry'
36         );
37
38         /**
39          * Sub nodes in the XML tree
40          */
41         private $subNodes = array(
42                 'object-list',
43                 'object-list-entry',
44                 'object-name',
45                 'object-recipient-limitation',
46                 'object-max-spread',
47                 'object-protocol',
48                 'object-recipient-type'
49         );
50
51         /**
52          * Current main node
53          */
54         private $curr = array();
55
56         /**
57          * Content from dependency
58          */
59         private $dependencyContent = array();
60
61         /**
62          * Protected constructor
63          *
64          * @return      void
65          */
66         protected function __construct () {
67                 // Call parent constructor
68                 parent::__construct(__CLASS__);
69
70                 // Init object type registry instance
71                 $this->objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('node_object_type_registry_class');
72         }
73
74         /**
75          * Creates an instance of the class TemplateEngine and prepares it for usage
76          *
77          * @param       $applicationInstance    A manageable application
78          * @return      $templateInstance               An instance of TemplateEngine
79          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
80          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
81          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
82          *                                                                                      directory or not found
83          * @throws      BasePathReadProtectedException  If $templateBasePath is
84          *                                                                                      read-protected
85          */
86         public static final function createXmlObjectRegistryTemplateEngine (ManageableApplication $applicationInstance) {
87                 // Get a new instance
88                 $templateInstance = new XmlObjectRegistryTemplateEngine();
89
90                 // Determine base path
91                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
92
93                 // Is the base path valid?
94                 if (empty($templateBasePath)) {
95                         // Base path is empty
96                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
97                 } elseif (!is_string($templateBasePath)) {
98                         // Is not a string
99                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
100                 } elseif (!is_dir($templateBasePath)) {
101                         // Is not a path
102                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
103                 } elseif (!is_readable($templateBasePath)) {
104                         // Is not readable
105                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
106                 }
107
108                 // Set the base path
109                 $templateInstance->setTemplateBasePath($templateBasePath);
110
111                 // Set template extensions
112                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
113                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_object_registry_template_extension'));
114
115                 // Absolute output path for compiled templates
116                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
117
118                 // Init a variable stacker
119                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_object_registry_stacker_class');
120
121                 // Set it
122                 $templateInstance->setStackerInstance($stackerInstance);
123
124                 // Return the prepared instance
125                 return $templateInstance;
126         }
127
128         /**
129          * Load a specified object_registry template into the engine
130          *
131          * @param       $template       The object_registry template we shall load which is
132          *                                              located in 'object_registry' by default
133          * @return      void
134          */
135         public function loadObjectRegistryTemplate ($template) {
136                 // Set template type
137                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_object_registry_template_type'));
138
139                 // Load the special template
140                 $this->loadTemplate($template);
141         }
142
143         /**
144          * Getter for current main node
145          *
146          * @return      $currMainNode   Current main node
147          */
148         public final function getCurrMainNode () {
149                 return $this->curr['main_node'];
150         }
151
152         /**
153          * Setter for current main node
154          *
155          * @param       $element                Element name to set as current main node
156          * @return      $currMainNode   Current main node
157          */
158         private final function setCurrMainNode ($element) {
159                 $this->curr['main_node'] = (string) $element;
160         }
161
162         /**
163          * Getter for main node array
164          *
165          * @return      $mainNodes      Array with valid main node names
166          */
167         public final function getMainNodes () {
168                 return $this->mainNodes;
169         }
170
171         /**
172          * Getter for sub node array
173          *
174          * @return      $subNodes       Array with valid sub node names
175          */
176         public final function getSubNodes () {
177                 return $this->subNodes;
178         }
179
180         /**
181          * Handles the start element of an XML resource
182          *
183          * @param       $resource               XML parser resource (currently ignored)
184          * @param       $element                The element we shall handle
185          * @param       $attributes             All attributes
186          * @return      void
187          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
188          */
189         public function startElement ($resource, $element, array $attributes) {
190                 // Initial method name which will never be called...
191                 $methodName = 'initObjectRegistry';
192
193                 // Make the element name lower-case
194                 $element = strtolower($element);
195
196                 // Is the element a main node?
197                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
198                 if (in_array($element, $this->getMainNodes())) {
199                         // Okay, main node found!
200                         $methodName = 'start' . $this->convertToClassName($element);
201
202                         // Set it
203                         $this->setCurrMainNode($element);
204                 } elseif (in_array($element, $this->getSubNodes())) {
205                         // Sub node found
206                         $methodName = 'start' . $this->convertToClassName($element);
207                 } else {
208                         // Invalid node name found
209                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
210                 }
211
212                 // Call method
213                 call_user_func_array(array($this, $methodName), $attributes);
214         }
215
216         /**
217          * Ends the main or sub node by sending out the gathered data
218          *
219          * @param       $resource       An XML resource pointer (currently ignored)
220          * @param       $nodeName       Name of the node we want to finish
221          * @return      void
222          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
223          */
224         public function endElement ($resource, $nodeName) {
225                 // Make all lower-case
226                 $nodeName = strtolower($nodeName);
227
228                 // Does this match with current main node?
229                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
230                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
231                         // Did not match!
232                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
233                 } // END - if
234
235                 // Construct method name
236                 $methodName = 'finish' . $this->convertToClassName($nodeName);
237
238                 // Call the corresponding method
239                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
240                 call_user_func_array(array($this, $methodName), array());
241         }
242
243         /**
244          * Currently not used
245          *
246          * @param       $resource               XML parser resource (currently ignored)
247          * @param       $characters             Characters to handle
248          * @return      void
249          * @todo        Find something useful with this!
250          */
251         public function characterHandler ($resource, $characters) {
252                 // Trim all spaces away
253                 $characters = trim($characters);
254
255                 // Is this string empty?
256                 if (empty($characters)) {
257                         // Then skip it silently
258                         return false;
259                 } // END - if
260
261                 // Get current XML node name as an array index
262                 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
263
264                 // Is the node name 'object-name'?
265                 if ($nodeName == 'object-name') {
266                         // Output debug message
267                         $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
268                 } // END - if
269
270                 // Add it to the registry
271                 $this->objectRegistryInstance->addEntry($nodeName, $characters);
272         }
273
274         /**
275          * Handles the template dependency for given node
276          *
277          * @param       $node                                   The node we should load a dependency template
278          * @param       $templateDependency             A template to load to satisfy dependencies
279          * @return      void
280          */
281         private function handleTemplateDependency ($node, $templateDependency) {
282                 // Is the template dependency set?
283                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
284                         // Get a temporay template instance
285                         $templateInstance = ObjectFactory::createObjectByConfiguredName('node_object_registry_template_class', array($this->getApplicationInstance()));
286
287                         // Then load it
288                         $templateInstance->loadObjectRegistryTemplate($templateDependency);
289
290                         // Parse the XML content
291                         $templateInstance->renderXmlContent();
292
293                         // Save the parsed raw content in our dependency array
294                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
295                 } // END - if
296         }
297
298         /**
299          * Getter for cache file (FQFN)
300          *
301          * @return      $fqfn   Full-qualified file name of the menu cache
302          */
303         public function getObjectRegistryCacheFqfn () {
304                 $this->partialStub('Please implement this method.');
305         }
306
307         /**
308          * Starts the object-registry
309          *
310          * @return      void
311          */
312         private function startObjectRegistry () {
313                 // Push the node name on the stacker
314                 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
315         }
316
317         /**
318          * Starts the object-list
319          *
320          * @return      void
321          */
322         private function startObjectList () {
323                 // Push the node name on the stacker
324                 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
325         }
326
327         /**
328          * Starts the object-list-entry
329          *
330          * @return      void
331          */
332         private function startObjectListEntry () {
333                 // Push the node name on the stacker
334                 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
335         }
336
337         /**
338          * Starts the object-name
339          *
340          * @return      void
341          */
342         private function startObjectName () {
343                 // Push the node name on the stacker
344                 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
345         }
346
347         /**
348          * Starts the object-recipient-limitation
349          *
350          * @return      void
351          */
352         private function startObjectRecipientLimitation () {
353                 // Push the node name on the stacker
354                 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
355         }
356
357         /**
358          * Starts the object-max-spread
359          *
360          * @return      void
361          */
362         private function startObjectMaxSpread () {
363                 // Push the node name on the stacker
364                 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
365         }
366
367         /**
368          * Starts the object-protocol
369          *
370          * @return      void
371          */
372         private function startObjectProtocol () {
373                 // Push the node name on the stacker
374                 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
375         }
376
377         /**
378          * Starts the object-recipient-type
379          *
380          * @return      void
381          */
382         private function startObjectRecipientType () {
383                 // Push the node name on the stacker
384                 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-type');
385         }
386
387         /**
388          * Finishes the object-recipient-type
389          *
390          * @return      void
391          */
392         private function finishObjectRecipientType () {
393                 // Pop the last entry
394                 $this->getStackerInstance()->popNamed('object_registry');
395         }
396
397         /**
398          * Finishes the object-protocol
399          *
400          * @return      void
401          */
402         private function finishObjectProtocol () {
403                 // Pop the last entry
404                 $this->getStackerInstance()->popNamed('object_registry');
405         }
406
407         /**
408          * Finishes the object-max-spread
409          *
410          * @return      void
411          */
412         private function finishObjectMaxSpread () {
413                 // Pop the last entry
414                 $this->getStackerInstance()->popNamed('object_registry');
415         }
416
417         /**
418          * Finishes the object-recipient-limitation
419          *
420          * @return      void
421          */
422         private function finishObjectRecipientLimitation () {
423                 // Pop the last entry
424                 $this->getStackerInstance()->popNamed('object_registry');
425         }
426
427         /**
428          * Finishes the object-name
429          *
430          * @return      void
431          */
432         private function finishObjectName () {
433                 // Pop the last entry
434                 $this->getStackerInstance()->popNamed('object_registry');
435         }
436
437         /**
438          * Finishes the object-list-entry
439          *
440          * @return      void
441          */
442         private function finishObjectListEntry () {
443                 // Pop the last entry
444                 $this->getStackerInstance()->popNamed('object_registry');
445         }
446
447         /**
448          * Finishes the object-list
449          *
450          * @return      void
451          */
452         private function finishObjectList () {
453                 // Pop the last entry
454                 $this->getStackerInstance()->popNamed('object_registry');
455         }
456
457         /**
458          * Finishes the object-registry
459          *
460          * @return      void
461          */
462         private function finishObjectRegistry () {
463                 // Pop the last entry
464                 $this->getStackerInstance()->popNamed('object_registry');
465         }
466 }
467
468 // [EOF]
469 ?>