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