3 * An ??? 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 XmlAnnouncementAnswerTemplateEngine 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(
37 // These nodes don't contain any data
40 // Data from *this* node
47 // Data from other node
51 // Answer status (code)
58 private $curr = array();
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
71 * Creates an instance of the class TemplateEngine and prepares it for usage
73 * @return $templateInstance An instance of TemplateEngine
74 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
75 * @throws InvalidBasePathStringException If $templateBasePath is no string
76 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
77 * directory or not found
78 * @throws BasePathReadProtectedException If $templateBasePath is
81 public static final function createXmlAnnouncementAnswerTemplateEngine () {
83 $templateInstance = new XmlAnnouncementAnswerTemplateEngine();
85 // Get application instance from registry
86 $applicationInstance = Registry::getRegistry()->getInstance('app');
88 // Determine base path
89 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
91 // Is the base path valid?
92 if (empty($templateBasePath)) {
94 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
95 } elseif (!is_string($templateBasePath)) {
97 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
98 } elseif (!is_dir($templateBasePath)) {
100 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
101 } elseif (!is_readable($templateBasePath)) {
103 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
107 $templateInstance->setTemplateBasePath($templateBasePath);
109 // Set template extensions
110 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
111 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_answer_template_extension'));
113 // Absolute output path for compiled templates
114 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
116 // Init a variable stacker
117 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_answer_stacker_class');
120 $templateInstance->setStackerInstance($stackerInstance);
122 // Return the prepared instance
123 return $templateInstance;
127 * Load a specified announcement-answer template into the engine
129 * @param $template The announcement-answer template we shall load which is
130 * located in 'announcement_answer' by default
133 public function loadAnnouncementAnswerTemplate ($template = 'announcement_answer') {
135 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_answer_template_type'));
137 // Load the special template
138 $this->loadTemplate($template);
142 * Getter for current main node
144 * @return $currMainNode Current main node
146 public final function getCurrMainNode () {
147 return $this->curr['main_node'];
151 * Setter for current main node
153 * @param $element Element name to set as current main node
154 * @return $currMainNode Current main node
156 private final function setCurrMainNode ($element) {
157 $this->curr['main_node'] = (string) $element;
161 * Getter for main node array
163 * @return $mainNodes Array with valid main node names
165 public final function getMainNodes () {
166 return $this->mainNodes;
170 * Getter for sub node array
172 * @return $subNodes Array with valid sub node names
174 public final function getSubNodes () {
175 return $this->subNodes;
179 * Handles the start element of an XML resource
181 * @param $resource XML parser resource (currently ignored)
182 * @param $element The element we shall handle
183 * @param $attributes All attributes
185 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
187 public function startElement ($resource, $element, array $attributes) {
188 // Initial method name which will never be called...
189 $methodName = 'initAnnouncementAnswer';
191 // Make the element name lower-case
192 $element = strtolower($element);
194 // Is the element a main node?
195 //* DEBUG: */ echo "START: >".$element."<<br />\n";
196 if (in_array($element, $this->getMainNodes())) {
197 // Okay, main node found!
198 $methodName = 'start' . $this->convertToClassName($element);
201 $this->setCurrMainNode($element);
202 } elseif (in_array($element, $this->getSubNodes())) {
204 $methodName = 'start' . $this->convertToClassName($element);
206 // Invalid node name found
207 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
211 call_user_func_array(array($this, $methodName), $attributes);
215 * Ends the main or sub node by sending out the gathered data
217 * @param $resource An XML resource pointer (currently ignored)
218 * @param $nodeName Name of the node we want to finish
220 * @throws XmlNodeMismatchException If current main node mismatches the closing one
222 public function endElement ($resource, $nodeName) {
223 // Make all lower-case
224 $nodeName = strtolower($nodeName);
226 // Does this match with current main node?
227 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
228 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
230 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
233 // Construct method name
234 $methodName = 'finish' . $this->convertToClassName($nodeName);
236 // Call the corresponding method
237 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
238 call_user_func_array(array($this, $methodName), array());
244 * @param $resource XML parser resource (currently ignored)
245 * @param $characters Characters to handle
247 * @todo Find something useful with this!
249 public function characterHandler ($resource, $characters) {
250 // Trim all spaces away
251 $characters = trim($characters);
253 // Is this string empty?
254 if (empty($characters)) {
255 // Then skip it silently
260 * Assign the found characters to variable and use the last entry from
263 parent::assignVariable($this->getStackerInstance()->getNamed('announcement_answer'), $characters);
267 * Getter for cache file (FQFN)
269 * @return $fqfn Full-qualified file name of the menu cache
271 public function getAnnouncementAnswerCacheFqfn () {
272 $this->partialStub('Please implement this method.');
276 * Starts the announcement-answer
280 private function startAnnouncementAnswer () {
281 // Push the node name on the stacker
282 $this->getStackerInstance()->pushNamed('announcement_answer', 'announcement-answer');
286 * Starts the node-data
290 private function startNodeData () {
291 // Push the node name on the stacker
292 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-data');
296 * Starts the node-external-ip
300 private function startNodeExternalIp () {
301 // Push the node name on the stacker
302 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-external-ip');
306 * Starts the node-internal-ip
310 private function startNodeInternalIp () {
311 // Push the node name on the stacker
312 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-internal-ip');
316 * Starts the node-tcp-port
320 private function startNodeTcpPort () {
321 // Push the node name on the stacker
322 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-tcp-port');
326 * Starts the node-udp-port
330 private function startNodeUdpPort () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-udp-port');
336 * Starts the node-session-id
340 private function startNodeSessionId () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-session-id');
346 * Starts the node-status
350 private function startNodeStatus () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('announcement_answer', 'node-status');
356 * Finishes the node-status
360 private function finishNodeStatus () {
361 // Pop the last entry
362 $this->getStackerInstance()->popNamed('announcement_answer');
366 * Finishes the node-session-id
370 private function finishNodeSessionId () {
371 // Pop the last entry
372 $this->getStackerInstance()->popNamed('announcement_answer');
376 * Finishes the node-udp-port
380 private function finishNodeUdpPort () {
381 // Pop the last entry
382 $this->getStackerInstance()->popNamed('announcement_answer');
386 * Finishes the node-tcp-port
390 private function finishNodeTcpPort () {
391 // Pop the last entry
392 $this->getStackerInstance()->popNamed('announcement_answer');
396 * Finishes the node-internal-ip
400 private function finishNodeInternalIp () {
401 // Pop the last entry
402 $this->getStackerInstance()->popNamed('announcement_answer');
406 * Finishes the node-external-ip
410 private function finishNodeExternalIp () {
411 // Pop the last entry
412 $this->getStackerInstance()->popNamed('announcement_answer');
416 * Finishes the node-data
420 private function finishNodeData () {
421 // Pop the last entry
422 $this->getStackerInstance()->popNamed('announcement_answer');
426 * Starts the other-data
430 private function startOtherData () {
431 // Push the node name on the stacker
432 $this->getStackerInstance()->pushNamed('announcement_answer', 'other-data');
436 * Starts the other-external-ip
440 private function startOtherExternalIp () {
441 // Push the node name on the stacker
442 $this->getStackerInstance()->pushNamed('announcement_answer', 'other-external-ip');
446 * Starts the other-internal-ip
450 private function startOtherInternalIp () {
451 // Push the node name on the stacker
452 $this->getStackerInstance()->pushNamed('announcement_answer', 'other-internal-ip');
456 * Starts the other-session-id
460 private function startOtherSessionId () {
461 // Push the node name on the stacker
462 $this->getStackerInstance()->pushNamed('announcement_answer', 'other-session-id');
466 * Finishes the other-session-id
470 private function finishOtherSessionId () {
471 // Pop the last entry
472 $this->getStackerInstance()->popNamed('announcement_answer');
476 * Finishes the other-internal-ip
480 private function finishOtherInternalIp () {
481 // Pop the last entry
482 $this->getStackerInstance()->popNamed('announcement_answer');
486 * Finishes the other-external-ip
490 private function finishOtherExternalIp () {
491 // Pop the last entry
492 $this->getStackerInstance()->popNamed('announcement_answer');
496 * Finishes the other-data
500 private function finishOtherData () {
501 // Pop the last entry
502 $this->getStackerInstance()->popNamed('announcement_answer');
506 * Starts the answer-status
510 private function startAnswerStatus () {
511 // Push the node name on the stacker
512 $this->getStackerInstance()->pushNamed('announcement_answer', 'answer-status');
516 * Finishes the answer-status
520 private function finishAnswerStatus () {
521 // Pop the last entry
522 $this->getStackerInstance()->popNamed('announcement_answer');
526 * Finishes the announcement-answer
530 private function finishAnnouncementAnswer () {
531 // Pop the last entry
532 $this->getStackerInstance()->popNamed('announcement_answer');