3 * A generic XML template engine class
5 * @author Roland Haeder <webmaster@shipsimu.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.shipsimu.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');
215 if (is_null($value)) {
216 // Bah, needs fixing.
217 $this->debugInstance('key=' . $key . ' returns NULL');
225 * Handles the template dependency for given node
227 * @param $node The node we should load a dependency template
228 * @param $templateDependency A template to load to satisfy dependencies
231 protected function handleTemplateDependency ($node, $templateDependency) {
232 // Check that node is not empty
233 assert(!empty($node));
235 // Is the template dependency set?
236 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
237 // Get a temporay template instance
238 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '_' . $this->convertDashesToUnderscores($node) . '_' . $this->xmlTemplateType . '_template_class');
241 $templateInstance->loadXmlTemplate($templateDependency);
243 // Parse the XML content
244 $templateInstance->renderXmlContent();
246 // Save the parsed raw content in our dependency array
247 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
252 * Handles the start element of an XML resource
254 * @param $resource XML parser resource (currently ignored)
255 * @param $element The element we shall handle
256 * @param $attributes All attributes
258 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
260 public final function startElement ($resource, $element, array $attributes) {
261 // Initial method name which will never be called...
262 $methodName = 'init' . $this->convertToClassName($this->xmlTemplateType);
264 // Make the element name lower-case
265 $element = strtolower($element);
267 // Is the element a main node?
268 //* DEBUG: */ echo "START: >".$element."<<br />\n";
269 if (in_array($element, $this->getMainNodes())) {
270 // Okay, main node found!
271 $methodName = 'start' . $this->convertToClassName($element);
274 $this->setCurrMainNode($element);
275 } elseif (in_array($element, $this->getSubNodes())) {
277 $methodName = 'start' . $this->convertToClassName($element);
279 // Invalid node name found
280 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
284 call_user_func_array(array($this, $methodName), $attributes);
288 * Ends the main or sub node by sending out the gathered data
290 * @param $resource An XML resource pointer (currently ignored)
291 * @param $nodeName Name of the node we want to finish
293 * @throws XmlNodeMismatchException If current main node mismatches the closing one
295 public final function finishElement ($resource, $nodeName) {
296 // Make all lower-case
297 $nodeName = strtolower($nodeName);
299 // Does this match with current main node?
300 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
301 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
303 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
306 // Construct method name
307 $methodName = 'finish' . $this->convertToClassName($nodeName);
309 // Call the corresponding method
310 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
311 call_user_func_array(array($this, $methodName), array());