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