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