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