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