]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/connect/class_XmlSelfConnectTemplateEngine.php
Removed obsolete parameter 'applicationInstance' from all template engines
[hub.git] / application / hub / main / template / connect / class_XmlSelfConnectTemplateEngine.php
1 <?php
2 /**
3  * An SelfConnect template engine class for XML templates
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 XmlSelfConnectTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Main nodes in the XML tree
28          */
29         private $mainNodes = array(
30                 'self-connect',
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37                 'self-connect-data',
38                 'node-id',
39                 'session-id'
40         );
41
42         /**
43          * Current main node
44          */
45         private $curr = array();
46
47         /**
48          * Content from dependency
49          */
50         private $dependencyContent = array();
51
52         /**
53          * Protected constructor
54          *
55          * @return      void
56          */
57         protected function __construct () {
58                 // Call parent constructor
59                 parent::__construct(__CLASS__);
60         }
61
62         /**
63          * Creates an instance of the class TemplateEngine and prepares it for usage
64          *
65          * @return      $templateInstance               An instance of TemplateEngine
66          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
67          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
68          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
69          *                                                                                      directory or not found
70          * @throws      BasePathReadProtectedException  If $templateBasePath is
71          *                                                                                      read-protected
72          */
73         public static final function createXmlSelfConnectTemplateEngine () {
74                 // Get a new instance
75                 $templateInstance = new XmlSelfConnectTemplateEngine();
76
77                 // Get the application instance from registry
78                 $applicationInstance = Registry::getRegistry()->getInstance('app');
79
80                 // Determine base path
81                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
82
83                 // Is the base path valid?
84                 if (empty($templateBasePath)) {
85                         // Base path is empty
86                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
87                 } elseif (!is_string($templateBasePath)) {
88                         // Is not a string
89                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
90                 } elseif (!is_dir($templateBasePath)) {
91                         // Is not a path
92                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
93                 } elseif (!is_readable($templateBasePath)) {
94                         // Is not readable
95                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
96                 }
97
98                 // Set the base path
99                 $templateInstance->setTemplateBasePath($templateBasePath);
100
101                 // Set template extensions
102                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
103                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_self_connect_template_extension'));
104
105                 // Absolute output path for compiled templates
106                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
107
108                 // Init a variable stacker
109                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_self_connect_stacker_class');
110
111                 // Set it
112                 $templateInstance->setStackerInstance($stackerInstance);
113
114                 // Return the prepared instance
115                 return $templateInstance;
116         }
117
118         /**
119          * Load a specified self-connect template into the engine
120          *
121          * @param       $template       The self-connect template we shall load which is
122          *                                              located in 'self_connect' by default
123          * @return      void
124          */
125         public function loadSelfConnectTemplate ($template = 'self_connect') {
126                 // Set template type
127                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_self_connect_template_type'));
128
129                 // Load the special template
130                 $this->loadTemplate($template);
131         }
132
133         /**
134          * Getter for current main node
135          *
136          * @return      $currMainNode   Current main node
137          */
138         public final function getCurrMainNode () {
139                 return $this->curr['main_node'];
140         }
141
142         /**
143          * Setter for current main node
144          *
145          * @param       $element                Element name to set as current main node
146          * @return      $currMainNode   Current main node
147          */
148         private final function setCurrMainNode ($element) {
149                 $this->curr['main_node'] = (string) $element;
150         }
151
152         /**
153          * Getter for main node array
154          *
155          * @return      $mainNodes      Array with valid main node names
156          */
157         public final function getMainNodes () {
158                 return $this->mainNodes;
159         }
160
161         /**
162          * Getter for sub node array
163          *
164          * @return      $subNodes       Array with valid sub node names
165          */
166         public final function getSubNodes () {
167                 return $this->subNodes;
168         }
169
170         /**
171          * Handles the start element of an XML resource
172          *
173          * @param       $resource               XML parser resource (currently ignored)
174          * @param       $element                The element we shall handle
175          * @param       $attributes             All attributes
176          * @return      void
177          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
178          */
179         public function startElement ($resource, $element, array $attributes) {
180                 // Initial method name which will never be called...
181                 $methodName = 'initSelfConnect';
182
183                 // Make the element name lower-case
184                 $element = strtolower($element);
185
186                 // Is the element a main node?
187                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
188                 if (in_array($element, $this->getMainNodes())) {
189                         // Okay, main node found!
190                         $methodName = 'start' . $this->convertToClassName($element);
191
192                         // Set it
193                         $this->setCurrMainNode($element);
194                 } elseif (in_array($element, $this->getSubNodes())) {
195                         // Sub node found
196                         $methodName = 'start' . $this->convertToClassName($element);
197                 } else {
198                         // Invalid node name found
199                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
200                 }
201
202                 // Call method
203                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
204                 call_user_func_array(array($this, $methodName), $attributes);
205         }
206
207         /**
208          * Ends the main or sub node by sending out the gathered data
209          *
210          * @param       $resource       An XML resource pointer (currently ignored)
211          * @param       $nodeName       Name of the node we want to finish
212          * @return      void
213          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
214          */
215         public function endElement ($resource, $nodeName) {
216                 // Make all lower-case
217                 $nodeName = strtolower($nodeName);
218
219                 // Does this match with current main node?
220                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
221                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
222                         // Did not match!
223                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
224                 } // END - if
225
226                 // Construct method name
227                 $methodName = 'finish' . $this->convertToClassName($nodeName);
228
229                 // Call the corresponding method
230                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
231                 call_user_func_array(array($this, $methodName), array());
232         }
233
234         /**
235          * Currently not used
236          *
237          * @param       $resource               XML parser resource (currently ignored)
238          * @param       $characters             Characters to handle
239          * @return      void
240          * @todo        Find something useful with this!
241          */
242         public function characterHandler ($resource, $characters) {
243                 // Trim all spaces away
244                 $characters = trim($characters);
245
246                 // Is this string empty?
247                 if (empty($characters)) {
248                         // Then skip it silently
249                         return false;
250                 } // END - if
251
252                 // Assign the found characters to variable and use the last entry from
253                 // stack as the name
254                 parent::assignVariable($this->getStackerInstance()->getNamed('self_connect'), $characters);
255         }
256
257         /**
258          * Handles the template dependency for given node
259          *
260          * @param       $node                                   The node we should load a dependency template
261          * @param       $templateDependency             A template to load to satisfy dependencies
262          * @return      void
263          */
264         private function handleTemplateDependency ($node, $templateDependency) {
265                 // Is the template dependency set?
266                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
267                         // Get a temporay template instance
268                         $templateInstance = ObjectFactory::createObjectByConfiguredName('node_self_connect_template_class');
269
270                         // Then load it
271                         $templateInstance->loadSelfConnectTemplate($templateDependency);
272
273                         // Parse the XML content
274                         $templateInstance->renderXmlContent();
275
276                         // Save the parsed raw content in our dependency array
277                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
278                 } // END - if
279         }
280
281         /**
282          * Getter for cache file (FQFN)
283          *
284          * @return      $fqfn   Full-qualified file name of the menu cache
285          */
286         public function getMenuCacheFqfn () {
287                 $this->partialStub('Please implement this method.');
288         }
289
290         /**
291          * Starts the self_connect
292          *
293          * @return      void
294          */
295         private function startSelfConnect () {
296                 // Push the node name on the stacker
297                 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect');
298         }
299
300         /**
301          * Starts the self_connect data
302          *
303          * @return      void
304          */
305         private function startSelfConnectData () {
306                 // Push the node name on the stacker
307                 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect-data');
308         }
309
310         /**
311          * Starts the node id
312          *
313          * @return      void
314          */
315         private function startNodeId () {
316                 // Push the node name on the stacker
317                 $this->getStackerInstance()->pushNamed('self_connect', 'node-id');
318         }
319
320         /**
321          * Starts the session id
322          *
323          * @return      void
324          */
325         private function startSessionId () {
326                 // Push the node name on the stacker
327                 $this->getStackerInstance()->pushNamed('self_connect', 'session-id');
328         }
329
330         /**
331          * Finishes the session id
332          *
333          * @return      void
334          */
335         private function finishSessionId () {
336                 // Pop the last entry
337                 $this->getStackerInstance()->popNamed('self_connect');
338         }
339
340         /**
341          * Finishes the node id
342          *
343          * @return      void
344          */
345         private function finishNodeId () {
346                 // Pop the last entry
347                 $this->getStackerInstance()->popNamed('self_connect');
348         }
349
350         /**
351          * Finishes the self_connect data
352          *
353          * @return      void
354          */
355         private function finishSelfConnectData () {
356                 // Pop the last entry
357                 $this->getStackerInstance()->popNamed('self_connect');
358         }
359
360         /**
361          * Finishes the self_connect
362          *
363          * @return      void
364          */
365         private function finishSelfConnect () {
366                 // Pop the last entry
367                 $this->getStackerInstance()->popNamed('self_connect');
368         }
369 }
370
371 // [EOF]
372 ?>