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_EXTERNAL_IP = 'external-ip';
32 const ANNOUNCEMENT_DATA_INTERNAL_IP = 'internal-ip';
33 const ANNOUNCEMENT_DATA_TCP_PORT = 'tcp-port';
34 const ANNOUNCEMENT_DATA_UDP_PORT = 'udp-port';
37 * Main nodes in the XML tree
39 private $mainNodes = array(
44 * Sub nodes in the XML tree
46 private $subNodes = array();
51 private $curr = array();
54 * Content from dependency
56 private $dependencyContent = array();
59 * Protected constructor
63 protected function __construct () {
64 // Call parent constructor
65 parent::__construct(__CLASS__);
68 $this->subNodes = array(
71 self::ANNOUNCEMENT_DATA_NODE_STATUS,
72 self::ANNOUNCEMENT_DATA_TCP_PORT,
73 self::ANNOUNCEMENT_DATA_UDP_PORT,
74 self::ANNOUNCEMENT_DATA_SESSION_ID,
75 self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
76 self::ANNOUNCEMENT_DATA_INTERNAL_IP,
82 * Creates an instance of the class TemplateEngine and prepares it for usage
84 * @return $templateInstance An instance of TemplateEngine
85 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
86 * @throws InvalidBasePathStringException If $templateBasePath is no string
87 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
88 * directory or not found
89 * @throws BasePathReadProtectedException If $templateBasePath is
92 public static final function createXmlAnnouncementTemplateEngine () {
94 $templateInstance = new XmlAnnouncementTemplateEngine();
96 // Get the application instance from registry
97 $applicationInstance = Registry::getRegistry()->getInstance('app');
99 // Determine base path
100 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
102 // Is the base path valid?
103 if (empty($templateBasePath)) {
104 // Base path is empty
105 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
106 } elseif (!is_string($templateBasePath)) {
108 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
109 } elseif (!is_dir($templateBasePath)) {
111 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
112 } elseif (!is_readable($templateBasePath)) {
114 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
118 $templateInstance->setTemplateBasePath($templateBasePath);
120 // Set template extensions
121 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
122 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
124 // Absolute output path for compiled templates
125 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
127 // Init a variable stacker
128 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
131 $templateInstance->setStackerInstance($stackerInstance);
133 // Return the prepared instance
134 return $templateInstance;
138 * Load a specified announcement template into the engine
140 * @param $template The announcement template we shall load which is
141 * located in 'announcement' by default
144 public function loadAnnouncementTemplate ($template = 'self_announcement') {
146 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
148 // Load the special template
149 $this->loadTemplate($template);
153 * Getter for current main node
155 * @return $currMainNode Current main node
157 public final function getCurrMainNode () {
158 return $this->curr['main_node'];
162 * Setter for current main node
164 * @param $element Element name to set as current main node
165 * @return $currMainNode Current main node
167 private final function setCurrMainNode ($element) {
168 $this->curr['main_node'] = (string) $element;
172 * Getter for main node array
174 * @return $mainNodes Array with valid main node names
176 public final function getMainNodes () {
177 return $this->mainNodes;
181 * Getter for sub node array
183 * @return $subNodes Array with valid sub node names
185 public final function getSubNodes () {
186 return $this->subNodes;
190 * Handles the start element of an XML resource
192 * @param $resource XML parser resource (currently ignored)
193 * @param $element The element we shall handle
194 * @param $attributes All attributes
196 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
198 public function startElement ($resource, $element, array $attributes) {
199 // Initial method name which will never be called...
200 $methodName = 'initAnnouncement';
202 // Make the element name lower-case
203 $element = strtolower($element);
205 // Is the element a main node?
206 //* DEBUG: */ echo "START: >".$element."<<br />\n";
207 if (in_array($element, $this->getMainNodes())) {
208 // Okay, main node found!
209 $methodName = 'start' . $this->convertToClassName($element);
212 $this->setCurrMainNode($element);
213 } elseif (in_array($element, $this->getSubNodes())) {
215 $methodName = 'start' . $this->convertToClassName($element);
217 // Invalid node name found
218 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
222 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
223 call_user_func_array(array($this, $methodName), $attributes);
227 * Ends the main or sub node by sending out the gathered data
229 * @param $resource An XML resource pointer (currently ignored)
230 * @param $nodeName Name of the node we want to finish
232 * @throws XmlNodeMismatchException If current main node mismatches the closing one
234 public function endElement ($resource, $nodeName) {
235 // Make all lower-case
236 $nodeName = strtolower($nodeName);
238 // Does this match with current main node?
239 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
240 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
242 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
245 // Construct method name
246 $methodName = 'finish' . $this->convertToClassName($nodeName);
248 // Call the corresponding method
249 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
250 call_user_func_array(array($this, $methodName), array());
256 * @param $resource XML parser resource (currently ignored)
257 * @param $characters Characters to handle
260 public function characterHandler ($resource, $characters) {
261 // Trim all spaces away
262 $characters = trim($characters);
264 // Is this string empty?
265 if (empty($characters)) {
266 // Then skip it silently
271 * Assign the found characters to variable and use the last entry from
274 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
278 * Handles the template dependency for given node
280 * @param $node The node we should load a dependency template
281 * @param $templateDependency A template to load to satisfy dependencies
284 private function handleTemplateDependency ($node, $templateDependency) {
285 // Is the template dependency set?
286 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
287 // Get a temporay template instance
288 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
291 $templateInstance->loadAnnouncementTemplate($templateDependency);
293 // Parse the XML content
294 $templateInstance->renderXmlContent();
296 // Save the parsed raw content in our dependency array
297 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
302 * Read announcement variables by calling readVariable() with 'general' as
305 * @param $key Key to read from
306 * @return $value Value from variable
308 public function readAnnouncementData ($key) {
310 $value = parent::readVariable($key, 'general');
317 * Getter for cache file (FQFN)
319 * @return $fqfn Full-qualified file name of the menu cache
321 public function getMenuCacheFqfn () {
322 $this->partialStub('Please implement this method.');
326 * Starts the announcement
330 private function startAnnouncement () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
336 * Starts the announcement data
340 private function startAnnouncementData () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
346 * Starts the node status
350 private function startNodeStatus () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_NODE_STATUS);
356 * Starts the listener
360 private function startListener () {
361 // Push the node name on the stacker
362 $this->getStackerInstance()->pushNamed('announcement', 'listener');
366 * Starts the TCP port
370 private function startTcpPort () {
371 // Push the node name on the stacker
372 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_TCP_PORT);
376 * Starts the UDP port
380 private function startUdpPort () {
381 // Push the node name on the stacker
382 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_UDP_PORT);
386 * Starts the session id
390 private function startSessionId () {
391 // Push the node name on the stacker
392 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_SESSION_ID);
396 * Starts the public ip
400 private function startExternalIp () {
401 // Push the node name on the stacker
402 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
406 * Starts the private ip
410 private function startInternalIp () {
411 // Push the node name on the stacker
412 $this->getStackerInstance()->pushNamed('announcement', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
416 * Starts the object type list
420 private function startObjectTypeList () {
421 // Push the node name on the stacker
422 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
426 * Starts the object type
430 private function startObjectType () {
431 // Push the node name on the stacker
432 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
436 * Finishes the object type
440 private function finishObjectType () {
441 // Pop the last entry
442 $this->getStackerInstance()->popNamed('announcement');
446 * Finishes the object type list
450 private function finishObjectTypeList () {
451 // Pop the last entry
452 $this->getStackerInstance()->popNamed('announcement');
456 * Finishes the session id
460 private function finishSessionId () {
461 // Pop the last entry
462 $this->getStackerInstance()->popNamed('announcement');
466 * Finishes the private ip
470 private function finishInternalIp () {
471 // Pop the last entry
472 $this->getStackerInstance()->popNamed('announcement');
476 * Finishes the public ip
480 private function finishExternalIp () {
481 // Pop the last entry
482 $this->getStackerInstance()->popNamed('announcement');
486 * Finishes the UDP port
490 private function finishUdpPort () {
491 // Pop the last entry
492 $this->getStackerInstance()->popNamed('announcement');
496 * Finishes the TCP port
500 private function finishTcpPort () {
501 // Pop the last entry
502 $this->getStackerInstance()->popNamed('announcement');
506 * Finishes the listener
510 private function finishListener () {
511 // Pop the last entry
512 $this->getStackerInstance()->popNamed('announcement');
516 * Finishes the node status
520 private function finishNodeStatus () {
521 // Pop the last entry
522 $this->getStackerInstance()->popNamed('announcement');
526 * Finishes the announcement data
530 private function finishAnnouncementData () {
531 // Pop the last entry
532 $this->getStackerInstance()->popNamed('announcement');
536 * Finishes the announcement
540 private function finishAnnouncement () {
541 // Pop the last entry
542 $this->getStackerInstance()->popNamed('announcement');