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