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