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