]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/announcement/class_XmlAnnouncementTemplateEngine.php
Forgot this ...
[hub.git] / application / hub / main / template / announcement / class_XmlAnnouncementTemplateEngine.php
1 <?php
2 /**
3  * An Announcement 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 XmlAnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
26         /**
27          * Some XML nodes must be available for later data extraction
28          */
29         const ANNOUNCEMENT_DATA_SESSION_ID  = 'session-id';
30         const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
31         const ANNOUNCEMENT_DATA_NODE_MODE   = 'node-mode';
32         const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'external-ip';
33         const ANNOUNCEMENT_DATA_INTERNAL_IP = 'internal-ip';
34         const ANNOUNCEMENT_DATA_TCP_PORT    = 'tcp-port';
35         const ANNOUNCEMENT_DATA_UDP_PORT    = 'udp-port';
36
37         /**
38          * Main nodes in the XML tree
39          */
40         private $mainNodes = array(
41                 'announcement',
42         );
43
44         /**
45          * Sub nodes in the XML tree
46          */
47         private $subNodes = array();
48
49         /**
50          * Current main node
51          */
52         private $curr = array();
53
54         /**
55          * Content from dependency
56          */
57         private $dependencyContent = array();
58
59         /**
60          * Protected constructor
61          *
62          * @return      void
63          */
64         protected function __construct () {
65                 // Call parent constructor
66                 parent::__construct(__CLASS__);
67
68                 // Init array
69                 $this->subNodes = array(
70                         'announcement-data',
71                         'listener',
72                         self::ANNOUNCEMENT_DATA_NODE_STATUS,
73                         self::ANNOUNCEMENT_DATA_NODE_MODE,
74                         self::ANNOUNCEMENT_DATA_TCP_PORT,
75                         self::ANNOUNCEMENT_DATA_UDP_PORT,
76                         self::ANNOUNCEMENT_DATA_SESSION_ID,
77                         self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
78                         self::ANNOUNCEMENT_DATA_INTERNAL_IP,
79                         'object-type-list',
80                 );
81         }
82
83         /**
84          * Creates an instance of the class TemplateEngine and prepares it for usage
85          *
86          * @return      $templateInstance               An instance of TemplateEngine
87          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
88          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
89          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
90          *                                                                                      directory or not found
91          * @throws      BasePathReadProtectedException  If $templateBasePath is
92          *                                                                                      read-protected
93          */
94         public static final function createXmlAnnouncementTemplateEngine () {
95                 // Get a new instance
96                 $templateInstance = new XmlAnnouncementTemplateEngine();
97
98                 // Get the application instance from registry
99                 $applicationInstance = Registry::getRegistry()->getInstance('app');
100
101                 // Determine base path
102                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
103
104                 // Is the base path valid?
105                 if (empty($templateBasePath)) {
106                         // Base path is empty
107                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
108                 } elseif (!is_string($templateBasePath)) {
109                         // Is not a string
110                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
111                 } elseif (!is_dir($templateBasePath)) {
112                         // Is not a path
113                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
114                 } elseif (!is_readable($templateBasePath)) {
115                         // Is not readable
116                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
117                 }
118
119                 // Set the base path
120                 $templateInstance->setTemplateBasePath($templateBasePath);
121
122                 // Set template extensions
123                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
124                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
125
126                 // Absolute output path for compiled templates
127                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
128
129                 // Init a variable stacker
130                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
131
132                 // Set it
133                 $templateInstance->setStackerInstance($stackerInstance);
134
135                 // Return the prepared instance
136                 return $templateInstance;
137         }
138
139         /**
140          * Load a specified announcement template into the engine
141          *
142          * @param       $template       The announcement template we shall load which is
143          *                                              located in 'announcement' by default
144          * @return      void
145          */
146         public function loadAnnouncementTemplate ($template = 'self_announcement') {
147                 // Set template type
148                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
149
150                 // Load the special template
151                 $this->loadTemplate($template);
152         }
153
154         /**
155          * Getter for current main node
156          *
157          * @return      $currMainNode   Current main node
158          */
159         public final function getCurrMainNode () {
160                 return $this->curr['main_node'];
161         }
162
163         /**
164          * Setter for current main node
165          *
166          * @param       $element                Element name to set as current main node
167          * @return      $currMainNode   Current main node
168          */
169         private final function setCurrMainNode ($element) {
170                 $this->curr['main_node'] = (string) $element;
171         }
172
173         /**
174          * Getter for main node array
175          *
176          * @return      $mainNodes      Array with valid main node names
177          */
178         public final function getMainNodes () {
179                 return $this->mainNodes;
180         }
181
182         /**
183          * Getter for sub node array
184          *
185          * @return      $subNodes       Array with valid sub node names
186          */
187         public final function getSubNodes () {
188                 return $this->subNodes;
189         }
190
191         /**
192          * Handles the start element of an XML resource
193          *
194          * @param       $resource               XML parser resource (currently ignored)
195          * @param       $element                The element we shall handle
196          * @param       $attributes             All attributes
197          * @return      void
198          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
199          */
200         public function startElement ($resource, $element, array $attributes) {
201                 // Initial method name which will never be called...
202                 $methodName = 'initAnnouncement';
203
204                 // Make the element name lower-case
205                 $element = strtolower($element);
206
207                 // Is the element a main node?
208                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
209                 if (in_array($element, $this->getMainNodes())) {
210                         // Okay, main node found!
211                         $methodName = 'start' . $this->convertToClassName($element);
212
213                         // Set it
214                         $this->setCurrMainNode($element);
215                 } elseif (in_array($element, $this->getSubNodes())) {
216                         // Sub node found
217                         $methodName = 'start' . $this->convertToClassName($element);
218                 } else {
219                         // Invalid node name found
220                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
221                 }
222
223                 // Call method
224                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
225                 call_user_func_array(array($this, $methodName), $attributes);
226         }
227
228         /**
229          * Ends the main or sub node by sending out the gathered data
230          *
231          * @param       $resource       An XML resource pointer (currently ignored)
232          * @param       $nodeName       Name of the node we want to finish
233          * @return      void
234          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
235          */
236         public function endElement ($resource, $nodeName) {
237                 // Make all lower-case
238                 $nodeName = strtolower($nodeName);
239
240                 // Does this match with current main node?
241                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
242                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
243                         // Did not match!
244                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
245                 } // END - if
246
247                 // Construct method name
248                 $methodName = 'finish' . $this->convertToClassName($nodeName);
249
250                 // Call the corresponding method
251                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
252                 call_user_func_array(array($this, $methodName), array());
253         }
254
255         /**
256          * Currently not used
257          *
258          * @param       $resource               XML parser resource (currently ignored)
259          * @param       $characters             Characters to handle
260          * @return      void
261          */
262         public function characterHandler ($resource, $characters) {
263                 // Trim all spaces away
264                 $characters = trim($characters);
265
266                 // Is this string empty?
267                 if (empty($characters)) {
268                         // Then skip it silently
269                         return false;
270                 } // END - if
271
272                 /*
273                  * Assign the found characters to variable and use the last entry from
274                  * stack as the name.
275                  */
276                 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
277         }
278
279         /**
280          * Handles the template dependency for given node
281          *
282          * @param       $node                                   The node we should load a dependency template
283          * @param       $templateDependency             A template to load to satisfy dependencies
284          * @return      void
285          */
286         private function handleTemplateDependency ($node, $templateDependency) {
287                 // Is the template dependency set?
288                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
289                         // Get a temporay template instance
290                         $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
291
292                         // Then load it
293                         $templateInstance->loadAnnouncementTemplate($templateDependency);
294
295                         // Parse the XML content
296                         $templateInstance->renderXmlContent();
297
298                         // Save the parsed raw content in our dependency array
299                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
300                 } // END - if
301         }
302
303         /**
304          * Read announcement variables by calling readVariable() with 'general' as
305          * variable stack.
306          *
307          * @param       $key    Key to read from
308          * @return      $value  Value from variable
309          */
310         public function readAnnouncementData ($key) {
311                 // Read the variable
312                 $value = parent::readVariable($key, 'general');
313
314                 // Return value
315                 return $value;
316         }
317
318         /**
319          * Getter for cache file (FQFN)
320          *
321          * @return      $fqfn   Full-qualified file name of the menu cache
322          */
323         public function getMenuCacheFqfn () {
324                 $this->partialStub('Please implement this method.');
325         }
326
327         /**
328          * Starts the announcement
329          *
330          * @return      void
331          */
332         private function startAnnouncement () {
333                 // Push the node name on the stacker
334                 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
335         }
336
337         /**
338          * Starts the announcement data
339          *
340          * @return      void
341          */
342         private function startAnnouncementData () {
343                 // Push the node name on the stacker
344                 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
345         }
346
347         /**
348          * Starts the node status
349          *
350          * @return      void
351          */
352         private function startNodeStatus () {
353                 // Push the node name on the stacker
354                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
355         }
356
357         /**
358          * Starts the node-mode
359          *
360          * @return      void
361          */
362         private function startNodeMode () {
363                 // Push the node name on the stacker
364                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_MODE);
365         }
366
367         /**
368          * Starts the listener
369          *
370          * @return      void
371          */
372         private function startListener () {
373                 // Push the node name on the stacker
374                 $this->getStackerInstance()->pushNamed('announcement', 'listener');
375         }
376
377         /**
378          * Starts the TCP port
379          *
380          * @return      void
381          */
382         private function startTcpPort () {
383                 // Push the node name on the stacker
384                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
385         }
386
387         /**
388          * Starts the UDP port
389          *
390          * @return      void
391          */
392         private function startUdpPort () {
393                 // Push the node name on the stacker
394                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
395         }
396
397         /**
398          * Starts the session id
399          *
400          * @return      void
401          */
402         private function startSessionId () {
403                 // Push the node name on the stacker
404                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
405         }
406
407         /**
408          * Starts the public ip
409          *
410          * @return      void
411          */
412         private function startExternalIp () {
413                 // Push the node name on the stacker
414                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
415         }
416
417         /**
418          * Starts the private ip
419          *
420          * @return      void
421          */
422         private function startInternalIp () {
423                 // Push the node name on the stacker
424                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
425         }
426
427         /**
428          * Starts the object type list
429          *
430          * @return      void
431          */
432         private function startObjectTypeList () {
433                 // Push the node name on the stacker
434                 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
435         }
436
437         /**
438          * Starts the object type
439          *
440          * @return      void
441          */
442         private function startObjectType () {
443                 // Push the node name on the stacker
444                 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
445         }
446
447         /**
448          * Finishes the object type
449          *
450          * @return      void
451          */
452         private function finishObjectType () {
453                 // Pop the last entry
454                 $this->getStackerInstance()->popNamed('announcement');
455         }
456
457         /**
458          * Finishes the object type list
459          *
460          * @return      void
461          */
462         private function finishObjectTypeList () {
463                 // Pop the last entry
464                 $this->getStackerInstance()->popNamed('announcement');
465         }
466
467         /**
468          * Finishes the session id
469          *
470          * @return      void
471          */
472         private function finishSessionId () {
473                 // Pop the last entry
474                 $this->getStackerInstance()->popNamed('announcement');
475         }
476
477         /**
478          * Finishes the private ip
479          *
480          * @return      void
481          */
482         private function finishInternalIp () {
483                 // Pop the last entry
484                 $this->getStackerInstance()->popNamed('announcement');
485         }
486
487         /**
488          * Finishes the public ip
489          *
490          * @return      void
491          */
492         private function finishExternalIp () {
493                 // Pop the last entry
494                 $this->getStackerInstance()->popNamed('announcement');
495         }
496
497         /**
498          * Finishes the UDP port
499          *
500          * @return      void
501          */
502         private function finishUdpPort () {
503                 // Pop the last entry
504                 $this->getStackerInstance()->popNamed('announcement');
505         }
506
507         /**
508          * Finishes the TCP port
509          *
510          * @return      void
511          */
512         private function finishTcpPort () {
513                 // Pop the last entry
514                 $this->getStackerInstance()->popNamed('announcement');
515         }
516
517         /**
518          * Finishes the listener
519          *
520          * @return      void
521          */
522         private function finishListener () {
523                 // Pop the last entry
524                 $this->getStackerInstance()->popNamed('announcement');
525         }
526
527         /**
528          * Finishes the node mode
529          *
530          * @return      void
531          */
532         private function finishNodeMode () {
533                 // Pop the last entry
534                 $this->getStackerInstance()->popNamed('announcement');
535         }
536
537         /**
538          * Finishes the node status
539          *
540          * @return      void
541          */
542         private function finishNodeStatus () {
543                 // Pop the last entry
544                 $this->getStackerInstance()->popNamed('announcement');
545         }
546
547         /**
548          * Finishes the announcement data
549          *
550          * @return      void
551          */
552         private function finishAnnouncementData () {
553                 // Pop the last entry
554                 $this->getStackerInstance()->popNamed('announcement');
555         }
556
557         /**
558          * Finishes the announcement
559          *
560          * @return      void
561          */
562         private function finishAnnouncement () {
563                 // Pop the last entry
564                 $this->getStackerInstance()->popNamed('announcement');
565         }
566 }
567
568 // [EOF]
569 ?>