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_NODE_MODE,
74 self::ANNOUNCEMENT_DATA_TCP_PORT,
75 self::ANNOUNCEMENT_DATA_UDP_PORT,
76 self::ANNOUNCEMENT_DATA_SESSION_ID,
77 self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
78 self::ANNOUNCEMENT_DATA_INTERNAL_IP,
84 * Creates an instance of the class TemplateEngine and prepares it for usage
86 * @return $templateInstance An instance of TemplateEngine
87 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
88 * @throws InvalidBasePathStringException If $templateBasePath is no string
89 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
90 * directory or not found
91 * @throws BasePathReadProtectedException If $templateBasePath is
94 public static final function createXmlAnnouncementTemplateEngine () {
96 $templateInstance = new XmlAnnouncementTemplateEngine();
98 // Get the application instance from registry
99 $applicationInstance = Registry::getRegistry()->getInstance('app');
101 // Determine base path
102 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
104 // Is the base path valid?
105 if (empty($templateBasePath)) {
106 // Base path is empty
107 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
108 } elseif (!is_string($templateBasePath)) {
110 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
111 } elseif (!is_dir($templateBasePath)) {
113 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
114 } elseif (!is_readable($templateBasePath)) {
116 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
120 $templateInstance->setTemplateBasePath($templateBasePath);
122 // Set template extensions
123 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
124 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
126 // Absolute output path for compiled templates
127 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
129 // Init a variable stacker
130 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
133 $templateInstance->setStackerInstance($stackerInstance);
135 // Return the prepared instance
136 return $templateInstance;
140 * Load a specified announcement template into the engine
142 * @param $template The announcement template we shall load which is
143 * located in 'announcement' by default
146 public function loadAnnouncementTemplate ($template = 'self_announcement') {
148 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
150 // Load the special template
151 $this->loadTemplate($template);
155 * Getter for current main node
157 * @return $currMainNode Current main node
159 public final function getCurrMainNode () {
160 return $this->curr['main_node'];
164 * Setter for current main node
166 * @param $element Element name to set as current main node
167 * @return $currMainNode Current main node
169 private final function setCurrMainNode ($element) {
170 $this->curr['main_node'] = (string) $element;
174 * Getter for main node array
176 * @return $mainNodes Array with valid main node names
178 public final function getMainNodes () {
179 return $this->mainNodes;
183 * Getter for sub node array
185 * @return $subNodes Array with valid sub node names
187 public final function getSubNodes () {
188 return $this->subNodes;
192 * Handles the start element of an XML resource
194 * @param $resource XML parser resource (currently ignored)
195 * @param $element The element we shall handle
196 * @param $attributes All attributes
198 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
200 public function startElement ($resource, $element, array $attributes) {
201 // Initial method name which will never be called...
202 $methodName = 'initAnnouncement';
204 // Make the element name lower-case
205 $element = strtolower($element);
207 // Is the element a main node?
208 //* DEBUG: */ echo "START: >".$element."<<br />\n";
209 if (in_array($element, $this->getMainNodes())) {
210 // Okay, main node found!
211 $methodName = 'start' . $this->convertToClassName($element);
214 $this->setCurrMainNode($element);
215 } elseif (in_array($element, $this->getSubNodes())) {
217 $methodName = 'start' . $this->convertToClassName($element);
219 // Invalid node name found
220 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
224 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
225 call_user_func_array(array($this, $methodName), $attributes);
229 * Ends the main or sub node by sending out the gathered data
231 * @param $resource An XML resource pointer (currently ignored)
232 * @param $nodeName Name of the node we want to finish
234 * @throws XmlNodeMismatchException If current main node mismatches the closing one
236 public function endElement ($resource, $nodeName) {
237 // Make all lower-case
238 $nodeName = strtolower($nodeName);
240 // Does this match with current main node?
241 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
242 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
244 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
247 // Construct method name
248 $methodName = 'finish' . $this->convertToClassName($nodeName);
250 // Call the corresponding method
251 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
252 call_user_func_array(array($this, $methodName), array());
258 * @param $resource XML parser resource (currently ignored)
259 * @param $characters Characters to handle
262 public function characterHandler ($resource, $characters) {
263 // Trim all spaces away
264 $characters = trim($characters);
266 // Is this string empty?
267 if (empty($characters)) {
268 // Then skip it silently
273 * Assign the found characters to variable and use the last entry from
276 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
280 * Handles the template dependency for given node
282 * @param $node The node we should load a dependency template
283 * @param $templateDependency A template to load to satisfy dependencies
286 private function handleTemplateDependency ($node, $templateDependency) {
287 // Is the template dependency set?
288 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
289 // Get a temporay template instance
290 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
293 $templateInstance->loadAnnouncementTemplate($templateDependency);
295 // Parse the XML content
296 $templateInstance->renderXmlContent();
298 // Save the parsed raw content in our dependency array
299 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
304 * Read announcement variables by calling readVariable() with 'general' as
307 * @param $key Key to read from
308 * @return $value Value from variable
310 public function readAnnouncementData ($key) {
312 $value = parent::readVariable($key, 'general');
319 * Getter for cache file (FQFN)
321 * @return $fqfn Full-qualified file name of the menu cache
323 public function getMenuCacheFqfn () {
324 $this->partialStub('Please implement this method.');
328 * Starts the announcement
332 private function startAnnouncement () {
333 // Push the node name on the stacker
334 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
338 * Starts the announcement data
342 private function startAnnouncementData () {
343 // Push the node name on the stacker
344 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
348 * Starts the node status
352 private function startNodeStatus () {
353 // Push the node name on the stacker
354 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
358 * Starts the node-mode
362 private function startNodeMode () {
363 // Push the node name on the stacker
364 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_MODE);
368 * Starts the listener
372 private function startListener () {
373 // Push the node name on the stacker
374 $this->getStackerInstance()->pushNamed('announcement', 'listener');
378 * Starts the TCP port
382 private function startTcpPort () {
383 // Push the node name on the stacker
384 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
388 * Starts the UDP port
392 private function startUdpPort () {
393 // Push the node name on the stacker
394 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
398 * Starts the session id
402 private function startSessionId () {
403 // Push the node name on the stacker
404 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
408 * Starts the public ip
412 private function startExternalIp () {
413 // Push the node name on the stacker
414 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
418 * Starts the private ip
422 private function startInternalIp () {
423 // Push the node name on the stacker
424 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
428 * Starts the object type list
432 private function startObjectTypeList () {
433 // Push the node name on the stacker
434 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
438 * Starts the object type
442 private function startObjectType () {
443 // Push the node name on the stacker
444 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
448 * Finishes the object type
452 private function finishObjectType () {
453 // Pop the last entry
454 $this->getStackerInstance()->popNamed('announcement');
458 * Finishes the object type list
462 private function finishObjectTypeList () {
463 // Pop the last entry
464 $this->getStackerInstance()->popNamed('announcement');
468 * Finishes the session id
472 private function finishSessionId () {
473 // Pop the last entry
474 $this->getStackerInstance()->popNamed('announcement');
478 * Finishes the private ip
482 private function finishInternalIp () {
483 // Pop the last entry
484 $this->getStackerInstance()->popNamed('announcement');
488 * Finishes the public ip
492 private function finishExternalIp () {
493 // Pop the last entry
494 $this->getStackerInstance()->popNamed('announcement');
498 * Finishes the UDP port
502 private function finishUdpPort () {
503 // Pop the last entry
504 $this->getStackerInstance()->popNamed('announcement');
508 * Finishes the TCP port
512 private function finishTcpPort () {
513 // Pop the last entry
514 $this->getStackerInstance()->popNamed('announcement');
518 * Finishes the listener
522 private function finishListener () {
523 // Pop the last entry
524 $this->getStackerInstance()->popNamed('announcement');
528 * Finishes the node mode
532 private function finishNodeMode () {
533 // Pop the last entry
534 $this->getStackerInstance()->popNamed('announcement');
538 * Finishes the node status
542 private function finishNodeStatus () {
543 // Pop the last entry
544 $this->getStackerInstance()->popNamed('announcement');
548 * Finishes the announcement data
552 private function finishAnnouncementData () {
553 // Pop the last entry
554 $this->getStackerInstance()->popNamed('announcement');
558 * Finishes the announcement
562 private function finishAnnouncement () {
563 // Pop the last entry
564 $this->getStackerInstance()->popNamed('announcement');