3 * A Announcement template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class AnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('menu' is ignored)
28 private $mainNodes = array(
33 * Sub nodes in the XML tree
35 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 * @param $appInstance A manageable application
72 * @return $tplInstance An instance of TemplateEngine
73 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
74 * @throws InvalidBasePathStringException If $templateBasePath is no string
75 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
76 * directory or not found
77 * @throws BasePathReadProtectedException If $templateBasePath is
80 public final static function createAnnouncementTemplateEngine (ManageableApplication $appInstance) {
82 $tplInstance = new AnnouncementTemplateEngine();
84 // Get language and file I/O instances from application
85 $langInstance = $appInstance->getLanguageInstance();
86 $ioInstance = $appInstance->getFileIoInstance();
88 // Determine base path
89 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
91 // Is the base path valid?
92 if (empty($templateBasePath)) {
94 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
95 } elseif (!is_string($templateBasePath)) {
97 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
98 } elseif (!is_dir($templateBasePath)) {
100 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
101 } elseif (!is_readable($templateBasePath)) {
103 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
106 // Get configuration instance
107 $configInstance = FrameworkConfiguration::getInstance();
110 $tplInstance->setTemplateBasePath($templateBasePath);
112 // Set the language and IO instances
113 $tplInstance->setLanguageInstance($langInstance);
114 $tplInstance->setFileIoInstance($ioInstance);
116 // Set template extensions
117 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
118 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('announcement_template_extension'));
120 // Absolute output path for compiled templates
121 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
123 // Init a variable stacker
124 $stackerInstance = ObjectFactory::createObjectByConfiguredName('announcement_stacker_class');
127 $tplInstance->setStackerInstance($stackerInstance);
129 // Return the prepared instance
134 * Load a specified announcement template into the engine
136 * @param $template The announcement template we shall load which is
137 * located in 'announcement' by default
140 public function loadAnnouncementTemplate ($template) {
142 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('announcement_template_type'));
144 // Load the special template
145 $this->loadTemplate($template);
149 * Getter for current main node
151 * @return $currMainNode Current main node
153 public final function getCurrMainNode () {
154 return $this->curr['main_node'];
158 * Setter for current main node
160 * @param $element Element name to set as current main node
161 * @return $currMainNode Current main node
163 private final function setCurrMainNode ($element) {
164 $this->curr['main_node'] = (string) $element;
168 * Getter for main node array
170 * @return $mainNodes Array with valid main node names
172 public final function getMainNodes () {
173 return $this->mainNodes;
177 * Getter for sub node array
179 * @return $subNodes Array with valid sub node names
181 public final function getSubNodes () {
182 return $this->subNodes;
186 * Handles the start element of an XML resource
188 * @param $resource XML parser resource (currently ignored)
189 * @param $element The element we shall handle
190 * @param $attributes All attributes
192 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
194 public function startElement ($resource, $element, array $attributes) {
195 // Initial method name which will never be called...
196 $methodName = 'initMenu';
198 // Make the element name lower-case
199 $element = strtolower($element);
201 // Is the element a main node?
202 //* DEBUG: */ echo "START: >".$element."<<br />\n";
203 if (in_array($element, $this->getMainNodes())) {
204 // Okay, main node found!
205 $methodName = 'start' . $this->convertToClassName($element);
208 $this->setCurrMainNode($element);
209 } elseif (in_array($element, $this->getSubNodes())) {
211 $methodName = 'start' . $this->convertToClassName($element);
212 } elseif ($element != 'menu') {
213 // Invalid node name found
214 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
218 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
219 call_user_func_array(array($this, $methodName), $attributes);
223 * Ends the main or sub node by sending out the gathered data
225 * @param $resource An XML resource pointer (currently ignored)
226 * @param $nodeName Name of the node we want to finish
228 * @throws XmlNodeMismatchException If current main node mismatches the closing one
230 public function endElement ($resource, $nodeName) {
231 // Make all lower-case
232 $nodeName = strtolower($nodeName);
234 // Does this match with current main node?
235 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
236 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
238 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
241 // Construct method name
242 $methodName = 'finish' . $this->convertToClassName($nodeName);
244 // Call the corresponding method
245 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
246 call_user_func_array(array($this, $methodName), array());
252 * @param $resource XML parser resource (currently ignored)
253 * @param $characters Characters to handle
255 * @todo Find something useful with this!
257 public function characterHandler ($resource, $characters) {
258 // Trim all spaces away
259 $characters = trim($characters);
261 // Is this string empty?
262 if (empty($characters)) {
263 // Then skip it silently
267 // Assign the found characters to variable and use the last entry from
269 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
273 * Handles the template dependency for given node
275 * @param $node The node we should load a dependency template
276 * @param $templateDependency A template to load to satisfy dependencies
279 private function handleTemplateDependency ($node, $templateDependency) {
280 // Is the template dependency set?
281 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
282 // Get a temporay menu template instance
283 $templateInstance = ObjectFactory::createObjectByConfiguredName('announcement_template_class', array($this->getApplicationInstance()));
286 $templateInstance->loadAnnouncementTemplate($templateDependency);
288 // Get an XmlParser instance
289 $templateInstance->renderXmlContent();
291 // Parse the template's content contents
292 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
297 * Getter for cache file (FQFN)
299 * @return $fqfn Full-qualified file name of the menu cache
301 public function getMenuCacheFqfn () {
302 // Get the FQFN ready
303 $fqfn = sprintf("%s%s%s/%s.%s",
304 $this->getConfigInstance()->getConfigEntry('base_path'),
305 $this->getGenericBasePath(),
308 $this->getMenuInstance()->getMenuName() . ':' .
309 $this->__toString() . ':' .
310 $this->getMenuInstance()->__toString()
312 $this->getMenuInstance()->getMenuType()
320 * Starts the announcement
324 private function startAnnouncement () {
325 // Push the node name on the stacker
326 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
330 * Starts the announcement data
334 private function startAnnouncementData () {
335 // Push the node name on the stacker
336 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
344 private function startNodeId () {
345 // Push the node name on the stacker
346 $this->getStackerInstance()->pushNamed('announcement', 'node-id');
350 * Starts the node status
354 private function startNodeStatus () {
355 // Push the node name on the stacker
356 $this->getStackerInstance()->pushNamed('announcement', 'node-status');
360 * Starts the listener
364 private function startListener () {
365 // Push the node name on the stacker
366 $this->getStackerInstance()->pushNamed('announcement', 'listener');
370 * Starts the client listener
374 private function startClientListener () {
375 // Push the node name on the stacker
376 $this->getStackerInstance()->pushNamed('announcement', 'client-listener');
380 * Starts the hub listener
384 private function startHubListener () {
385 // Push the node name on the stacker
386 $this->getStackerInstance()->pushNamed('announcement', 'hub-listener');
390 * Starts the TCP port
394 private function startTcpPort () {
395 // Push the node name on the stacker
396 $this->getStackerInstance()->pushNamed('announcement', 'tcp-port');
400 * Starts the UDP port
404 private function startUdpPort () {
405 // Push the node name on the stacker
406 $this->getStackerInstance()->pushNamed('announcement', 'udp-port');
410 * Starts the session id
414 private function startSessionId () {
415 // Push the node name on the stacker
416 $this->getStackerInstance()->pushNamed('announcement', 'session-id');
420 * Starts the object type list
424 private function startObjectTypeList () {
425 // Push the node name on the stacker
426 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
430 * Starts the object type
434 private function startObjectType () {
435 // Push the node name on the stacker
436 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
440 * Finishes the object type
444 private function finishObjectType () {
445 // Pop the last entry
446 $this->getStackerInstance()->popNamed('announcement');
450 * Finishes the object type list
454 private function finishObjectTypeList () {
455 // Pop the last entry
456 $this->getStackerInstance()->popNamed('announcement');
460 * Finishes the session id
464 private function finishSessionId () {
465 // Pop the last entry
466 $this->getStackerInstance()->popNamed('announcement');
470 * Finishes the UDP port
474 private function finishUdpPort () {
475 // Pop the last entry
476 $this->getStackerInstance()->popNamed('announcement');
480 * Finishes the TCP port
484 private function finishTcpPort () {
485 // Pop the last entry
486 $this->getStackerInstance()->popNamed('announcement');
490 * Finishes the hub listener
494 private function finishHubListener () {
495 // Pop the last entry
496 $this->getStackerInstance()->popNamed('announcement');
500 * Finishes the client listener
504 private function finishClientListener () {
505 // Pop the last entry
506 $this->getStackerInstance()->popNamed('announcement');
510 * Finishes the listener
514 private function finishListener () {
515 // Pop the last entry
516 $this->getStackerInstance()->popNamed('announcement');
520 * Finishes the node status
524 private function finishNodeStatus () {
525 // Pop the last entry
526 $this->getStackerInstance()->popNamed('announcement');
530 * Finishes the node id
534 private function finishNodeId () {
535 // Pop the last entry
536 $this->getStackerInstance()->popNamed('announcement');
540 * Finishes the announcement data
544 private function finishAnnouncementData () {
545 // Pop the last entry
546 $this->getStackerInstance()->popNamed('announcement');
550 * Finishes the announcement
554 private function finishAnnouncement () {
555 // Pop the last entry
556 $this->getStackerInstance()->popNamed('announcement');