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_STATUS = 'my-status';
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';
35 const ANNOUNCEMENT_DATA_ANSWER_STATUS = 'answer-status';
38 * Main nodes in the XML tree
40 private $mainNodes = array(
45 * Sub nodes in the XML tree
47 private $subNodes = array(
53 private $curr = array();
56 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
64 // Init sub-nodes array
65 $this->subNodes = array(
66 // These nodes don't contain any data
69 // Data from *this* node
70 self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
71 self::ANNOUNCEMENT_DATA_INTERNAL_IP,
72 self::ANNOUNCEMENT_DATA_TCP_PORT,
73 self::ANNOUNCEMENT_DATA_UDP_PORT,
74 self::ANNOUNCEMENT_DATA_NODE_STATUS,
75 self::ANNOUNCEMENT_DATA_NODE_STATUS,
76 // Data from other node
80 // Answer status (code)
81 self::ANNOUNCEMENT_DATA_ANSWER_STATUS
86 * Creates an instance of the class TemplateEngine and prepares it for usage
88 * @return $templateInstance An instance of TemplateEngine
89 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
90 * @throws InvalidBasePathStringException If $templateBasePath is no string
91 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
92 * directory or not found
93 * @throws BasePathReadProtectedException If $templateBasePath is
96 public static final function createXmlAnnouncementAnswerTemplateEngine () {
98 $templateInstance = new XmlAnnouncementAnswerTemplateEngine();
100 // Get application instance from registry
101 $applicationInstance = Registry::getRegistry()->getInstance('app');
103 // Determine base path
104 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
106 // Is the base path valid?
107 if (empty($templateBasePath)) {
108 // Base path is empty
109 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
110 } elseif (!is_string($templateBasePath)) {
112 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
113 } elseif (!is_dir($templateBasePath)) {
115 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
116 } elseif (!is_readable($templateBasePath)) {
118 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
122 $templateInstance->setTemplateBasePath($templateBasePath);
124 // Set template extensions
125 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
126 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_answer_template_extension'));
128 // Absolute output path for compiled templates
129 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
131 // Init a variable stacker
132 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_answer_stacker_class');
135 $templateInstance->setStackerInstance($stackerInstance);
137 // Return the prepared instance
138 return $templateInstance;
142 * Load a specified announcement-answer template into the engine
144 * @param $template The announcement-answer template we shall load which is
145 * located in 'announcement_answer' by default
148 public function loadAnnouncementAnswerTemplate ($template = 'announcement_answer') {
150 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_answer_template_type'));
152 // Load the special template
153 $this->loadTemplate($template);
157 * Getter for current main node
159 * @return $currMainNode Current main node
161 public final function getCurrMainNode () {
162 return $this->curr['main_node'];
166 * Setter for current main node
168 * @param $element Element name to set as current main node
169 * @return $currMainNode Current main node
171 private final function setCurrMainNode ($element) {
172 $this->curr['main_node'] = (string) $element;
176 * Getter for main node array
178 * @return $mainNodes Array with valid main node names
180 public final function getMainNodes () {
181 return $this->mainNodes;
185 * Getter for sub node array
187 * @return $subNodes Array with valid sub node names
189 public final function getSubNodes () {
190 return $this->subNodes;
194 * Handles the start element of an XML resource
196 * @param $resource XML parser resource (currently ignored)
197 * @param $element The element we shall handle
198 * @param $attributes All attributes
200 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
202 public function startElement ($resource, $element, array $attributes) {
203 // Initial method name which will never be called...
204 $methodName = 'initAnnouncementAnswer';
206 // Make the element name lower-case
207 $element = strtolower($element);
209 // Is the element a main node?
210 //* DEBUG: */ echo "START: >".$element."<<br />\n";
211 if (in_array($element, $this->getMainNodes())) {
212 // Okay, main node found!
213 $methodName = 'start' . $this->convertToClassName($element);
216 $this->setCurrMainNode($element);
217 } elseif (in_array($element, $this->getSubNodes())) {
219 $methodName = 'start' . $this->convertToClassName($element);
221 // Invalid node name found
222 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
226 call_user_func_array(array($this, $methodName), $attributes);
230 * Ends the main or sub node by sending out the gathered data
232 * @param $resource An XML resource pointer (currently ignored)
233 * @param $nodeName Name of the node we want to finish
235 * @throws XmlNodeMismatchException If current main node mismatches the closing one
237 public function endElement ($resource, $nodeName) {
238 // Make all lower-case
239 $nodeName = strtolower($nodeName);
241 // Does this match with current main node?
242 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
243 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
245 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
248 // Construct method name
249 $methodName = 'finish' . $this->convertToClassName($nodeName);
251 // Call the corresponding method
252 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
253 call_user_func_array(array($this, $methodName), array());
259 * @param $resource XML parser resource (currently ignored)
260 * @param $characters Characters to handle
262 * @todo Find something useful with this!
264 public function characterHandler ($resource, $characters) {
265 // Trim all spaces away
266 $characters = trim($characters);
268 // Is this string empty?
269 if (empty($characters)) {
270 // Then skip it silently
275 * Assign the found characters to variable and use the last entry from
278 parent::assignVariable($this->getStackerInstance()->getNamed('announcement_answer'), $characters);
282 * Read announcement-answer variables by calling readVariable() with 'general' as
285 * @param $key Key to read from
286 * @return $value Value from variable
288 public function readAnnouncementAnswerData ($key) {
290 $value = parent::readVariable($key, 'general');
297 * Getter for cache file (FQFN)
299 * @return $fqfn Full-qualified file name of the menu cache
301 public function getAnnouncementAnswerCacheFqfn () {
302 $this->partialStub('Please implement this method.');
306 * Starts the announcement-answer
310 private function startAnnouncementAnswer () {
311 // Push the node name on the stacker
312 $this->getStackerInstance()->pushNamed('announcement_answer', 'announcement-answer');
320 private function startMyData () {
321 // Push the node name on the stacker
322 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-data');
326 * Starts the my-external-ip
330 private function startMyExternalIp () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
336 * Starts the my-internal-ip
340 private function startMyInternalIp () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
346 * Starts the my-tcp-port
350 private function startMyTcpPort () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_TCP_PORT);
356 * Starts the my-udp-port
360 private function startMyUdpPort () {
361 // Push the node name on the stacker
362 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_UDP_PORT);
366 * Starts the my-session-id
370 private function startMySessionId () {
371 // Push the node name on the stacker
372 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_SESSION_ID);
376 * Starts the my-status
380 private function startMyStatus () {
381 // Push the node name on the stacker
382 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_NODE_STATUS);
386 * Finishes the my-status
390 private function finishMyStatus () {
391 // Pop the last entry
392 $this->getStackerInstance()->popNamed('announcement_answer');
396 * Finishes the my-session-id
400 private function finishMySessionId () {
401 // Pop the last entry
402 $this->getStackerInstance()->popNamed('announcement_answer');
406 * Finishes the my-udp-port
410 private function finishMyUdpPort () {
411 // Pop the last entry
412 $this->getStackerInstance()->popNamed('announcement_answer');
416 * Finishes the my-tcp-port
420 private function finishMyTcpPort () {
421 // Pop the last entry
422 $this->getStackerInstance()->popNamed('announcement_answer');
426 * Finishes the my-internal-ip
430 private function finishMyInternalIp () {
431 // Pop the last entry
432 $this->getStackerInstance()->popNamed('announcement_answer');
436 * Finishes the my-external-ip
440 private function finishMyExternalIp () {
441 // Pop the last entry
442 $this->getStackerInstance()->popNamed('announcement_answer');
446 * Finishes the my-data
450 private function finishMyData () {
451 // Pop the last entry
452 $this->getStackerInstance()->popNamed('announcement_answer');
456 * Starts the your-data
460 private function startYourData () {
461 // Push the node name on the stacker
462 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-data');
466 * Starts the your-external-ip
470 private function startYourExternalIp () {
471 // Push the node name on the stacker
472 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-external-ip');
476 * Starts the your-internal-ip
480 private function startYourInternalIp () {
481 // Push the node name on the stacker
482 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-internal-ip');
486 * Starts the your-session-id
490 private function startYourSessionId () {
491 // Push the node name on the stacker
492 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-session-id');
496 * Finishes the your-session-id
500 private function finishYourSessionId () {
501 // Pop the last entry
502 $this->getStackerInstance()->popNamed('announcement_answer');
506 * Finishes the your-internal-ip
510 private function finishYourInternalIp () {
511 // Pop the last entry
512 $this->getStackerInstance()->popNamed('announcement_answer');
516 * Finishes the your-external-ip
520 private function finishYourExternalIp () {
521 // Pop the last entry
522 $this->getStackerInstance()->popNamed('announcement_answer');
526 * Finishes the your-data
530 private function finishYourData () {
531 // Pop the last entry
532 $this->getStackerInstance()->popNamed('announcement_answer');
536 * Starts the answer-status
540 private function startAnswerStatus () {
541 // Push the node name on the stacker
542 $this->getStackerInstance()->pushNamed('announcement_answer', self::ANNOUNCEMENT_DATA_ANSWER_STATUS);
546 * Finishes the answer-status
550 private function finishAnswerStatus () {
551 // Pop the last entry
552 $this->getStackerInstance()->popNamed('announcement_answer');
556 * Finishes the announcement-answer
560 private function finishAnnouncementAnswer () {
561 // Pop the last entry
562 $this->getStackerInstance()->popNamed('announcement_answer');