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';
54 private $stackerName = '';
57 * Content from dependency
59 protected $dependencyContent = array();
62 * Protected constructor
64 * @param $className Name of the class
67 protected function __construct ($className) {
68 // Call parent constructor
69 parent::__construct($className);
73 * Does a generic initialization of the template engine
75 * @param $typePrefix Type prefix
76 * @param $xmlTemplateType Type of XML template
77 * @return $templateInstance An instance of TemplateEngine
78 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
79 * @throws InvalidBasePathStringException If $templateBasePath is no string
80 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
81 * directory or not found
82 * @throws BasePathReadProtectedException If $templateBasePath is
85 protected function initXmlTemplateEngine ($typePrefix, $xmlTemplateType) {
86 // Get template instance
87 $applicationInstance = Registry::getRegistry()->getInstance('app');
89 // Determine base path
90 $templateBasePath = $this->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
92 // Is the base path valid?
93 if (empty($templateBasePath)) {
95 throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($templateBasePath)) {
98 throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
99 } elseif (!is_dir($templateBasePath)) {
101 throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102 } elseif (!is_readable($templateBasePath)) {
104 throw new BasePathReadProtectedException(array($this, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
108 $this->setTemplateBasePath($templateBasePath);
110 // Set template extensions
111 $this->setRawTemplateExtension($this->getConfigInstance()->getConfigEntry('raw_template_extension'));
112 $this->setCodeTemplateExtension($this->getConfigInstance()->getConfigEntry($typePrefix . '_message_template_extension'));
114 // Absolute output path for compiled templates
115 $this->setCompileOutputPath($this->getConfigInstance()->getConfigEntry('base_path') . $this->getConfigInstance()->getConfigEntry('compile_output_path'));
117 // Init a variable stacker
118 $stackerInstance = ObjectFactory::createObjectByConfiguredName($typePrefix . '_' . $xmlTemplateType . '_stacker_class');
121 $this->stackerName = $typePrefix . '_' . $xmlTemplateType;
124 $stackerInstance->initStack($this->stackerName);
127 $this->setStackerInstance($stackerInstance);
129 // Set XML template type and prefix
130 $this->xmlTemplateType = $xmlTemplateType;
131 $this->typePrefix = $typePrefix;
133 // Set it in main nodes
134 array_push($this->mainNodes, str_replace('_', '-', $xmlTemplateType));
138 * Load a specified XML template into the engine
140 * @param $templateName Optional name of template
143 public function loadXmlTemplate ($templateName = '') {
144 // Is the template name empty?
145 if (empty($templateName)) {
146 // Set generic template name
147 $templateName = $this->typePrefix . '_' . $this->xmlTemplateType . '_template_type';
151 $this->setTemplateType($this->getConfigInstance()->getConfigEntry($templateName));
153 // Load the special template
154 $this->loadTemplate($this->xmlTemplateType);
158 * Getter for current main node
160 * @return $currMainNode Current main node
162 public final function getCurrMainNode () {
163 return $this->curr['main_node'];
167 * Setter for current main node
169 * @param $element Element name to set as current main node
170 * @return $currMainNode Current main node
172 private final function setCurrMainNode ($element) {
173 $this->curr['main_node'] = (string) $element;
177 * Getter for main node array
179 * @return $mainNodes Array with valid main node names
181 public final function getMainNodes () {
182 return $this->mainNodes;
186 * Getter for stacker name
188 * @return $stackerName Name of stacker of this class
190 protected final function getStackerName () {
191 return $this->stackerName;
195 * Getter for sub node array
197 * @return $subNodes Array with valid sub node names
199 public final function getSubNodes () {
200 return $this->subNodes;
204 * Read XML variables by calling readVariable() with 'general' as
207 * @param $key Key to read from
208 * @return $value Value from variable
210 public function readXmlData ($key) {
212 $value = parent::readVariable($key, 'general');
219 * Handles the template dependency for given node
221 * @param $node The node we should load a dependency template
222 * @param $templateDependency A template to load to satisfy dependencies
225 protected function handleTemplateDependency ($node, $templateDependency) {
226 // Check that node is not empty
227 assert(!empty($node));
229 // Is the template dependency set?
230 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
231 // Get a temporay template instance
232 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '_' . $this->convertDashesToUnderscores($node) . '_' . $this->xmlTemplateType . '_template_class');
235 $templateInstance->loadXmlTemplate($templateDependency);
237 // Parse the XML content
238 $templateInstance->renderXmlContent();
240 // Save the parsed raw content in our dependency array
241 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
246 * Handles the start element of an XML resource
248 * @param $resource XML parser resource (currently ignored)
249 * @param $element The element we shall handle
250 * @param $attributes All attributes
252 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
254 public final function startElement ($resource, $element, array $attributes) {
255 // Initial method name which will never be called...
256 $methodName = 'init' . $this->convertToClassName($this->xmlTemplateType);
258 // Make the element name lower-case
259 $element = strtolower($element);
261 // Is the element a main node?
262 //* DEBUG: */ echo "START: >".$element."<<br />\n";
263 if (in_array($element, $this->getMainNodes())) {
264 // Okay, main node found!
265 $methodName = 'start' . $this->convertToClassName($element);
268 $this->setCurrMainNode($element);
269 } elseif (in_array($element, $this->getSubNodes())) {
271 $methodName = 'start' . $this->convertToClassName($element);
273 // Invalid node name found
274 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
278 call_user_func_array(array($this, $methodName), $attributes);
282 * Ends the main or sub node by sending out the gathered data
284 * @param $resource An XML resource pointer (currently ignored)
285 * @param $nodeName Name of the node we want to finish
287 * @throws XmlNodeMismatchException If current main node mismatches the closing one
289 public final function finishElement ($resource, $nodeName) {
290 // Make all lower-case
291 $nodeName = strtolower($nodeName);
293 // Does this match with current main node?
294 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
295 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
297 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
300 // Construct method name
301 $methodName = 'finish' . $this->convertToClassName($nodeName);
303 // Call the corresponding method
304 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
305 call_user_func_array(array($this, $methodName), array());