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 * Some XML nodes must be available for later data extraction
29 const ANNOUNCEMENT_DATA_SESSION_ID = 'my-session-id';
30 const ANNOUNCEMENT_DATA_NODE_STATE = 'my-state';
31 const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'my-external-ip';
32 const ANNOUNCEMENT_DATA_INTERNAL_IP = 'my-internal-ip';
33 const ANNOUNCEMENT_DATA_TCP_PORT = 'my-tcp-port';
34 const ANNOUNCEMENT_DATA_UDP_PORT = 'my-udp-port';
37 * Main nodes in the XML tree
39 private $mainNodes = array(
44 * Sub nodes in the XML tree
46 private $subNodes = array(
47 // These nodes don't contain any data
50 // Data from *this* node
57 // Data from other node
61 // Answer status (code)
68 private $curr = array();
71 * Protected constructor
75 protected function __construct () {
76 // Call parent constructor
77 parent::__construct(__CLASS__);
81 * Creates an instance of the class TemplateEngine and prepares it for usage
83 * @return $templateInstance An instance of TemplateEngine
84 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
85 * @throws InvalidBasePathStringException If $templateBasePath is no string
86 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
87 * directory or not found
88 * @throws BasePathReadProtectedException If $templateBasePath is
91 public static final function createXmlAnnouncementAnswerTemplateEngine () {
93 $templateInstance = new XmlAnnouncementAnswerTemplateEngine();
95 // Get application instance from registry
96 $applicationInstance = Registry::getRegistry()->getInstance('app');
98 // Determine base path
99 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
101 // Is the base path valid?
102 if (empty($templateBasePath)) {
103 // Base path is empty
104 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
105 } elseif (!is_string($templateBasePath)) {
107 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
108 } elseif (!is_dir($templateBasePath)) {
110 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
111 } elseif (!is_readable($templateBasePath)) {
113 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
117 $templateInstance->setTemplateBasePath($templateBasePath);
119 // Set template extensions
120 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
121 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_answer_template_extension'));
123 // Absolute output path for compiled templates
124 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
126 // Init a variable stacker
127 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_answer_stacker_class');
130 $templateInstance->setStackerInstance($stackerInstance);
132 // Return the prepared instance
133 return $templateInstance;
137 * Load a specified announcement-answer template into the engine
139 * @param $template The announcement-answer template we shall load which is
140 * located in 'announcement_answer' by default
143 public function loadAnnouncementAnswerTemplate ($template = 'announcement_answer') {
145 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_answer_template_type'));
147 // Load the special template
148 $this->loadTemplate($template);
152 * Getter for current main node
154 * @return $currMainNode Current main node
156 public final function getCurrMainNode () {
157 return $this->curr['main_node'];
161 * Setter for current main node
163 * @param $element Element name to set as current main node
164 * @return $currMainNode Current main node
166 private final function setCurrMainNode ($element) {
167 $this->curr['main_node'] = (string) $element;
171 * Getter for main node array
173 * @return $mainNodes Array with valid main node names
175 public final function getMainNodes () {
176 return $this->mainNodes;
180 * Getter for sub node array
182 * @return $subNodes Array with valid sub node names
184 public final function getSubNodes () {
185 return $this->subNodes;
189 * Handles the start element of an XML resource
191 * @param $resource XML parser resource (currently ignored)
192 * @param $element The element we shall handle
193 * @param $attributes All attributes
195 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
197 public function startElement ($resource, $element, array $attributes) {
198 // Initial method name which will never be called...
199 $methodName = 'initAnnouncementAnswer';
201 // Make the element name lower-case
202 $element = strtolower($element);
204 // Is the element a main node?
205 //* DEBUG: */ echo "START: >".$element."<<br />\n";
206 if (in_array($element, $this->getMainNodes())) {
207 // Okay, main node found!
208 $methodName = 'start' . $this->convertToClassName($element);
211 $this->setCurrMainNode($element);
212 } elseif (in_array($element, $this->getSubNodes())) {
214 $methodName = 'start' . $this->convertToClassName($element);
216 // Invalid node name found
217 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
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
257 * @todo Find something useful with this!
259 public function characterHandler ($resource, $characters) {
260 // Trim all spaces away
261 $characters = trim($characters);
263 // Is this string empty?
264 if (empty($characters)) {
265 // Then skip it silently
270 * Assign the found characters to variable and use the last entry from
273 parent::assignVariable($this->getStackerInstance()->getNamed('announcement_answer'), $characters);
277 * Read announcement-answer variables by calling readVariable() with 'general' as
280 * @param $key Key to read from
281 * @return $value Value from variable
283 public function readAnnouncementAnswerData ($key) {
285 $value = parent::readVariable($key, 'general');
292 * Getter for cache file (FQFN)
294 * @return $fqfn Full-qualified file name of the menu cache
296 public function getAnnouncementAnswerCacheFqfn () {
297 $this->partialStub('Please implement this method.');
301 * Starts the announcement-answer
305 private function startAnnouncementAnswer () {
306 // Push the node name on the stacker
307 $this->getStackerInstance()->pushNamed('announcement_answer', 'announcement-answer');
315 private function startMyData () {
316 // Push the node name on the stacker
317 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-data');
321 * Starts the my-external-ip
325 private function startMyExternalIp () {
326 // Push the node name on the stacker
327 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-external-ip');
331 * Starts the my-internal-ip
335 private function startMyInternalIp () {
336 // Push the node name on the stacker
337 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-internal-ip');
341 * Starts the my-tcp-port
345 private function startMyTcpPort () {
346 // Push the node name on the stacker
347 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-tcp-port');
351 * Starts the my-udp-port
355 private function startMyUdpPort () {
356 // Push the node name on the stacker
357 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-udp-port');
361 * Starts the my-session-id
365 private function startMySessionId () {
366 // Push the node name on the stacker
367 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-session-id');
371 * Starts the my-state
375 private function startMyState () {
376 // Push the node name on the stacker
377 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-state');
381 * Finishes the my-state
385 private function finishMyState () {
386 // Pop the last entry
387 $this->getStackerInstance()->popNamed('announcement_answer');
391 * Finishes the my-session-id
395 private function finishMySessionId () {
396 // Pop the last entry
397 $this->getStackerInstance()->popNamed('announcement_answer');
401 * Finishes the my-udp-port
405 private function finishMyUdpPort () {
406 // Pop the last entry
407 $this->getStackerInstance()->popNamed('announcement_answer');
411 * Finishes the my-tcp-port
415 private function finishMyTcpPort () {
416 // Pop the last entry
417 $this->getStackerInstance()->popNamed('announcement_answer');
421 * Finishes the my-internal-ip
425 private function finishMyInternalIp () {
426 // Pop the last entry
427 $this->getStackerInstance()->popNamed('announcement_answer');
431 * Finishes the my-external-ip
435 private function finishMyExternalIp () {
436 // Pop the last entry
437 $this->getStackerInstance()->popNamed('announcement_answer');
441 * Finishes the my-data
445 private function finishMyData () {
446 // Pop the last entry
447 $this->getStackerInstance()->popNamed('announcement_answer');
451 * Starts the your-data
455 private function startYourData () {
456 // Push the node name on the stacker
457 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-data');
461 * Starts the your-external-ip
465 private function startYourExternalIp () {
466 // Push the node name on the stacker
467 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-external-ip');
471 * Starts the your-internal-ip
475 private function startYourInternalIp () {
476 // Push the node name on the stacker
477 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-internal-ip');
481 * Starts the your-session-id
485 private function startYourSessionId () {
486 // Push the node name on the stacker
487 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-session-id');
491 * Finishes the your-session-id
495 private function finishYourSessionId () {
496 // Pop the last entry
497 $this->getStackerInstance()->popNamed('announcement_answer');
501 * Finishes the your-internal-ip
505 private function finishYourInternalIp () {
506 // Pop the last entry
507 $this->getStackerInstance()->popNamed('announcement_answer');
511 * Finishes the your-external-ip
515 private function finishYourExternalIp () {
516 // Pop the last entry
517 $this->getStackerInstance()->popNamed('announcement_answer');
521 * Finishes the your-data
525 private function finishYourData () {
526 // Pop the last entry
527 $this->getStackerInstance()->popNamed('announcement_answer');
531 * Starts the answer-status
535 private function startAnswerStatus () {
536 // Push the node name on the stacker
537 $this->getStackerInstance()->pushNamed('announcement_answer', 'answer-status');
541 * Finishes the answer-status
545 private function finishAnswerStatus () {
546 // Pop the last entry
547 $this->getStackerInstance()->popNamed('announcement_answer');
551 * Finishes the announcement-answer
555 private function finishAnnouncementAnswer () {
556 // Pop the last entry
557 $this->getStackerInstance()->popNamed('announcement_answer');