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_EXTERNAL_IP = 'external-ip';
30 const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
31 const ANNOUNCEMENT_DATA_SESSION_ID = 'session-id';
32 const ANNOUNCEMENT_DATA_TCP_PORT = 'tcp-port';
33 const ANNOUNCEMENT_DATA_UDP_PORT = 'udp-port';
36 * Main nodes in the XML tree
38 private $mainNodes = array(
43 * Sub nodes in the XML tree
45 private $subNodes = array();
50 private $curr = array();
53 * Content from dependency
55 private $dependencyContent = array();
58 * Protected constructor
62 protected function __construct () {
63 // Call parent constructor
64 parent::__construct(__CLASS__);
67 $this->subNodes = array(
70 self::ANNOUNCEMENT_DATA_NODE_STATUS,
71 self::ANNOUNCEMENT_DATA_TCP_PORT,
72 self::ANNOUNCEMENT_DATA_UDP_PORT,
73 self::ANNOUNCEMENT_DATA_SESSION_ID,
74 self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
80 * Creates an instance of the class TemplateEngine and prepares it for usage
82 * @return $templateInstance An instance of TemplateEngine
83 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
84 * @throws InvalidBasePathStringException If $templateBasePath is no string
85 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
86 * directory or not found
87 * @throws BasePathReadProtectedException If $templateBasePath is
90 public static final function createXmlAnnouncementTemplateEngine () {
92 $templateInstance = new XmlAnnouncementTemplateEngine();
94 // Get the application instance from registry
95 $applicationInstance = Registry::getRegistry()->getInstance('app');
97 // Determine base path
98 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
100 // Is the base path valid?
101 if (empty($templateBasePath)) {
102 // Base path is empty
103 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
104 } elseif (!is_string($templateBasePath)) {
106 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
107 } elseif (!is_dir($templateBasePath)) {
109 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
110 } elseif (!is_readable($templateBasePath)) {
112 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
116 $templateInstance->setTemplateBasePath($templateBasePath);
118 // Set template extensions
119 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
120 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_announcement_template_extension'));
122 // Absolute output path for compiled templates
123 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
125 // Init a variable stacker
126 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
129 $templateInstance->setStackerInstance($stackerInstance);
131 // Return the prepared instance
132 return $templateInstance;
136 * Load a specified announcement template into the engine
138 * @param $template The announcement template we shall load which is
139 * located in 'announcement' by default
142 public function loadAnnouncementTemplate ($template = 'self_announcement') {
144 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
146 // Load the special template
147 $this->loadTemplate($template);
151 * Getter for current main node
153 * @return $currMainNode Current main node
155 public final function getCurrMainNode () {
156 return $this->curr['main_node'];
160 * Setter for current main node
162 * @param $element Element name to set as current main node
163 * @return $currMainNode Current main node
165 private final function setCurrMainNode ($element) {
166 $this->curr['main_node'] = (string) $element;
170 * Getter for main node array
172 * @return $mainNodes Array with valid main node names
174 public final function getMainNodes () {
175 return $this->mainNodes;
179 * Getter for sub node array
181 * @return $subNodes Array with valid sub node names
183 public final function getSubNodes () {
184 return $this->subNodes;
188 * Handles the start element of an XML resource
190 * @param $resource XML parser resource (currently ignored)
191 * @param $element The element we shall handle
192 * @param $attributes All attributes
194 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
196 public function startElement ($resource, $element, array $attributes) {
197 // Initial method name which will never be called...
198 $methodName = 'initAnnouncement';
200 // Make the element name lower-case
201 $element = strtolower($element);
203 // Is the element a main node?
204 //* DEBUG: */ echo "START: >".$element."<<br />\n";
205 if (in_array($element, $this->getMainNodes())) {
206 // Okay, main node found!
207 $methodName = 'start' . $this->convertToClassName($element);
210 $this->setCurrMainNode($element);
211 } elseif (in_array($element, $this->getSubNodes())) {
213 $methodName = 'start' . $this->convertToClassName($element);
215 // Invalid node name found
216 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
220 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
221 call_user_func_array(array($this, $methodName), $attributes);
225 * Ends the main or sub node by sending out the gathered data
227 * @param $resource An XML resource pointer (currently ignored)
228 * @param $nodeName Name of the node we want to finish
230 * @throws XmlNodeMismatchException If current main node mismatches the closing one
232 public function endElement ($resource, $nodeName) {
233 // Make all lower-case
234 $nodeName = strtolower($nodeName);
236 // Does this match with current main node?
237 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
238 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
240 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
243 // Construct method name
244 $methodName = 'finish' . $this->convertToClassName($nodeName);
246 // Call the corresponding method
247 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
248 call_user_func_array(array($this, $methodName), array());
254 * @param $resource XML parser resource (currently ignored)
255 * @param $characters Characters to handle
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 template instance
284 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
287 $templateInstance->loadAnnouncementTemplate($templateDependency);
289 // Parse the XML content
290 $templateInstance->renderXmlContent();
292 // Save the parsed raw content in our dependency array
293 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
298 * Read announcement variables by calling readVariable() with 'general' as
301 * @param $key Key to read from
302 * @return $value Value from variable
304 public function readAnnouncementData ($key) {
306 $value = parent::readVariable($key, 'general');
313 * Getter for cache file (FQFN)
315 * @return $fqfn Full-qualified file name of the menu cache
317 public function getMenuCacheFqfn () {
318 $this->partialStub('Please implement this method.');
322 * Starts the announcement
326 private function startAnnouncement () {
327 // Push the node name on the stacker
328 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
332 * Starts the announcement data
336 private function startAnnouncementData () {
337 // Push the node name on the stacker
338 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
342 * Starts the node status
346 private function startNodeStatus () {
347 // Push the node name on the stacker
348 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
352 * Starts the listener
356 private function startListener () {
357 // Push the node name on the stacker
358 $this->getStackerInstance()->pushNamed('announcement', 'listener');
362 * Starts the TCP port
366 private function startTcpPort () {
367 // Push the node name on the stacker
368 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
372 * Starts the UDP port
376 private function startUdpPort () {
377 // Push the node name on the stacker
378 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
382 * Starts the session id
386 private function startSessionId () {
387 // Push the node name on the stacker
388 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
392 * Starts the public ip
396 private function startExternalIp () {
397 // Push the node name on the stacker
398 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
402 * Starts the object type list
406 private function startObjectTypeList () {
407 // Push the node name on the stacker
408 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
412 * Starts the object type
416 private function startObjectType () {
417 // Push the node name on the stacker
418 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
422 * Finishes the object type
426 private function finishObjectType () {
427 // Pop the last entry
428 $this->getStackerInstance()->popNamed('announcement');
432 * Finishes the object type list
436 private function finishObjectTypeList () {
437 // Pop the last entry
438 $this->getStackerInstance()->popNamed('announcement');
442 * Finishes the session id
446 private function finishSessionId () {
447 // Pop the last entry
448 $this->getStackerInstance()->popNamed('announcement');
452 * Finishes the public ip
456 private function finishExternalIp () {
457 // Pop the last entry
458 $this->getStackerInstance()->popNamed('announcement');
462 * Finishes the UDP port
466 private function finishUdpPort () {
467 // Pop the last entry
468 $this->getStackerInstance()->popNamed('announcement');
472 * Finishes the TCP port
476 private function finishTcpPort () {
477 // Pop the last entry
478 $this->getStackerInstance()->popNamed('announcement');
482 * Finishes the listener
486 private function finishListener () {
487 // Pop the last entry
488 $this->getStackerInstance()->popNamed('announcement');
492 * Finishes the node status
496 private function finishNodeStatus () {
497 // Pop the last entry
498 $this->getStackerInstance()->popNamed('announcement');
502 * Finishes the announcement data
506 private function finishAnnouncementData () {
507 // Pop the last entry
508 $this->getStackerInstance()->popNamed('announcement');
512 * Finishes the announcement
516 private function finishAnnouncement () {
517 // Pop the last entry
518 $this->getStackerInstance()->popNamed('announcement');