]> git.mxchange.org Git - hub.git/blob - application/hub/classes/template/class_BaseXmlTemplateEngine.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / 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 - 2015 Hub 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         private $mainNodes = array();
30
31         /**
32          * Sub nodes in the XML tree
33          */
34         private $subNodes = array();
35
36         /**
37          * Current main node
38          */
39         private $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          * Setter for sub node array
196          *
197          * @param       $subNodes       Array with valid sub node names
198          * @return      void
199          */
200         public final function setSubNodes (array $subNodes) {
201                 $this->subNodes = $subNodes;
202         }
203
204         /**
205          * Getter for sub node array
206          *
207          * @return      $subNodes       Array with valid sub node names
208          */
209         public final function getSubNodes () {
210                 return $this->subNodes;
211         }
212
213         /**
214          * Read XML variables by calling readVariable() with 'general' as
215          * variable stack.
216          *
217          * @param       $key    Key to read from
218          * @return      $value  Value from variable
219          */
220         public function readXmlData ($key) {
221                 // Read the variable
222                 $value = parent::readVariable($key, 'general');
223
224                 // Is this null?
225                 if (is_null($value)) {
226                         // Bah, needs fixing.
227                         $this->debugInstance('key=' . $key . ' returns NULL');
228                 } // END - if
229
230                 // Return value
231                 return $value;
232         }
233
234         /**
235          * Handles the template dependency for given XML node
236          *
237          * @param       $node                                   The XML node we should load a dependency template
238          * @param       $templateDependency             A template to load to satisfy dependencies
239          * @return      void
240          */
241         protected function handleTemplateDependency ($node, $templateDependency) {
242                 // Check that the XML node is not empty
243                 assert(!empty($node));
244
245                 // Is the template dependency set?
246                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
247                         // Get a temporay template instance
248                         $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '_' . self::convertDashesToUnderscores($node) . '_' . $this->xmlTemplateType . '_template_class');
249
250                         // Then load it
251                         $templateInstance->loadXmlTemplate($templateDependency);
252
253                         // Parse the XML content
254                         $templateInstance->renderXmlContent();
255
256                         // Save the parsed raw content in our dependency array
257                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
258                 } // END - if
259         }
260
261         /**
262          * Handles the start element of an XML resource
263          *
264          * @param       $resource               XML parser resource (currently ignored)
265          * @param       $element                The element we shall handle
266          * @param       $attributes             All attributes
267          * @return      void
268          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
269          */
270         public final function startElement ($resource, $element, array $attributes) {
271                 // Initial method name which will never be called...
272                 $methodName = 'init' . self::convertToClassName($this->xmlTemplateType);
273
274                 // Make the element name lower-case
275                 $element = strtolower($element);
276
277                 // Is the element a main node?
278                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
279                 if (in_array($element, $this->getMainNodes())) {
280                         // Okay, main node found!
281                         $methodName = 'start' . self::convertToClassName($element);
282
283                         // Set it
284                         $this->setCurrMainNode($element);
285                 } elseif (in_array($element, $this->getSubNodes())) {
286                         // Sub node found
287                         $methodName = 'start' . self::convertToClassName($element);
288                 } else {
289                         // Invalid node name found
290                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
291                 }
292
293                 // Call method
294                 call_user_func_array(array($this, $methodName), $attributes);
295         }
296
297         /**
298          * Ends the main or sub node by sending out the gathered data
299          *
300          * @param       $resource       An XML resource pointer (currently ignored)
301          * @param       $nodeName       Name of the node we want to finish
302          * @return      void
303          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
304          */
305         public final function finishElement ($resource, $nodeName) {
306                 // Make all lower-case
307                 $nodeName = strtolower($nodeName);
308
309                 // Does this match with current main node?
310                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
311                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
312                         // Did not match!
313                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
314                 } // END - if
315
316                 // Construct method name
317                 $methodName = 'finish' . self::convertToClassName($nodeName);
318
319                 // Call the corresponding method
320                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
321                 call_user_func_array(array($this, $methodName), array());
322         }
323 }
324
325 // [EOF]
326 ?>