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