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