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