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