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, Registerable {
27 * Main nodes in the XML tree
29 private $mainNodes = array(
34 * Sub nodes in the XML tree
36 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__);
69 * Creates an instance of the class TemplateEngine and prepares it for usage
71 * @return $templateInstance An instance of TemplateEngine
72 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
73 * @throws InvalidBasePathStringException If $templateBasePath is no string
74 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
75 * directory or not found
76 * @throws BasePathReadProtectedException If $templateBasePath is
79 public static final function createXmlAnnouncementTemplateEngine () {
81 $templateInstance = new XmlAnnouncementTemplateEngine();
83 // Get the application instance from registry
84 $applicationInstance = Registry::getRegistry()->getInstance('app');
86 // Determine base path
87 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
89 // Is the base path valid?
90 if (empty($templateBasePath)) {
92 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
93 } elseif (!is_string($templateBasePath)) {
95 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
96 } elseif (!is_dir($templateBasePath)) {
98 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
99 } elseif (!is_readable($templateBasePath)) {
101 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
105 $templateInstance->setTemplateBasePath($templateBasePath);
107 // Set template extensions
108 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
109 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_announcement_template_extension'));
111 // Absolute output path for compiled templates
112 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
114 // Init a variable stacker
115 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_stacker_class');
118 $templateInstance->setStackerInstance($stackerInstance);
120 // Return the prepared instance
121 return $templateInstance;
125 * Load a specified announcement template into the engine
127 * @param $template The announcement template we shall load which is
128 * located in 'announcement' by default
131 public function loadAnnouncementTemplate ($template = 'self_announcement') {
133 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_template_type'));
135 // Load the special template
136 $this->loadTemplate($template);
140 * Getter for current main node
142 * @return $currMainNode Current main node
144 public final function getCurrMainNode () {
145 return $this->curr['main_node'];
149 * Setter for current main node
151 * @param $element Element name to set as current main node
152 * @return $currMainNode Current main node
154 private final function setCurrMainNode ($element) {
155 $this->curr['main_node'] = (string) $element;
159 * Getter for main node array
161 * @return $mainNodes Array with valid main node names
163 public final function getMainNodes () {
164 return $this->mainNodes;
168 * Getter for sub node array
170 * @return $subNodes Array with valid sub node names
172 public final function getSubNodes () {
173 return $this->subNodes;
177 * Handles the start element of an XML resource
179 * @param $resource XML parser resource (currently ignored)
180 * @param $element The element we shall handle
181 * @param $attributes All attributes
183 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
185 public function startElement ($resource, $element, array $attributes) {
186 // Initial method name which will never be called...
187 $methodName = 'initAnnouncement';
189 // Make the element name lower-case
190 $element = strtolower($element);
192 // Is the element a main node?
193 //* DEBUG: */ echo "START: >".$element."<<br />\n";
194 if (in_array($element, $this->getMainNodes())) {
195 // Okay, main node found!
196 $methodName = 'start' . $this->convertToClassName($element);
199 $this->setCurrMainNode($element);
200 } elseif (in_array($element, $this->getSubNodes())) {
202 $methodName = 'start' . $this->convertToClassName($element);
204 // Invalid node name found
205 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
209 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
210 call_user_func_array(array($this, $methodName), $attributes);
214 * Ends the main or sub node by sending out the gathered data
216 * @param $resource An XML resource pointer (currently ignored)
217 * @param $nodeName Name of the node we want to finish
219 * @throws XmlNodeMismatchException If current main node mismatches the closing one
221 public function endElement ($resource, $nodeName) {
222 // Make all lower-case
223 $nodeName = strtolower($nodeName);
225 // Does this match with current main node?
226 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
227 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
229 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
232 // Construct method name
233 $methodName = 'finish' . $this->convertToClassName($nodeName);
235 // Call the corresponding method
236 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
237 call_user_func_array(array($this, $methodName), array());
243 * @param $resource XML parser resource (currently ignored)
244 * @param $characters Characters to handle
247 public function characterHandler ($resource, $characters) {
248 // Trim all spaces away
249 $characters = trim($characters);
251 // Is this string empty?
252 if (empty($characters)) {
253 // Then skip it silently
257 // Assign the found characters to variable and use the last entry from
259 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
263 * Handles the template dependency for given node
265 * @param $node The node we should load a dependency template
266 * @param $templateDependency A template to load to satisfy dependencies
269 private function handleTemplateDependency ($node, $templateDependency) {
270 // Is the template dependency set?
271 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
272 // Get a temporay template instance
273 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_announcement_template_class');
276 $templateInstance->loadAnnouncementTemplate($templateDependency);
278 // Parse the XML content
279 $templateInstance->renderXmlContent();
281 // Save the parsed raw content in our dependency array
282 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
287 * Getter for cache file (FQFN)
289 * @return $fqfn Full-qualified file name of the menu cache
291 public function getMenuCacheFqfn () {
292 $this->partialStub('Please implement this method.');
296 * Starts the announcement
300 private function startAnnouncement () {
301 // Push the node name on the stacker
302 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
306 * Starts the announcement data
310 private function startAnnouncementData () {
311 // Push the node name on the stacker
312 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
316 * Starts the node status
320 private function startNodeStatus () {
321 // Push the node name on the stacker
322 $this->getStackerInstance()->pushNamed('announcement', 'node-status');
326 * Starts the listener
330 private function startListener () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('announcement', 'listener');
336 * Starts the client listener
340 private function startClientListener () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('announcement', 'client-listener');
346 * Starts the hub listener
350 private function startHubListener () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('announcement', 'hub-listener');
356 * Starts the TCP port
360 private function startTcpPort () {
361 // Push the node name on the stacker
362 $this->getStackerInstance()->pushNamed('announcement', 'tcp-port');
366 * Starts the UDP port
370 private function startUdpPort () {
371 // Push the node name on the stacker
372 $this->getStackerInstance()->pushNamed('announcement', 'udp-port');
376 * Starts the session id
380 private function startSessionId () {
381 // Push the node name on the stacker
382 $this->getStackerInstance()->pushNamed('announcement', 'session-id');
386 * Starts the object type list
390 private function startObjectTypeList () {
391 // Push the node name on the stacker
392 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
396 * Starts the object type
400 private function startObjectType () {
401 // Push the node name on the stacker
402 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
406 * Finishes the object type
410 private function finishObjectType () {
411 // Pop the last entry
412 $this->getStackerInstance()->popNamed('announcement');
416 * Finishes the object type list
420 private function finishObjectTypeList () {
421 // Pop the last entry
422 $this->getStackerInstance()->popNamed('announcement');
426 * Finishes the session id
430 private function finishSessionId () {
431 // Pop the last entry
432 $this->getStackerInstance()->popNamed('announcement');
436 * Finishes the UDP port
440 private function finishUdpPort () {
441 // Pop the last entry
442 $this->getStackerInstance()->popNamed('announcement');
446 * Finishes the TCP port
450 private function finishTcpPort () {
451 // Pop the last entry
452 $this->getStackerInstance()->popNamed('announcement');
456 * Finishes the hub listener
460 private function finishHubListener () {
461 // Pop the last entry
462 $this->getStackerInstance()->popNamed('announcement');
466 * Finishes the client listener
470 private function finishClientListener () {
471 // Pop the last entry
472 $this->getStackerInstance()->popNamed('announcement');
476 * Finishes the listener
480 private function finishListener () {
481 // Pop the last entry
482 $this->getStackerInstance()->popNamed('announcement');
486 * Finishes the node status
490 private function finishNodeStatus () {
491 // Pop the last entry
492 $this->getStackerInstance()->popNamed('announcement');
496 * Finishes the announcement data
500 private function finishAnnouncementData () {
501 // Pop the last entry
502 $this->getStackerInstance()->popNamed('announcement');
506 * Finishes the announcement
510 private function finishAnnouncement () {
511 // Pop the last entry
512 $this->getStackerInstance()->popNamed('announcement');