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(
48 // These nodes don't contain any data
51 // Data from *this* node
58 // Data from other node
62 // Answer status (code)
69 private $curr = array();
72 * Protected constructor
76 protected function __construct () {
77 // Call parent constructor
78 parent::__construct(__CLASS__);
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 createXmlAnnouncementAnswerTemplateEngine () {
94 $templateInstance = new XmlAnnouncementAnswerTemplateEngine();
96 // Get 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_answer_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_answer_stacker_class');
131 $templateInstance->setStackerInstance($stackerInstance);
133 // Return the prepared instance
134 return $templateInstance;
138 * Load a specified announcement-answer template into the engine
140 * @param $template The announcement-answer template we shall load which is
141 * located in 'announcement_answer' by default
144 public function loadAnnouncementAnswerTemplate ($template = 'announcement_answer') {
146 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_answer_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 = 'initAnnouncementAnswer';
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 call_user_func_array(array($this, $methodName), $attributes);
226 * Ends the main or sub node by sending out the gathered data
228 * @param $resource An XML resource pointer (currently ignored)
229 * @param $nodeName Name of the node we want to finish
231 * @throws XmlNodeMismatchException If current main node mismatches the closing one
233 public function endElement ($resource, $nodeName) {
234 // Make all lower-case
235 $nodeName = strtolower($nodeName);
237 // Does this match with current main node?
238 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
239 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
241 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
244 // Construct method name
245 $methodName = 'finish' . $this->convertToClassName($nodeName);
247 // Call the corresponding method
248 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
249 call_user_func_array(array($this, $methodName), array());
255 * @param $resource XML parser resource (currently ignored)
256 * @param $characters Characters to handle
258 * @todo Find something useful with this!
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_answer'), $characters);
278 * Read announcement-answer variables by calling readVariable() with 'general' as
281 * @param $key Key to read from
282 * @return $value Value from variable
284 public function readAnnouncementAnswerData ($key) {
286 $value = parent::readVariable($key, 'general');
293 * Getter for cache file (FQFN)
295 * @return $fqfn Full-qualified file name of the menu cache
297 public function getAnnouncementAnswerCacheFqfn () {
298 $this->partialStub('Please implement this method.');
302 * Starts the announcement-answer
306 private function startAnnouncementAnswer () {
307 // Push the node name on the stacker
308 $this->getStackerInstance()->pushNamed('announcement_answer', 'announcement-answer');
316 private function startMyData () {
317 // Push the node name on the stacker
318 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-data');
322 * Starts the my-external-ip
326 private function startMyExternalIp () {
327 // Push the node name on the stacker
328 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-external-ip');
332 * Starts the my-internal-ip
336 private function startMyInternalIp () {
337 // Push the node name on the stacker
338 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-internal-ip');
342 * Starts the my-tcp-port
346 private function startMyTcpPort () {
347 // Push the node name on the stacker
348 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-tcp-port');
352 * Starts the my-udp-port
356 private function startMyUdpPort () {
357 // Push the node name on the stacker
358 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-udp-port');
362 * Starts the my-session-id
366 private function startMySessionId () {
367 // Push the node name on the stacker
368 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-session-id');
372 * Starts the my-status
376 private function startMyStatus () {
377 // Push the node name on the stacker
378 $this->getStackerInstance()->pushNamed('announcement_answer', 'my-status');
382 * Finishes the my-status
386 private function finishMyStatus () {
387 // Pop the last entry
388 $this->getStackerInstance()->popNamed('announcement_answer');
392 * Finishes the my-session-id
396 private function finishMySessionId () {
397 // Pop the last entry
398 $this->getStackerInstance()->popNamed('announcement_answer');
402 * Finishes the my-udp-port
406 private function finishMyUdpPort () {
407 // Pop the last entry
408 $this->getStackerInstance()->popNamed('announcement_answer');
412 * Finishes the my-tcp-port
416 private function finishMyTcpPort () {
417 // Pop the last entry
418 $this->getStackerInstance()->popNamed('announcement_answer');
422 * Finishes the my-internal-ip
426 private function finishMyInternalIp () {
427 // Pop the last entry
428 $this->getStackerInstance()->popNamed('announcement_answer');
432 * Finishes the my-external-ip
436 private function finishMyExternalIp () {
437 // Pop the last entry
438 $this->getStackerInstance()->popNamed('announcement_answer');
442 * Finishes the my-data
446 private function finishMyData () {
447 // Pop the last entry
448 $this->getStackerInstance()->popNamed('announcement_answer');
452 * Starts the your-data
456 private function startYourData () {
457 // Push the node name on the stacker
458 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-data');
462 * Starts the your-external-ip
466 private function startYourExternalIp () {
467 // Push the node name on the stacker
468 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-external-ip');
472 * Starts the your-internal-ip
476 private function startYourInternalIp () {
477 // Push the node name on the stacker
478 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-internal-ip');
482 * Starts the your-session-id
486 private function startYourSessionId () {
487 // Push the node name on the stacker
488 $this->getStackerInstance()->pushNamed('announcement_answer', 'your-session-id');
492 * Finishes the your-session-id
496 private function finishYourSessionId () {
497 // Pop the last entry
498 $this->getStackerInstance()->popNamed('announcement_answer');
502 * Finishes the your-internal-ip
506 private function finishYourInternalIp () {
507 // Pop the last entry
508 $this->getStackerInstance()->popNamed('announcement_answer');
512 * Finishes the your-external-ip
516 private function finishYourExternalIp () {
517 // Pop the last entry
518 $this->getStackerInstance()->popNamed('announcement_answer');
522 * Finishes the your-data
526 private function finishYourData () {
527 // Pop the last entry
528 $this->getStackerInstance()->popNamed('announcement_answer');
532 * Starts the answer-status
536 private function startAnswerStatus () {
537 // Push the node name on the stacker
538 $this->getStackerInstance()->pushNamed('announcement_answer', 'answer-status');
542 * Finishes the answer-status
546 private function finishAnswerStatus () {
547 // Pop the last entry
548 $this->getStackerInstance()->popNamed('announcement_answer');
552 * Finishes the announcement-answer
556 private function finishAnnouncementAnswer () {
557 // Pop the last entry
558 $this->getStackerInstance()->popNamed('announcement_answer');