3 * An SelfConnect 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 XmlSelfConnectTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
29 const SELF_CONNECT_DATA_NODE_ID = 'node-id';
30 const SELF_CONNECT_DATA_SESSION_ID = 'session-id';
33 * Main nodes in the XML tree
35 private $mainNodes = array(
40 * Sub nodes in the XML tree
42 private $subNodes = array();
47 private $curr = array();
50 * Content from dependency
52 private $dependencyContent = array();
55 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
64 $this->subNodes = array(
66 self::SELF_CONNECT_DATA_NODE_ID,
67 self::SELF_CONNECT_DATA_SESSION_ID
72 * Creates an instance of the class TemplateEngine and prepares it for usage
74 * @return $templateInstance An instance of TemplateEngine
75 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
76 * @throws InvalidBasePathStringException If $templateBasePath is no string
77 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
78 * directory or not found
79 * @throws BasePathReadProtectedException If $templateBasePath is
82 public static final function createXmlSelfConnectTemplateEngine () {
84 $templateInstance = new XmlSelfConnectTemplateEngine();
86 // Get the application instance from registry
87 $applicationInstance = Registry::getRegistry()->getInstance('app');
89 // Determine base path
90 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
92 // Is the base path valid?
93 if (empty($templateBasePath)) {
95 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($templateBasePath)) {
98 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
99 } elseif (!is_dir($templateBasePath)) {
101 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102 } elseif (!is_readable($templateBasePath)) {
104 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
108 $templateInstance->setTemplateBasePath($templateBasePath);
110 // Set template extensions
111 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
112 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
114 // Absolute output path for compiled templates
115 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
117 // Init a variable stacker
118 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_self_connect_stacker_class');
121 $templateInstance->setStackerInstance($stackerInstance);
123 // Return the prepared instance
124 return $templateInstance;
128 * Load a specified self-connect template into the engine
130 * @param $template The self-connect template we shall load which is
131 * located in 'self_connect' by default
134 public function loadSelfConnectTemplate ($template = 'self_connect') {
136 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_self_connect_template_type'));
138 // Load the special template
139 $this->loadTemplate($template);
143 * Getter for current main node
145 * @return $currMainNode Current main node
147 public final function getCurrMainNode () {
148 return $this->curr['main_node'];
152 * Setter for current main node
154 * @param $element Element name to set as current main node
155 * @return $currMainNode Current main node
157 private final function setCurrMainNode ($element) {
158 $this->curr['main_node'] = (string) $element;
162 * Getter for main node array
164 * @return $mainNodes Array with valid main node names
166 public final function getMainNodes () {
167 return $this->mainNodes;
171 * Getter for sub node array
173 * @return $subNodes Array with valid sub node names
175 public final function getSubNodes () {
176 return $this->subNodes;
180 * Handles the start element of an XML resource
182 * @param $resource XML parser resource (currently ignored)
183 * @param $element The element we shall handle
184 * @param $attributes All attributes
186 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
188 public function startElement ($resource, $element, array $attributes) {
189 // Initial method name which will never be called...
190 $methodName = 'initSelfConnect';
192 // Make the element name lower-case
193 $element = strtolower($element);
195 // Is the element a main node?
196 //* DEBUG: */ echo "START: >".$element."<<br />\n";
197 if (in_array($element, $this->getMainNodes())) {
198 // Okay, main node found!
199 $methodName = 'start' . $this->convertToClassName($element);
202 $this->setCurrMainNode($element);
203 } elseif (in_array($element, $this->getSubNodes())) {
205 $methodName = 'start' . $this->convertToClassName($element);
207 // Invalid node name found
208 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
212 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
213 call_user_func_array(array($this, $methodName), $attributes);
217 * Ends the main or sub node by sending out the gathered data
219 * @param $resource An XML resource pointer (currently ignored)
220 * @param $nodeName Name of the node we want to finish
222 * @throws XmlNodeMismatchException If current main node mismatches the closing one
224 public function endElement ($resource, $nodeName) {
225 // Make all lower-case
226 $nodeName = strtolower($nodeName);
228 // Does this match with current main node?
229 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
230 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
232 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
235 // Construct method name
236 $methodName = 'finish' . $this->convertToClassName($nodeName);
238 // Call the corresponding method
239 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
240 call_user_func_array(array($this, $methodName), array());
246 * @param $resource XML parser resource (currently ignored)
247 * @param $characters Characters to handle
249 * @todo Find something useful with this!
251 public function characterHandler ($resource, $characters) {
252 // Trim all spaces away
253 $characters = trim($characters);
255 // Is this string empty?
256 if (empty($characters)) {
257 // Then skip it silently
262 * Assign the found characters to variable and use the last entry from
265 parent::assignVariable($this->getStackerInstance()->getNamed('self_connect'), $characters);
269 * Handles the template dependency for given node
271 * @param $node The node we should load a dependency template
272 * @param $templateDependency A template to load to satisfy dependencies
275 private function handleTemplateDependency ($node, $templateDependency) {
276 // Is the template dependency set?
277 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
278 // Get a temporay template instance
279 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_self_connect_template_class');
282 $templateInstance->loadSelfConnectTemplate($templateDependency);
284 // Parse the XML content
285 $templateInstance->renderXmlContent();
287 // Save the parsed raw content in our dependency array
288 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
293 * Read self-connect variables by calling readVariable() with 'general' as
296 * @param $key Key to read from
297 * @return $value Value from variable
299 public function readSelfConnectData ($key) {
301 $value = parent::readVariable($key, 'general');
308 * Getter for cache file (FQFN)
310 * @return $fqfn Full-qualified file name of the menu cache
312 public function getMenuCacheFqfn () {
313 $this->partialStub('Please implement this method.');
317 * Starts the self_connect
321 private function startSelfConnect () {
322 // Push the node name on the stacker
323 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect');
327 * Starts the self_connect data
331 private function startSelfConnectData () {
332 // Push the node name on the stacker
333 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect-data');
341 private function startNodeId () {
342 // Push the node name on the stacker
343 $this->getStackerInstance()->pushNamed('self_connect', self::SELF_CONNECT_DATA_NODE_ID);
347 * Starts the session id
351 private function startSessionId () {
352 // Push the node name on the stacker
353 $this->getStackerInstance()->pushNamed('self_connect', self::SELF_CONNECT_DATA_SESSION_ID);
357 * Finishes the session id
361 private function finishSessionId () {
362 // Pop the last entry
363 $this->getStackerInstance()->popNamed('self_connect');
367 * Finishes the node id
371 private function finishNodeId () {
372 // Pop the last entry
373 $this->getStackerInstance()->popNamed('self_connect');
377 * Finishes the self_connect data
381 private function finishSelfConnectData () {
382 // Pop the last entry
383 $this->getStackerInstance()->popNamed('self_connect');
387 * Finishes the self_connect
391 private function finishSelfConnect () {
392 // Pop the last entry
393 $this->getStackerInstance()->popNamed('self_connect');