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