3 * A generic XML template engine class
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 BaseXMLTemplateEngine extends BaseTemplateEngine {
27 * Main nodes in the XML tree
29 protected $mainNodes = array();
32 * Sub nodes in the XML tree
34 protected $subNodes = array();
39 protected $curr = array();
44 private $xmlTemplateType = 'xml';
49 private $typePrefix = 'xml';
52 * Content from dependency
54 protected $dependencyContent = array();
57 * Protected constructor
59 * @param $className Name of the class
62 protected function __construct ($className) {
63 // Call parent constructor
64 parent::__construct($className);
68 * Does a generic initialization of the template engine
70 * @param $typePrefix Type prefix
71 * @param $xmlTemplateType Type of XML template
72 * @return $templateInstance 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 protected function initXmlTemplateEngine ($typePrefix, $xmlTemplateType) {
81 // Get template instance
82 $applicationInstance = Registry::getRegistry()->getInstance('app');
84 // Determine base path
85 $templateBasePath = $this->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
87 // Is the base path valid?
88 if (empty($templateBasePath)) {
90 throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
91 } elseif (!is_string($templateBasePath)) {
93 throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
94 } elseif (!is_dir($templateBasePath)) {
96 throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
97 } elseif (!is_readable($templateBasePath)) {
99 throw new BasePathReadProtectedException(array($this, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
103 $this->setTemplateBasePath($templateBasePath);
105 // Set template extensions
106 $this->setRawTemplateExtension($this->getConfigInstance()->getConfigEntry('raw_template_extension'));
107 $this->setCodeTemplateExtension($this->getConfigInstance()->getConfigEntry($typePrefix . '_message_template_extension'));
109 // Absolute output path for compiled templates
110 $this->setCompileOutputPath($this->getConfigInstance()->getConfigEntry('base_path') . $this->getConfigInstance()->getConfigEntry('compile_output_path'));
112 // Init a variable stacker
113 $stackerInstance = ObjectFactory::createObjectByConfiguredName($typePrefix . '_' . $xmlTemplateType . '_stacker_class');
116 $this->setStackerInstance($stackerInstance);
118 // Set XML template type and prefix
119 $this->xmlTemplateType = $xmlTemplateType;
120 $this->typePrefix = $typePrefix;
122 // Set it in main nodes
123 array_push($this->mainNodes, $xmlTemplateType);
127 * Load a specified XML template into the engine
129 * @param $template The XML template we shall load
132 public function loadXmlTemplate ($template) {
134 $this->setTemplateType($this->getConfigInstance()->getConfigEntry($this->xmlTemplateType . '_template_type'));
136 // Load the special template
137 $this->loadTemplate($template);
141 * Getter for current main node
143 * @return $currMainNode Current main node
145 public final function getCurrMainNode () {
146 return $this->curr['main_node'];
150 * Setter for current main node
152 * @param $element Element name to set as current main node
153 * @return $currMainNode Current main node
155 private final function setCurrMainNode ($element) {
156 $this->curr['main_node'] = (string) $element;
160 * Getter for main node array
162 * @return $mainNodes Array with valid main node names
164 public final function getMainNodes () {
165 return $this->mainNodes;
169 * Getter for sub node array
171 * @return $subNodes Array with valid sub node names
173 public final function getSubNodes () {
174 return $this->subNodes;
178 * Handles the template dependency for given node
180 * @param $node The node we should load a dependency template
181 * @param $templateDependency A template to load to satisfy dependencies
184 protected function handleTemplateDependency ($node, $templateDependency) {
185 // Is the template dependency set?
186 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
187 // Get a temporay template instance
188 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '__' . $this->xmlTemplateType . '_template_class');
190 // Create method name
191 $methodName = 'load' . $this->convertToClassName($this->xmlTemplateType) . 'Template';
194 call_user_func(array($templateInstance, $methodName), $templateDependency);
196 // Parse the XML content
197 $templateInstance->renderXmlContent();
199 // Save the parsed raw content in our dependency array
200 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
205 * Handles the start element of an XML resource
207 * @param $resource XML parser resource (currently ignored)
208 * @param $element The element we shall handle
209 * @param $attributes All attributes
211 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
213 public final function startElement ($resource, $element, array $attributes) {
214 // Initial method name which will never be called...
215 $methodName = 'init' . $this->convertToClassName($this->xmlTemplateType);
217 // Make the element name lower-case
218 $element = strtolower($element);
220 // Is the element a main node?
221 //* DEBUG: */ echo "START: >".$element."<<br />\n";
222 if (in_array($element, $this->getMainNodes())) {
223 // Okay, main node found!
224 $methodName = 'start' . $this->convertToClassName($element);
227 $this->setCurrMainNode($element);
228 } elseif (in_array($element, $this->getSubNodes())) {
230 $methodName = 'start' . $this->convertToClassName($element);
232 // Invalid node name found
233 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
237 call_user_func_array(array($this, $methodName), $attributes);
241 * Ends the main or sub node by sending out the gathered data
243 * @param $resource An XML resource pointer (currently ignored)
244 * @param $nodeName Name of the node we want to finish
246 * @throws XmlNodeMismatchException If current main node mismatches the closing one
248 public final function endElement ($resource, $nodeName) {
249 // Make all lower-case
250 $nodeName = strtolower($nodeName);
252 // Does this match with current main node?
253 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
254 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
256 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
259 // Construct method name
260 $methodName = 'finish' . $this->convertToClassName($nodeName);
262 // Call the corresponding method
263 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
264 call_user_func_array(array($this, $methodName), array());