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