]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/class_BaseXmlTemplateEngine.php
Added template class for entries in a request-node-list
[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@ship-simu.org>
6  * @version             0.0.0
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()
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          * Content from dependency
53          */
54         protected $dependencyContent = array();
55
56         /**
57          * Protected constructor
58          *
59          * @param       $className      Name of the class
60          * @return      void
61          */
62         protected function __construct ($className) {
63                 // Call parent constructor
64                 parent::__construct($className);
65         }
66
67         /**
68          * Does a generic initialization of the template engine
69          *
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
78          *                                                                                      read-protected
79          */
80         protected function initXmlTemplateEngine ($typePrefix, $xmlTemplateType) {
81                 // Get template instance
82                 $applicationInstance = Registry::getRegistry()->getInstance('app');
83
84                 // Determine base path
85                 $templateBasePath = $this->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
86
87                 // Is the base path valid?
88                 if (empty($templateBasePath)) {
89                         // Base path is empty
90                         throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
91                 } elseif (!is_string($templateBasePath)) {
92                         // Is not a string
93                         throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
94                 } elseif (!is_dir($templateBasePath)) {
95                         // Is not a path
96                         throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
97                 } elseif (!is_readable($templateBasePath)) {
98                         // Is not readable
99                         throw new BasePathReadProtectedException(array($this, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
100                 }
101
102                 // Set the base path
103                 $this->setTemplateBasePath($templateBasePath);
104
105                 // Set template extensions
106                 $this->setRawTemplateExtension($this->getConfigInstance()->getConfigEntry('raw_template_extension'));
107                 $this->setCodeTemplateExtension($this->getConfigInstance()->getConfigEntry($typePrefix . '_message_template_extension'));
108
109                 // Absolute output path for compiled templates
110                 $this->setCompileOutputPath($this->getConfigInstance()->getConfigEntry('base_path') . $this->getConfigInstance()->getConfigEntry('compile_output_path'));
111
112                 // Init a variable stacker
113                 $stackerInstance = ObjectFactory::createObjectByConfiguredName($typePrefix . '_' . $xmlTemplateType . '_stacker_class');
114
115                 // Init stacker
116                 $stackerInstance->initStacker($typePrefix . '_' . $xmlTemplateType);
117
118                 // Set it
119                 $this->setStackerInstance($stackerInstance);
120
121                 // Set XML template type and prefix
122                 $this->xmlTemplateType = $xmlTemplateType;
123                 $this->typePrefix      = $typePrefix;
124
125                 // Set it in main nodes
126                 array_push($this->mainNodes, str_replace('_', '-', $xmlTemplateType));
127         }
128
129         /**
130          * Load a specified XML template into the engine
131          *
132          * @param       $templateName   Optional name of template
133          * @return      void
134          */
135         public function loadXmlTemplate ($templateName = '') {
136                 // Is the template name empty?
137                 if (empty($templateName)) {
138                         // Set generic template name
139                         $templateName = $this->typePrefix . '_' . $this->xmlTemplateType . '_template_type';
140                 } // END - if
141
142                 // Set template type
143                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry($templateName));
144
145                 // Load the special template
146                 $this->loadTemplate($this->xmlTemplateType);
147         }
148
149         /**
150          * Getter for current main node
151          *
152          * @return      $currMainNode   Current main node
153          */
154         public final function getCurrMainNode () {
155                 return $this->curr['main_node'];
156         }
157
158         /**
159          * Setter for current main node
160          *
161          * @param       $element                Element name to set as current main node
162          * @return      $currMainNode   Current main node
163          */
164         private final function setCurrMainNode ($element) {
165                 $this->curr['main_node'] = (string) $element;
166         }
167
168         /**
169          * Getter for main node array
170          *
171          * @return      $mainNodes      Array with valid main node names
172          */
173         public final function getMainNodes () {
174                 return $this->mainNodes;
175         }
176
177         /**
178          * Getter for sub node array
179          *
180          * @return      $subNodes       Array with valid sub node names
181          */
182         public final function getSubNodes () {
183                 return $this->subNodes;
184         }
185
186         /**
187          * Read XML variables by calling readVariable() with 'general' as
188          * variable stack.
189          *
190          * @param       $key    Key to read from
191          * @return      $value  Value from variable
192          */
193         public function readXmlData ($key) {
194                 // Read the variable
195                 $value = parent::readVariable($key, 'general');
196
197                 // Return value
198                 return $value;
199         }
200
201         /**
202          * Handles the template dependency for given node
203          *
204          * @param       $node                                   The node we should load a dependency template
205          * @param       $templateDependency             A template to load to satisfy dependencies
206          * @return      void
207          */
208         protected function handleTemplateDependency ($node, $templateDependency) {
209                 // Check that node is not empty
210                 assert(!empty($node));
211
212                 // Is the template dependency set?
213                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
214                         // Get a temporay template instance
215                         $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '_' . $this->convertDashesToUnderscores($node) . '_' . $this->xmlTemplateType . '_template_class');
216
217                         // Then load it
218                         $templateInstance->loadXmlTemplate($templateDependency);
219
220                         // Parse the XML content
221                         $templateInstance->renderXmlContent();
222
223                         // Save the parsed raw content in our dependency array
224                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
225                 } // END - if
226         }
227
228         /**
229          * Handles the start element of an XML resource
230          *
231          * @param       $resource               XML parser resource (currently ignored)
232          * @param       $element                The element we shall handle
233          * @param       $attributes             All attributes
234          * @return      void
235          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
236          */
237         public final function startElement ($resource, $element, array $attributes) {
238                 // Initial method name which will never be called...
239                 $methodName = 'init' . $this->convertToClassName($this->xmlTemplateType);
240
241                 // Make the element name lower-case
242                 $element = strtolower($element);
243
244                 // Is the element a main node?
245                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
246                 if (in_array($element, $this->getMainNodes())) {
247                         // Okay, main node found!
248                         $methodName = 'start' . $this->convertToClassName($element);
249
250                         // Set it
251                         $this->setCurrMainNode($element);
252                 } elseif (in_array($element, $this->getSubNodes())) {
253                         // Sub node found
254                         $methodName = 'start' . $this->convertToClassName($element);
255                 } else {
256                         // Invalid node name found
257                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
258                 }
259
260                 // Call method
261                 call_user_func_array(array($this, $methodName), $attributes);
262         }
263
264         /**
265          * Ends the main or sub node by sending out the gathered data
266          *
267          * @param       $resource       An XML resource pointer (currently ignored)
268          * @param       $nodeName       Name of the node we want to finish
269          * @return      void
270          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
271          */
272         public final function endElement ($resource, $nodeName) {
273                 // Make all lower-case
274                 $nodeName = strtolower($nodeName);
275
276                 // Does this match with current main node?
277                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
278                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
279                         // Did not match!
280                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
281                 } // END - if
282
283                 // Construct method name
284                 $methodName = 'finish' . $this->convertToClassName($nodeName);
285
286                 // Call the corresponding method
287                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
288                 call_user_func_array(array($this, $methodName), array());
289         }
290 }
291
292 // [EOF]
293 ?>