3 * An Announcement template engine class for XML templates
5 * @author Roland Haeder <webmaster@ship-simu.org>
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()
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 XmlAnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
27 * Main nodes in the XML tree
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 * @return $templateInstance 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
80 public static final function createXmlAnnouncementTemplateEngine () {
82 $templateInstance = new XmlAnnouncementTemplateEngine();
84 // Get the application instance from registry
85 $applicationInstance = Registry::getRegistry()->getInstance('app');
87 // Determine base path
88 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
90 // Is the base path valid?
91 if (empty($templateBasePath)) {
93 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
94 } elseif (!is_string($templateBasePath)) {
96 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
97 } elseif (!is_dir($templateBasePath)) {
99 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
100 } elseif (!is_readable($templateBasePath)) {
102 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
106 $templateInstance->setTemplateBasePath($templateBasePath);
108 // Set template extensions
109 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
110 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_announcement_template_extension'));
112 // Absolute output path for compiled templates
113 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
115 // Init a variable stacker
116 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
119 $templateInstance->setStackerInstance($stackerInstance);
121 // Return the prepared instance
122 return $templateInstance;
126 * Load a specified announcement template into the engine
128 * @param $template The announcement template we shall load which is
129 * located in 'announcement' by default
132 public function loadAnnouncementTemplate ($template = 'self_announcement') {
134 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
136 // Load the special template
137 $this->loadTemplate($template);
141 * Getter for current main node
143 * @return $currMainNode Current main node
145 public final function getCurrMainNode () {
146 return $this->curr['main_node'];
150 * Setter for current main node
152 * @param $element Element name to set as current main node
153 * @return $currMainNode Current main node
155 private final function setCurrMainNode ($element) {
156 $this->curr['main_node'] = (string) $element;
160 * Getter for main node array
162 * @return $mainNodes Array with valid main node names
164 public final function getMainNodes () {
165 return $this->mainNodes;
169 * Getter for sub node array
171 * @return $subNodes Array with valid sub node names
173 public final function getSubNodes () {
174 return $this->subNodes;
178 * Handles the start element of an XML resource
180 * @param $resource XML parser resource (currently ignored)
181 * @param $element The element we shall handle
182 * @param $attributes All attributes
184 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
186 public function startElement ($resource, $element, array $attributes) {
187 // Initial method name which will never be called...
188 $methodName = 'initAnnouncement';
190 // Make the element name lower-case
191 $element = strtolower($element);
193 // Is the element a main node?
194 //* DEBUG: */ echo "START: >".$element."<<br />\n";
195 if (in_array($element, $this->getMainNodes())) {
196 // Okay, main node found!
197 $methodName = 'start' . $this->convertToClassName($element);
200 $this->setCurrMainNode($element);
201 } elseif (in_array($element, $this->getSubNodes())) {
203 $methodName = 'start' . $this->convertToClassName($element);
205 // Invalid node name found
206 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
210 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
211 call_user_func_array(array($this, $methodName), $attributes);
215 * Ends the main or sub node by sending out the gathered data
217 * @param $resource An XML resource pointer (currently ignored)
218 * @param $nodeName Name of the node we want to finish
220 * @throws XmlNodeMismatchException If current main node mismatches the closing one
222 public function endElement ($resource, $nodeName) {
223 // Make all lower-case
224 $nodeName = strtolower($nodeName);
226 // Does this match with current main node?
227 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
228 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
230 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
233 // Construct method name
234 $methodName = 'finish' . $this->convertToClassName($nodeName);
236 // Call the corresponding method
237 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
238 call_user_func_array(array($this, $methodName), array());
244 * @param $resource XML parser resource (currently ignored)
245 * @param $characters Characters to handle
247 * @todo Find something useful with this!
249 public function characterHandler ($resource, $characters) {
250 // Trim all spaces away
251 $characters = trim($characters);
253 // Is this string empty?
254 if (empty($characters)) {
255 // Then skip it silently
259 // Assign the found characters to variable and use the last entry from
261 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
265 * Handles the template dependency for given node
267 * @param $node The node we should load a dependency template
268 * @param $templateDependency A template to load to satisfy dependencies
271 private function handleTemplateDependency ($node, $templateDependency) {
272 // Is the template dependency set?
273 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
274 // Get a temporay template instance
275 $templateInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_template_class');
278 $templateInstance->loadAnnouncementTemplate($templateDependency);
280 // Parse the XML content
281 $templateInstance->renderXmlContent();
283 // Save the parsed raw content in our dependency array
284 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
289 * Getter for cache file (FQFN)
291 * @return $fqfn Full-qualified file name of the menu cache
293 public function getMenuCacheFqfn () {
294 $this->partialStub('Please implement this method.');
298 * Starts the announcement
302 private function startAnnouncement () {
303 // Push the node name on the stacker
304 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
308 * Starts the announcement data
312 private function startAnnouncementData () {
313 // Push the node name on the stacker
314 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
322 private function startNodeId () {
323 // Push the node name on the stacker
324 $this->getStackerInstance()->pushNamed('announcement', 'node-id');
328 * Starts the node status
332 private function startNodeStatus () {
333 // Push the node name on the stacker
334 $this->getStackerInstance()->pushNamed('announcement', 'node-status');
338 * Starts the listener
342 private function startListener () {
343 // Push the node name on the stacker
344 $this->getStackerInstance()->pushNamed('announcement', 'listener');
348 * Starts the client listener
352 private function startClientListener () {
353 // Push the node name on the stacker
354 $this->getStackerInstance()->pushNamed('announcement', 'client-listener');
358 * Starts the hub listener
362 private function startHubListener () {
363 // Push the node name on the stacker
364 $this->getStackerInstance()->pushNamed('announcement', 'hub-listener');
368 * Starts the TCP port
372 private function startTcpPort () {
373 // Push the node name on the stacker
374 $this->getStackerInstance()->pushNamed('announcement', 'tcp-port');
378 * Starts the UDP port
382 private function startUdpPort () {
383 // Push the node name on the stacker
384 $this->getStackerInstance()->pushNamed('announcement', 'udp-port');
388 * Starts the session id
392 private function startSessionId () {
393 // Push the node name on the stacker
394 $this->getStackerInstance()->pushNamed('announcement', 'session-id');
398 * Starts the object type list
402 private function startObjectTypeList () {
403 // Push the node name on the stacker
404 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
408 * Starts the object type
412 private function startObjectType () {
413 // Push the node name on the stacker
414 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
418 * Finishes the object type
422 private function finishObjectType () {
423 // Pop the last entry
424 $this->getStackerInstance()->popNamed('announcement');
428 * Finishes the object type list
432 private function finishObjectTypeList () {
433 // Pop the last entry
434 $this->getStackerInstance()->popNamed('announcement');
438 * Finishes the session id
442 private function finishSessionId () {
443 // Pop the last entry
444 $this->getStackerInstance()->popNamed('announcement');
448 * Finishes the UDP port
452 private function finishUdpPort () {
453 // Pop the last entry
454 $this->getStackerInstance()->popNamed('announcement');
458 * Finishes the TCP port
462 private function finishTcpPort () {
463 // Pop the last entry
464 $this->getStackerInstance()->popNamed('announcement');
468 * Finishes the hub listener
472 private function finishHubListener () {
473 // Pop the last entry
474 $this->getStackerInstance()->popNamed('announcement');
478 * Finishes the client listener
482 private function finishClientListener () {
483 // Pop the last entry
484 $this->getStackerInstance()->popNamed('announcement');
488 * Finishes the listener
492 private function finishListener () {
493 // Pop the last entry
494 $this->getStackerInstance()->popNamed('announcement');
498 * Finishes the node status
502 private function finishNodeStatus () {
503 // Pop the last entry
504 $this->getStackerInstance()->popNamed('announcement');
508 * Finishes the node id
512 private function finishNodeId () {
513 // Pop the last entry
514 $this->getStackerInstance()->popNamed('announcement');
518 * Finishes the announcement data
522 private function finishAnnouncementData () {
523 // Pop the last entry
524 $this->getStackerInstance()->popNamed('announcement');
528 * Finishes the announcement
532 private function finishAnnouncement () {
533 // Pop the last entry
534 $this->getStackerInstance()->popNamed('announcement');