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