3 * A Announcement template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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()
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.
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.
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/>.
25 class AnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
27 * Main nodes in the XML tree ('menu' is ignored)
29 private $mainNodes = array(
34 * Sub nodes in the XML tree
36 private $subNodes = array(
52 private $curr = array();
55 * Content from dependency
57 private $dependencyContent = array();
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates an instance of the class TemplateEngine and prepares it for usage
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
81 public final static function createAnnouncementTemplateEngine (ManageableApplication $appInstance) {
83 $templateInstance = new AnnouncementTemplateEngine();
85 // Get language and file I/O instances from application
86 $langInstance = $appInstance->getLanguageInstance();
87 $ioInstance = $appInstance->getFileIoInstance();
89 // Determine base path
90 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
92 // Is the base path valid?
93 if (empty($templateBasePath)) {
95 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($templateBasePath)) {
98 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
99 } elseif (!is_dir($templateBasePath)) {
101 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102 } elseif (!is_readable($templateBasePath)) {
104 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
107 // Get configuration instance
108 $configInstance = FrameworkConfiguration::getInstance();
111 $templateInstance->setTemplateBasePath($templateBasePath);
113 // Set the language and IO instances
114 $templateInstance->setLanguageInstance($langInstance);
115 $templateInstance->setFileIoInstance($ioInstance);
117 // Set template extensions
118 $templateInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
119 $templateInstance->setCodeTemplateExtension($configInstance->getConfigEntry('announcement_template_extension'));
121 // Absolute output path for compiled templates
122 $templateInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
124 // Init a variable stacker
125 $stackerInstance = ObjectFactory::createObjectByConfiguredName('announcement_stacker_class');
128 $templateInstance->setStackerInstance($stackerInstance);
130 // Return the prepared instance
131 return $templateInstance;
135 * Load a specified announcement template into the engine
137 * @param $template The announcement template we shall load which is
138 * located in 'announcement' by default
141 public function loadAnnouncementTemplate ($template) {
143 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('announcement_template_type'));
145 // Load the special template
146 $this->loadTemplate($template);
150 * Getter for current main node
152 * @return $currMainNode Current main node
154 public final function getCurrMainNode () {
155 return $this->curr['main_node'];
159 * Setter for current main node
161 * @param $element Element name to set as current main node
162 * @return $currMainNode Current main node
164 private final function setCurrMainNode ($element) {
165 $this->curr['main_node'] = (string) $element;
169 * Getter for main node array
171 * @return $mainNodes Array with valid main node names
173 public final function getMainNodes () {
174 return $this->mainNodes;
178 * Getter for sub node array
180 * @return $subNodes Array with valid sub node names
182 public final function getSubNodes () {
183 return $this->subNodes;
187 * Handles the start element of an XML resource
189 * @param $resource XML parser resource (currently ignored)
190 * @param $element The element we shall handle
191 * @param $attributes All attributes
193 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
195 public function startElement ($resource, $element, array $attributes) {
196 // Initial method name which will never be called...
197 $methodName = 'initMenu';
199 // Make the element name lower-case
200 $element = strtolower($element);
202 // Is the element a main node?
203 //* DEBUG: */ echo "START: >".$element."<<br />\n";
204 if (in_array($element, $this->getMainNodes())) {
205 // Okay, main node found!
206 $methodName = 'start' . $this->convertToClassName($element);
209 $this->setCurrMainNode($element);
210 } elseif (in_array($element, $this->getSubNodes())) {
212 $methodName = 'start' . $this->convertToClassName($element);
213 } elseif ($element != 'menu') {
214 // Invalid node name found
215 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
219 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
220 call_user_func_array(array($this, $methodName), $attributes);
224 * Ends the main or sub node by sending out the gathered data
226 * @param $resource An XML resource pointer (currently ignored)
227 * @param $nodeName Name of the node we want to finish
229 * @throws XmlNodeMismatchException If current main node mismatches the closing one
231 public function endElement ($resource, $nodeName) {
232 // Make all lower-case
233 $nodeName = strtolower($nodeName);
235 // Does this match with current main node?
236 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
237 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
239 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
242 // Construct method name
243 $methodName = 'finish' . $this->convertToClassName($nodeName);
245 // Call the corresponding method
246 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
247 call_user_func_array(array($this, $methodName), array());
253 * @param $resource XML parser resource (currently ignored)
254 * @param $characters Characters to handle
256 * @todo Find something useful with this!
258 public function characterHandler ($resource, $characters) {
259 // Trim all spaces away
260 $characters = trim($characters);
262 // Is this string empty?
263 if (empty($characters)) {
264 // Then skip it silently
268 // Assign the found characters to variable and use the last entry from
270 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
274 * Handles the template dependency for given node
276 * @param $node The node we should load a dependency template
277 * @param $templateDependency A template to load to satisfy dependencies
280 private function handleTemplateDependency ($node, $templateDependency) {
281 // Is the template dependency set?
282 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
283 // Get a temporay menu template instance
284 $templateInstance = ObjectFactory::createObjectByConfiguredName('announcement_template_class', array($this->getApplicationInstance()));
287 $templateInstance->loadAnnouncementTemplate($templateDependency);
289 // Get an XmlParser instance
290 $templateInstance->renderXmlContent();
292 // Parse the template's content contents
293 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
298 * Getter for cache file (FQFN)
300 * @return $fqfn Full-qualified file name of the menu cache
302 public function getMenuCacheFqfn () {
303 // Get the FQFN ready
304 $fqfn = sprintf("%s%s%s/%s.%s",
305 $this->getConfigInstance()->getConfigEntry('base_path'),
306 $this->getGenericBasePath(),
309 $this->getMenuInstance()->getMenuName() . ':' .
310 $this->__toString() . ':' .
311 $this->getMenuInstance()->__toString()
313 $this->getMenuInstance()->getMenuType()
321 * Starts the announcement
325 private function startAnnouncement () {
326 // Push the node name on the stacker
327 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
331 * Starts the announcement data
335 private function startAnnouncementData () {
336 // Push the node name on the stacker
337 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
345 private function startNodeId () {
346 // Push the node name on the stacker
347 $this->getStackerInstance()->pushNamed('announcement', 'node-id');
351 * Starts the node status
355 private function startNodeStatus () {
356 // Push the node name on the stacker
357 $this->getStackerInstance()->pushNamed('announcement', 'node-status');
361 * Starts the listener
365 private function startListener () {
366 // Push the node name on the stacker
367 $this->getStackerInstance()->pushNamed('announcement', 'listener');
371 * Starts the client listener
375 private function startClientListener () {
376 // Push the node name on the stacker
377 $this->getStackerInstance()->pushNamed('announcement', 'client-listener');
381 * Starts the hub listener
385 private function startHubListener () {
386 // Push the node name on the stacker
387 $this->getStackerInstance()->pushNamed('announcement', 'hub-listener');
391 * Starts the TCP port
395 private function startTcpPort () {
396 // Push the node name on the stacker
397 $this->getStackerInstance()->pushNamed('announcement', 'tcp-port');
401 * Starts the UDP port
405 private function startUdpPort () {
406 // Push the node name on the stacker
407 $this->getStackerInstance()->pushNamed('announcement', 'udp-port');
411 * Starts the session id
415 private function startSessionId () {
416 // Push the node name on the stacker
417 $this->getStackerInstance()->pushNamed('announcement', 'session-id');
421 * Starts the object type list
425 private function startObjectTypeList () {
426 // Push the node name on the stacker
427 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
431 * Starts the object type
435 private function startObjectType () {
436 // Push the node name on the stacker
437 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
441 * Finishes the object type
445 private function finishObjectType () {
446 // Pop the last entry
447 $this->getStackerInstance()->popNamed('announcement');
451 * Finishes the object type list
455 private function finishObjectTypeList () {
456 // Pop the last entry
457 $this->getStackerInstance()->popNamed('announcement');
461 * Finishes the session id
465 private function finishSessionId () {
466 // Pop the last entry
467 $this->getStackerInstance()->popNamed('announcement');
471 * Finishes the UDP port
475 private function finishUdpPort () {
476 // Pop the last entry
477 $this->getStackerInstance()->popNamed('announcement');
481 * Finishes the TCP port
485 private function finishTcpPort () {
486 // Pop the last entry
487 $this->getStackerInstance()->popNamed('announcement');
491 * Finishes the hub listener
495 private function finishHubListener () {
496 // Pop the last entry
497 $this->getStackerInstance()->popNamed('announcement');
501 * Finishes the client listener
505 private function finishClientListener () {
506 // Pop the last entry
507 $this->getStackerInstance()->popNamed('announcement');
511 * Finishes the listener
515 private function finishListener () {
516 // Pop the last entry
517 $this->getStackerInstance()->popNamed('announcement');
521 * Finishes the node status
525 private function finishNodeStatus () {
526 // Pop the last entry
527 $this->getStackerInstance()->popNamed('announcement');
531 * Finishes the node id
535 private function finishNodeId () {
536 // Pop the last entry
537 $this->getStackerInstance()->popNamed('announcement');
541 * Finishes the announcement data
545 private function finishAnnouncementData () {
546 // Pop the last entry
547 $this->getStackerInstance()->popNamed('announcement');
551 * Finishes the announcement
555 private function finishAnnouncement () {
556 // Pop the last entry
557 $this->getStackerInstance()->popNamed('announcement');