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 - 2012 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, Registerable {
27 * Some XML nodes must be available for later data extraction
29 const ANNOUNCEMENT_DATA_SESSION_ID = 'session-id';
30 const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
31 const ANNOUNCEMENT_DATA_NODE_MODE = 'node-mode';
32 const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'external-ip';
33 const ANNOUNCEMENT_DATA_INTERNAL_IP = 'internal-ip';
34 const ANNOUNCEMENT_DATA_TCP_PORT = 'tcp-port';
35 const ANNOUNCEMENT_DATA_UDP_PORT = 'udp-port';
38 * Main nodes in the XML tree
40 private $mainNodes = array(
45 * Sub nodes in the XML tree
47 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__);
69 $this->subNodes = array(
72 self::ANNOUNCEMENT_DATA_NODE_STATUS,
73 self::ANNOUNCEMENT_DATA_TCP_PORT,
74 self::ANNOUNCEMENT_DATA_UDP_PORT,
75 self::ANNOUNCEMENT_DATA_SESSION_ID,
76 self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
77 self::ANNOUNCEMENT_DATA_INTERNAL_IP,
83 * Creates an instance of the class TemplateEngine and prepares it for usage
85 * @return $templateInstance An instance of TemplateEngine
86 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
87 * @throws InvalidBasePathStringException If $templateBasePath is no string
88 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
89 * directory or not found
90 * @throws BasePathReadProtectedException If $templateBasePath is
93 public static final function createXmlAnnouncementTemplateEngine () {
95 $templateInstance = new XmlAnnouncementTemplateEngine();
97 // Get the application instance from registry
98 $applicationInstance = Registry::getRegistry()->getInstance('app');
100 // Determine base path
101 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
103 // Is the base path valid?
104 if (empty($templateBasePath)) {
105 // Base path is empty
106 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
107 } elseif (!is_string($templateBasePath)) {
109 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
110 } elseif (!is_dir($templateBasePath)) {
112 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
113 } elseif (!is_readable($templateBasePath)) {
115 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
119 $templateInstance->setTemplateBasePath($templateBasePath);
121 // Set template extensions
122 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
123 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
125 // Absolute output path for compiled templates
126 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
128 // Init a variable stacker
129 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
132 $templateInstance->setStackerInstance($stackerInstance);
134 // Return the prepared instance
135 return $templateInstance;
139 * Load a specified announcement template into the engine
141 * @param $template The announcement template we shall load which is
142 * located in 'announcement' by default
145 public function loadAnnouncementTemplate ($template = 'self_announcement') {
147 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
149 // Load the special template
150 $this->loadTemplate($template);
154 * Getter for current main node
156 * @return $currMainNode Current main node
158 public final function getCurrMainNode () {
159 return $this->curr['main_node'];
163 * Setter for current main node
165 * @param $element Element name to set as current main node
166 * @return $currMainNode Current main node
168 private final function setCurrMainNode ($element) {
169 $this->curr['main_node'] = (string) $element;
173 * Getter for main node array
175 * @return $mainNodes Array with valid main node names
177 public final function getMainNodes () {
178 return $this->mainNodes;
182 * Getter for sub node array
184 * @return $subNodes Array with valid sub node names
186 public final function getSubNodes () {
187 return $this->subNodes;
191 * Handles the start element of an XML resource
193 * @param $resource XML parser resource (currently ignored)
194 * @param $element The element we shall handle
195 * @param $attributes All attributes
197 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
199 public function startElement ($resource, $element, array $attributes) {
200 // Initial method name which will never be called...
201 $methodName = 'initAnnouncement';
203 // Make the element name lower-case
204 $element = strtolower($element);
206 // Is the element a main node?
207 //* DEBUG: */ echo "START: >".$element."<<br />\n";
208 if (in_array($element, $this->getMainNodes())) {
209 // Okay, main node found!
210 $methodName = 'start' . $this->convertToClassName($element);
213 $this->setCurrMainNode($element);
214 } elseif (in_array($element, $this->getSubNodes())) {
216 $methodName = 'start' . $this->convertToClassName($element);
218 // Invalid node name found
219 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
223 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
224 call_user_func_array(array($this, $methodName), $attributes);
228 * Ends the main or sub node by sending out the gathered data
230 * @param $resource An XML resource pointer (currently ignored)
231 * @param $nodeName Name of the node we want to finish
233 * @throws XmlNodeMismatchException If current main node mismatches the closing one
235 public function endElement ($resource, $nodeName) {
236 // Make all lower-case
237 $nodeName = strtolower($nodeName);
239 // Does this match with current main node?
240 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
241 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
243 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
246 // Construct method name
247 $methodName = 'finish' . $this->convertToClassName($nodeName);
249 // Call the corresponding method
250 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
251 call_user_func_array(array($this, $methodName), array());
257 * @param $resource XML parser resource (currently ignored)
258 * @param $characters Characters to handle
261 public function characterHandler ($resource, $characters) {
262 // Trim all spaces away
263 $characters = trim($characters);
265 // Is this string empty?
266 if (empty($characters)) {
267 // Then skip it silently
272 * Assign the found characters to variable and use the last entry from
275 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
279 * Handles the template dependency for given node
281 * @param $node The node we should load a dependency template
282 * @param $templateDependency A template to load to satisfy dependencies
285 private function handleTemplateDependency ($node, $templateDependency) {
286 // Is the template dependency set?
287 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
288 // Get a temporay template instance
289 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
292 $templateInstance->loadAnnouncementTemplate($templateDependency);
294 // Parse the XML content
295 $templateInstance->renderXmlContent();
297 // Save the parsed raw content in our dependency array
298 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
303 * Read announcement variables by calling readVariable() with 'general' as
306 * @param $key Key to read from
307 * @return $value Value from variable
309 public function readAnnouncementData ($key) {
311 $value = parent::readVariable($key, 'general');
318 * Getter for cache file (FQFN)
320 * @return $fqfn Full-qualified file name of the menu cache
322 public function getMenuCacheFqfn () {
323 $this->partialStub('Please implement this method.');
327 * Starts the announcement
331 private function startAnnouncement () {
332 // Push the node name on the stacker
333 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
337 * Starts the announcement data
341 private function startAnnouncementData () {
342 // Push the node name on the stacker
343 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
347 * Starts the node status
351 private function startNodeStatus () {
352 // Push the node name on the stacker
353 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
357 * Starts the node-mode
361 private function startNodeMode () {
362 // Push the node name on the stacker
363 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_MODE);
367 * Starts the listener
371 private function startListener () {
372 // Push the node name on the stacker
373 $this->getStackerInstance()->pushNamed('announcement', 'listener');
377 * Starts the TCP port
381 private function startTcpPort () {
382 // Push the node name on the stacker
383 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
387 * Starts the UDP port
391 private function startUdpPort () {
392 // Push the node name on the stacker
393 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
397 * Starts the session id
401 private function startSessionId () {
402 // Push the node name on the stacker
403 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
407 * Starts the public ip
411 private function startExternalIp () {
412 // Push the node name on the stacker
413 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
417 * Starts the private ip
421 private function startInternalIp () {
422 // Push the node name on the stacker
423 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
427 * Starts the object type list
431 private function startObjectTypeList () {
432 // Push the node name on the stacker
433 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
437 * Starts the object type
441 private function startObjectType () {
442 // Push the node name on the stacker
443 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
447 * Finishes the object type
451 private function finishObjectType () {
452 // Pop the last entry
453 $this->getStackerInstance()->popNamed('announcement');
457 * Finishes the object type list
461 private function finishObjectTypeList () {
462 // Pop the last entry
463 $this->getStackerInstance()->popNamed('announcement');
467 * Finishes the session id
471 private function finishSessionId () {
472 // Pop the last entry
473 $this->getStackerInstance()->popNamed('announcement');
477 * Finishes the private ip
481 private function finishInternalIp () {
482 // Pop the last entry
483 $this->getStackerInstance()->popNamed('announcement');
487 * Finishes the public ip
491 private function finishExternalIp () {
492 // Pop the last entry
493 $this->getStackerInstance()->popNamed('announcement');
497 * Finishes the UDP port
501 private function finishUdpPort () {
502 // Pop the last entry
503 $this->getStackerInstance()->popNamed('announcement');
507 * Finishes the TCP port
511 private function finishTcpPort () {
512 // Pop the last entry
513 $this->getStackerInstance()->popNamed('announcement');
517 * Finishes the listener
521 private function finishListener () {
522 // Pop the last entry
523 $this->getStackerInstance()->popNamed('announcement');
527 * Finishes the node mode
531 private function finishNodeMode () {
532 // Pop the last entry
533 $this->getStackerInstance()->popNamed('announcement');
537 * Finishes the node status
541 private function finishNodeStatus () {
542 // Pop the last entry
543 $this->getStackerInstance()->popNamed('announcement');
547 * Finishes the announcement data
551 private function finishAnnouncementData () {
552 // Pop the last entry
553 $this->getStackerInstance()->popNamed('announcement');
557 * Finishes the announcement
561 private function finishAnnouncement () {
562 // Pop the last entry
563 $this->getStackerInstance()->popNamed('announcement');