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 XmlRequestNodeListTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
26 // Constants for array elements
27 const REQUEST_DATA_ACCEPTED_OBJECT_TYPES = 'accepted-object-types';
28 const REQUEST_DATA_SESSION_ID = 'session-id';
31 * Main nodes in the XML tree
33 private $mainNodes = array(
34 'node-request-node-list'
38 * Sub nodes in the XML tree
40 private $subNodes = array(
41 self::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
42 self::REQUEST_DATA_SESSION_ID,
48 private $curr = array();
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Creates an instance of the class TemplateEngine and prepares it for usage
63 * @return $templateInstance An instance of TemplateEngine
64 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
65 * @throws InvalidBasePathStringException If $templateBasePath is no string
66 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
67 * directory or not found
68 * @throws BasePathReadProtectedException If $templateBasePath is
71 public static final function createXmlRequestNodeListTemplateEngine () {
73 $templateInstance = new XmlRequestNodeListTemplateEngine();
75 // Get application instance from registry
76 $applicationInstance = Registry::getRegistry()->getInstance('app');
78 // Determine base path
79 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
81 // Is the base path valid?
82 if (empty($templateBasePath)) {
84 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
85 } elseif (!is_string($templateBasePath)) {
87 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
88 } elseif (!is_dir($templateBasePath)) {
90 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
91 } elseif (!is_readable($templateBasePath)) {
93 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
97 $templateInstance->setTemplateBasePath($templateBasePath);
99 // Set template extensions
100 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
101 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
103 // Absolute output path for compiled templates
104 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
106 // Init a variable stacker
107 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_request_node_list_stacker_class');
110 $templateInstance->setStackerInstance($stackerInstance);
112 // Return the prepared instance
113 return $templateInstance;
117 * Load a specified request_node_list template into the engine
119 * @param $template The request_node_list template we shall load which is
120 * located in 'node_request_node_list' by default
123 public function loadRequestNodeListTemplate ($template = 'node_request_node_list') {
125 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_request_node_list_template_type'));
127 // Load the special template
128 $this->loadTemplate($template);
132 * Getter for current main node
134 * @return $currMainNode Current main node
136 public final function getCurrMainNode () {
137 return $this->curr['main_node'];
141 * Setter for current main node
143 * @param $element Element name to set as current main node
144 * @return $currMainNode Current main node
146 private final function setCurrMainNode ($element) {
147 $this->curr['main_node'] = (string) $element;
151 * Getter for main node array
153 * @return $mainNodes Array with valid main node names
155 public final function getMainNodes () {
156 return $this->mainNodes;
160 * Getter for sub node array
162 * @return $subNodes Array with valid sub node names
164 public final function getSubNodes () {
165 return $this->subNodes;
169 * Handles the start element of an XML resource
171 * @param $resource XML parser resource (currently ignored)
172 * @param $element The element we shall handle
173 * @param $attributes All attributes
175 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
177 public function startElement ($resource, $element, array $attributes) {
178 // Initial method name which will never be called...
179 $methodName = 'initNodeRequestNodeList';
181 // Make the element name lower-case
182 $element = strtolower($element);
184 // Is the element a main node?
185 //* DEBUG: */ echo "START: >".$element."<<br />\n";
186 if (in_array($element, $this->getMainNodes())) {
187 // Okay, main node found!
188 $methodName = 'start' . $this->convertToClassName($element);
191 $this->setCurrMainNode($element);
192 } elseif (in_array($element, $this->getSubNodes())) {
194 $methodName = 'start' . $this->convertToClassName($element);
196 // Invalid node name found
197 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
201 call_user_func_array(array($this, $methodName), $attributes);
205 * Ends the main or sub node by sending out the gathered data
207 * @param $resource An XML resource pointer (currently ignored)
208 * @param $nodeName Name of the node we want to finish
210 * @throws XmlNodeMismatchException If current main node mismatches the closing one
212 public function endElement ($resource, $nodeName) {
213 // Make all lower-case
214 $nodeName = strtolower($nodeName);
216 // Does this match with current main node?
217 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
218 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
220 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
223 // Construct method name
224 $methodName = 'finish' . $this->convertToClassName($nodeName);
226 // Call the corresponding method
227 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
228 call_user_func_array(array($this, $methodName), array());
234 * @param $resource XML parser resource (currently ignored)
235 * @param $characters Characters to handle
237 * @todo Find something useful with this!
239 public function characterHandler ($resource, $characters) {
240 // Trim all spaces away
241 $characters = trim($characters);
243 // Is this string empty?
244 if (empty($characters)) {
245 // Then skip it silently
249 // Assign data with matching variable here
250 parent::assignVariable($this->getStackerInstance()->getNamed('node_request_node_list'), $characters);
254 * Getter for cache file (FQFN)
256 * @return $fqfn Full-qualified file name of the menu cache
258 public function getNodeRequestNodeListCacheFqfn () {
259 $this->partialStub('Please implement this method.');
263 * Starts the node-request-node-list
267 private function startNodeRequestNodeList () {
268 // Push the node name on the stacker
269 $this->getStackerInstance()->pushNamed('node_request_node_list', 'node-request-node-list');
273 * Starts the accepted-object-types
277 private function startAcceptedObjectTypes () {
278 // Push the node name on the stacker
279 $this->getStackerInstance()->pushNamed('node_request_node_list', self::REQUEST_DATA_ACCEPTED_OBJECT_TYPES);
283 * Starts the session-id
287 private function startSessionId () {
288 // Push the node name on the stacker
289 $this->getStackerInstance()->pushNamed('node_request_node_list', self::REQUEST_DATA_SESSION_ID);
293 * Finishes the session-id
297 private function finishSessionId () {
298 // Pop the last entry
299 $this->getStackerInstance()->popNamed('node_request_node_list');
303 * Finishes the accepted-object-types
307 private function finishAcceptedObjectTypes () {
308 // Pop the last entry
309 $this->getStackerInstance()->popNamed('node_request_node_list');
313 * Finishes the node-request-node-list
317 private function finishNodeRequestNodeList () {
318 // Pop the last entry
319 $this->getStackerInstance()->popNamed('node_request_node_list');