]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/class_BaseXmlTemplateEngine.php
Updated 'core' to latest commit.
[hub.git] / application / hub / main / template / class_BaseXmlTemplateEngine.php
1 <?php
2 /**
3  * A generic XML template engine class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 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()
11  *
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.
16  *
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.
21  *
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/>.
24  */
25 class BaseXmlTemplateEngine extends BaseTemplateEngine {
26         /**
27          * Main nodes in the XML tree
28          */
29         protected $mainNodes = array();
30
31         /**
32          * Sub nodes in the XML tree
33          */
34         protected $subNodes = array();
35
36         /**
37          * Current main node
38          */
39         protected $curr = array();
40
41         /**
42          * XML template type
43          */
44         private $xmlTemplateType = 'xml';
45
46         /**
47          * Type prefix
48          */
49         private $typePrefix = 'xml';
50
51         /**
52          * Name of stacker
53          */
54         private $stackerName = '';
55
56         /**
57          * Content from dependency
58          */
59         protected $dependencyContent = array();
60
61         /**
62          * Protected constructor
63          *
64          * @param       $className      Name of the class
65          * @return      void
66          */
67         protected function __construct ($className) {
68                 // Call parent constructor
69                 parent::__construct($className);
70         }
71
72         /**
73          * Does a generic initialization of the template engine
74          *
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
83          *                                                                                      read-protected
84          */
85         protected function initXmlTemplateEngine ($typePrefix, $xmlTemplateType) {
86                 // Get template instance
87                 $applicationInstance = Registry::getRegistry()->getInstance('app');
88
89                 // Determine base path
90                 $templateBasePath = $this->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
91
92                 // Is the base path valid?
93                 if (empty($templateBasePath)) {
94                         // Base path is empty
95                         throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96                 } elseif (!is_string($templateBasePath)) {
97                         // Is not a string
98                         throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
99                 } elseif (!is_dir($templateBasePath)) {
100                         // Is not a path
101                         throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102                 } elseif (!is_readable($templateBasePath)) {
103                         // Is not readable
104                         throw new BasePathReadProtectedException(array($this, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
105                 }
106
107                 // Set the base path
108                 $this->setTemplateBasePath($templateBasePath);
109
110                 // Set template extensions
111                 $this->setRawTemplateExtension($this->getConfigInstance()->getConfigEntry('raw_template_extension'));
112                 $this->setCodeTemplateExtension($this->getConfigInstance()->getConfigEntry($typePrefix . '_message_template_extension'));
113
114                 // Absolute output path for compiled templates
115                 $this->setCompileOutputPath($this->getConfigInstance()->getConfigEntry('base_path') . $this->getConfigInstance()->getConfigEntry('compile_output_path'));
116
117                 // Init a variable stacker
118                 $stackInstance = ObjectFactory::createObjectByConfiguredName($typePrefix . '_' . $xmlTemplateType . '_stacker_class');
119
120                 // Set name
121                 $this->stackerName = $typePrefix . '_' . $xmlTemplateType;
122
123                 // Init stacker
124                 $stackInstance->initStack($this->stackerName);
125
126                 // Set it
127                 $this->setStackInstance($stackInstance);
128
129                 // Set XML template type and prefix
130                 $this->xmlTemplateType = $xmlTemplateType;
131                 $this->typePrefix      = $typePrefix;
132
133                 // Set it in main nodes
134                 array_push($this->mainNodes, str_replace('_', '-', $xmlTemplateType));
135         }
136
137         /**
138          * Load a specified XML template into the engine
139          *
140          * @param       $templateName   Optional name of template
141          * @return      void
142          */
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';
148                 } // END - if
149
150                 // Set template type
151                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry($templateName));
152
153                 // Load the special template
154                 $this->loadTemplate($this->xmlTemplateType);
155         }
156
157         /**
158          * Getter for current main node
159          *
160          * @return      $currMainNode   Current main node
161          */
162         public final function getCurrMainNode () {
163                 return $this->curr['main_node'];
164         }
165
166         /**
167          * Setter for current main node
168          *
169          * @param       $element                Element name to set as current main node
170          * @return      $currMainNode   Current main node
171          */
172         private final function setCurrMainNode ($element) {
173                 $this->curr['main_node'] = (string) $element;
174         }
175
176         /**
177          * Getter for main node array
178          *
179          * @return      $mainNodes      Array with valid main node names
180          */
181         public final function getMainNodes () {
182                 return $this->mainNodes;
183         }
184
185         /**
186          * Getter for stacker name
187          *
188          * @return      $stackerName    Name of stacker of this class
189          */
190         protected final function getStackerName () {
191                 return $this->stackerName;
192         }
193
194         /**
195          * Getter for sub node array
196          *
197          * @return      $subNodes       Array with valid sub node names
198          */
199         public final function getSubNodes () {
200                 return $this->subNodes;
201         }
202
203         /**
204          * Read XML variables by calling readVariable() with 'general' as
205          * variable stack.
206          *
207          * @param       $key    Key to read from
208          * @return      $value  Value from variable
209          */
210         public function readXmlData ($key) {
211                 // Read the variable
212                 $value = parent::readVariable($key, 'general');
213
214                 // Is this null?
215                 if (is_null($value)) {
216                         // Bah, needs fixing.
217                         $this->debugInstance('key=' . $key . ' returns NULL');
218                 } // END - if
219
220                 // Return value
221                 return $value;
222         }
223
224         /**
225          * Handles the template dependency for given XML node
226          *
227          * @param       $node                                   The XML node we should load a dependency template
228          * @param       $templateDependency             A template to load to satisfy dependencies
229          * @return      void
230          */
231         protected function handleTemplateDependency ($node, $templateDependency) {
232                 // Check that the XML node is not empty
233                 assert(!empty($node));
234
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');
239
240                         // Then load it
241                         $templateInstance->loadXmlTemplate($templateDependency);
242
243                         // Parse the XML content
244                         $templateInstance->renderXmlContent();
245
246                         // Save the parsed raw content in our dependency array
247                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
248                 } // END - if
249         }
250
251         /**
252          * Handles the start element of an XML resource
253          *
254          * @param       $resource               XML parser resource (currently ignored)
255          * @param       $element                The element we shall handle
256          * @param       $attributes             All attributes
257          * @return      void
258          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
259          */
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);
263
264                 // Make the element name lower-case
265                 $element = strtolower($element);
266
267                 // Is the element a main node?
268                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
269                 if (in_array($element, $this->getMainNodes())) {
270                         // Okay, main node found!
271                         $methodName = 'start' . $this->convertToClassName($element);
272
273                         // Set it
274                         $this->setCurrMainNode($element);
275                 } elseif (in_array($element, $this->getSubNodes())) {
276                         // Sub node found
277                         $methodName = 'start' . $this->convertToClassName($element);
278                 } else {
279                         // Invalid node name found
280                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
281                 }
282
283                 // Call method
284                 call_user_func_array(array($this, $methodName), $attributes);
285         }
286
287         /**
288          * Ends the main or sub node by sending out the gathered data
289          *
290          * @param       $resource       An XML resource pointer (currently ignored)
291          * @param       $nodeName       Name of the node we want to finish
292          * @return      void
293          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
294          */
295         public final function finishElement ($resource, $nodeName) {
296                 // Make all lower-case
297                 $nodeName = strtolower($nodeName);
298
299                 // Does this match with current main node?
300                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
301                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
302                         // Did not match!
303                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
304                 } // END - if
305
306                 // Construct method name
307                 $methodName = 'finish' . $this->convertToClassName($nodeName);
308
309                 // Call the corresponding method
310                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
311                 call_user_func_array(array($this, $methodName), array());
312         }
313 }
314
315 // [EOF]
316 ?>