]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/announcement/class_XmlAnnouncementTemplateEngine.php
Added announcement answer XML, template engine and acceptance of announcements
[hub.git] / application / hub / main / template / announcement / class_XmlAnnouncementTemplateEngine.php
1 <?php
2 /**
3  * An Announcement 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 XmlAnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
26         /**
27          * Some XML nodes must be available for later data extraction
28          */
29         const ANNOUNCEMENT_DATA_SESSION_ID  = 'session-id';
30         const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
31         const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'external-ip';
32         const ANNOUNCEMENT_DATA_TCP_PORT    = 'tcp-port';
33         const ANNOUNCEMENT_DATA_UDP_PORT    = 'udp-port';
34
35         /**
36          * Main nodes in the XML tree
37          */
38         private $mainNodes = array(
39                 'announcement',
40         );
41
42         /**
43          * Sub nodes in the XML tree
44          */
45         private $subNodes = array();
46
47         /**
48          * Current main node
49          */
50         private $curr = array();
51
52         /**
53          * Content from dependency
54          */
55         private $dependencyContent = array();
56
57         /**
58          * Protected constructor
59          *
60          * @return      void
61          */
62         protected function __construct () {
63                 // Call parent constructor
64                 parent::__construct(__CLASS__);
65
66                 // Init array
67                 $this->subNodes = array(
68                         'announcement-data',
69                         'listener',
70                         self::ANNOUNCEMENT_DATA_NODE_STATUS,
71                         self::ANNOUNCEMENT_DATA_TCP_PORT,
72                         self::ANNOUNCEMENT_DATA_UDP_PORT,
73                         self::ANNOUNCEMENT_DATA_SESSION_ID,
74                         self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
75                         'object-type-list',
76                 );
77         }
78
79         /**
80          * Creates an instance of the class TemplateEngine and prepares it for usage
81          *
82          * @return      $templateInstance               An instance of TemplateEngine
83          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
84          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
85          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
86          *                                                                                      directory or not found
87          * @throws      BasePathReadProtectedException  If $templateBasePath is
88          *                                                                                      read-protected
89          */
90         public static final function createXmlAnnouncementTemplateEngine () {
91                 // Get a new instance
92                 $templateInstance = new XmlAnnouncementTemplateEngine();
93
94                 // Get the application instance from registry
95                 $applicationInstance = Registry::getRegistry()->getInstance('app');
96
97                 // Determine base path
98                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
99
100                 // Is the base path valid?
101                 if (empty($templateBasePath)) {
102                         // Base path is empty
103                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
104                 } elseif (!is_string($templateBasePath)) {
105                         // Is not a string
106                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
107                 } elseif (!is_dir($templateBasePath)) {
108                         // Is not a path
109                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
110                 } elseif (!is_readable($templateBasePath)) {
111                         // Is not readable
112                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
113                 }
114
115                 // Set the base path
116                 $templateInstance->setTemplateBasePath($templateBasePath);
117
118                 // Set template extensions
119                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
120                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
121
122                 // Absolute output path for compiled templates
123                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
124
125                 // Init a variable stacker
126                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
127
128                 // Set it
129                 $templateInstance->setStackerInstance($stackerInstance);
130
131                 // Return the prepared instance
132                 return $templateInstance;
133         }
134
135         /**
136          * Load a specified announcement template into the engine
137          *
138          * @param       $template       The announcement template we shall load which is
139          *                                              located in 'announcement' by default
140          * @return      void
141          */
142         public function loadAnnouncementTemplate ($template = 'self_announcement') {
143                 // Set template type
144                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
145
146                 // Load the special template
147                 $this->loadTemplate($template);
148         }
149
150         /**
151          * Getter for current main node
152          *
153          * @return      $currMainNode   Current main node
154          */
155         public final function getCurrMainNode () {
156                 return $this->curr['main_node'];
157         }
158
159         /**
160          * Setter for current main node
161          *
162          * @param       $element                Element name to set as current main node
163          * @return      $currMainNode   Current main node
164          */
165         private final function setCurrMainNode ($element) {
166                 $this->curr['main_node'] = (string) $element;
167         }
168
169         /**
170          * Getter for main node array
171          *
172          * @return      $mainNodes      Array with valid main node names
173          */
174         public final function getMainNodes () {
175                 return $this->mainNodes;
176         }
177
178         /**
179          * Getter for sub node array
180          *
181          * @return      $subNodes       Array with valid sub node names
182          */
183         public final function getSubNodes () {
184                 return $this->subNodes;
185         }
186
187         /**
188          * Handles the start element of an XML resource
189          *
190          * @param       $resource               XML parser resource (currently ignored)
191          * @param       $element                The element we shall handle
192          * @param       $attributes             All attributes
193          * @return      void
194          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
195          */
196         public function startElement ($resource, $element, array $attributes) {
197                 // Initial method name which will never be called...
198                 $methodName = 'initAnnouncement';
199
200                 // Make the element name lower-case
201                 $element = strtolower($element);
202
203                 // Is the element a main node?
204                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
205                 if (in_array($element, $this->getMainNodes())) {
206                         // Okay, main node found!
207                         $methodName = 'start' . $this->convertToClassName($element);
208
209                         // Set it
210                         $this->setCurrMainNode($element);
211                 } elseif (in_array($element, $this->getSubNodes())) {
212                         // Sub node found
213                         $methodName = 'start' . $this->convertToClassName($element);
214                 } else {
215                         // Invalid node name found
216                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
217                 }
218
219                 // Call method
220                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
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          */
258         public function characterHandler ($resource, $characters) {
259                 // Trim all spaces away
260                 $characters = trim($characters);
261
262                 // Is this string empty?
263                 if (empty($characters)) {
264                         // Then skip it silently
265                         return false;
266                 } // END - if
267
268                 // Assign the found characters to variable and use the last entry from
269                 // stack as the name
270                 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
271         }
272
273         /**
274          * Handles the template dependency for given node
275          *
276          * @param       $node                                   The node we should load a dependency template
277          * @param       $templateDependency             A template to load to satisfy dependencies
278          * @return      void
279          */
280         private function handleTemplateDependency ($node, $templateDependency) {
281                 // Is the template dependency set?
282                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
283                         // Get a temporay template instance
284                         $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
285
286                         // Then load it
287                         $templateInstance->loadAnnouncementTemplate($templateDependency);
288
289                         // Parse the XML content
290                         $templateInstance->renderXmlContent();
291
292                         // Save the parsed raw content in our dependency array
293                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
294                 } // END - if
295         }
296
297         /**
298          * Read announcement variables by calling readVariable() with 'general' as
299          * variable stack.
300          *
301          * @param       $key    Key to read from
302          * @return      $value  Value from variable
303          */
304         public function readAnnouncementData ($key) {
305                 // Read the variable
306                 $value = parent::readVariable($key, 'general');
307
308                 // Return value
309                 return $value;
310         }
311
312         /**
313          * Getter for cache file (FQFN)
314          *
315          * @return      $fqfn   Full-qualified file name of the menu cache
316          */
317         public function getMenuCacheFqfn () {
318                 $this->partialStub('Please implement this method.');
319         }
320
321         /**
322          * Starts the announcement
323          *
324          * @return      void
325          */
326         private function startAnnouncement () {
327                 // Push the node name on the stacker
328                 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
329         }
330
331         /**
332          * Starts the announcement data
333          *
334          * @return      void
335          */
336         private function startAnnouncementData () {
337                 // Push the node name on the stacker
338                 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
339         }
340
341         /**
342          * Starts the node status
343          *
344          * @return      void
345          */
346         private function startNodeStatus () {
347                 // Push the node name on the stacker
348                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
349         }
350
351         /**
352          * Starts the listener
353          *
354          * @return      void
355          */
356         private function startListener () {
357                 // Push the node name on the stacker
358                 $this->getStackerInstance()->pushNamed('announcement', 'listener');
359         }
360
361         /**
362          * Starts the TCP port
363          *
364          * @return      void
365          */
366         private function startTcpPort () {
367                 // Push the node name on the stacker
368                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
369         }
370
371         /**
372          * Starts the UDP port
373          *
374          * @return      void
375          */
376         private function startUdpPort () {
377                 // Push the node name on the stacker
378                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
379         }
380
381         /**
382          * Starts the session id
383          *
384          * @return      void
385          */
386         private function startSessionId () {
387                 // Push the node name on the stacker
388                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
389         }
390
391         /**
392          * Starts the public ip
393          *
394          * @return      void
395          */
396         private function startExternalIp () {
397                 // Push the node name on the stacker
398                 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
399         }
400
401         /**
402          * Starts the object type list
403          *
404          * @return      void
405          */
406         private function startObjectTypeList () {
407                 // Push the node name on the stacker
408                 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
409         }
410
411         /**
412          * Starts the object type
413          *
414          * @return      void
415          */
416         private function startObjectType () {
417                 // Push the node name on the stacker
418                 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
419         }
420
421         /**
422          * Finishes the object type
423          *
424          * @return      void
425          */
426         private function finishObjectType () {
427                 // Pop the last entry
428                 $this->getStackerInstance()->popNamed('announcement');
429         }
430
431         /**
432          * Finishes the object type list
433          *
434          * @return      void
435          */
436         private function finishObjectTypeList () {
437                 // Pop the last entry
438                 $this->getStackerInstance()->popNamed('announcement');
439         }
440
441         /**
442          * Finishes the session id
443          *
444          * @return      void
445          */
446         private function finishSessionId () {
447                 // Pop the last entry
448                 $this->getStackerInstance()->popNamed('announcement');
449         }
450
451         /**
452          * Finishes the public ip
453          *
454          * @return      void
455          */
456         private function finishExternalIp () {
457                 // Pop the last entry
458                 $this->getStackerInstance()->popNamed('announcement');
459         }
460
461         /**
462          * Finishes the UDP port
463          *
464          * @return      void
465          */
466         private function finishUdpPort () {
467                 // Pop the last entry
468                 $this->getStackerInstance()->popNamed('announcement');
469         }
470
471         /**
472          * Finishes the TCP port
473          *
474          * @return      void
475          */
476         private function finishTcpPort () {
477                 // Pop the last entry
478                 $this->getStackerInstance()->popNamed('announcement');
479         }
480
481         /**
482          * Finishes the listener
483          *
484          * @return      void
485          */
486         private function finishListener () {
487                 // Pop the last entry
488                 $this->getStackerInstance()->popNamed('announcement');
489         }
490
491         /**
492          * Finishes the node status
493          *
494          * @return      void
495          */
496         private function finishNodeStatus () {
497                 // Pop the last entry
498                 $this->getStackerInstance()->popNamed('announcement');
499         }
500
501         /**
502          * Finishes the announcement data
503          *
504          * @return      void
505          */
506         private function finishAnnouncementData () {
507                 // Pop the last entry
508                 $this->getStackerInstance()->popNamed('announcement');
509         }
510
511         /**
512          * Finishes the announcement
513          *
514          * @return      void
515          */
516         private function finishAnnouncement () {
517                 // Pop the last entry
518                 $this->getStackerInstance()->popNamed('announcement');
519         }
520 }
521
522 // [EOF]
523 ?>