]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/requests/class_XmlRequestNodeListTemplateEngine.php
Introduced new factory for object type registries, added a very simple way to get...
[hub.git] / application / hub / main / template / requests / class_XmlRequestNodeListTemplateEngine.php
1 <?php
2 /**
3  * An ??? 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 XmlRequestNodeListTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
26         // Constants for array elements
27         const REQUEST_DATA_ACCEPTED_OBJECT_TYPES = 'accepted-object-types';
28         const REQUEST_DATA_SESSION_ID            = 'session-id';
29
30         /**
31          * Main nodes in the XML tree
32          */
33         private $mainNodes = array(
34                 'node-request-node-list'
35         );
36
37         /**
38          * Sub nodes in the XML tree
39          */
40         private $subNodes = array(
41                 self::REQUEST_DATA_ACCEPTED_OBJECT_TYPES,
42                 self::REQUEST_DATA_SESSION_ID
43         );
44
45         /**
46          * Current main node
47          */
48         private $curr = array();
49
50         /**
51          * Protected constructor
52          *
53          * @return      void
54          */
55         protected function __construct () {
56                 // Call parent constructor
57                 parent::__construct(__CLASS__);
58         }
59
60         /**
61          * Creates an instance of the class TemplateEngine and prepares it for usage
62          *
63          * @return      $templateInstance               An instance of TemplateEngine
64          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
65          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
66          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
67          *                                                                                      directory or not found
68          * @throws      BasePathReadProtectedException  If $templateBasePath is
69          *                                                                                      read-protected
70          */
71         public static final function createXmlRequestNodeListTemplateEngine () {
72                 // Get a new instance
73                 $templateInstance = new XmlRequestNodeListTemplateEngine();
74
75                 // Get application instance from registry
76                 $applicationInstance = Registry::getRegistry()->getInstance('app');
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_message_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_request_node_list_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 request_node_list template into the engine
118          *
119          * @param       $template       The request_node_list template we shall load which is
120          *                                              located in 'node_request_node_list' by default
121          * @return      void
122          */
123         public function loadRequestNodeListTemplate ($template = 'node_request_node_list') {
124                 // Set template type
125                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_request_node_list_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 = 'initNodeRequestNodeList';
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                 call_user_func_array(array($this, $methodName), $attributes);
202         }
203
204         /**
205          * Ends the main or sub node by sending out the gathered data
206          *
207          * @param       $resource       An XML resource pointer (currently ignored)
208          * @param       $nodeName       Name of the node we want to finish
209          * @return      void
210          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
211          */
212         public function endElement ($resource, $nodeName) {
213                 // Make all lower-case
214                 $nodeName = strtolower($nodeName);
215
216                 // Does this match with current main node?
217                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
218                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
219                         // Did not match!
220                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
221                 } // END - if
222
223                 // Construct method name
224                 $methodName = 'finish' . $this->convertToClassName($nodeName);
225
226                 // Call the corresponding method
227                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
228                 call_user_func_array(array($this, $methodName), array());
229         }
230
231         /**
232          * Currently not used
233          *
234          * @param       $resource               XML parser resource (currently ignored)
235          * @param       $characters             Characters to handle
236          * @return      void
237          * @todo        Find something useful with this!
238          */
239         public function characterHandler ($resource, $characters) {
240                 // Trim all spaces away
241                 $characters = trim($characters);
242
243                 // Is this string empty?
244                 if (empty($characters)) {
245                         // Then skip it silently
246                         return false;
247                 } // END - if
248
249                 // Assign data with matching variable here
250                 parent::assignVariable($this->getStackerInstance()->getNamed('node_request_node_list'), $characters);
251         }
252
253         /**
254          * Getter for cache file (FQFN)
255          *
256          * @return      $fqfn   Full-qualified file name of the menu cache
257          */
258         public function getNodeRequestNodeListCacheFqfn () {
259                 $this->partialStub('Please implement this method.');
260         }
261
262         /**
263          * Starts the node-request-node-list
264          *
265          * @return      void
266          */
267         private function startNodeRequestNodeList () {
268                 // Push the node name on the stacker
269                 $this->getStackerInstance()->pushNamed('node_request_node_list', 'node-request-node-list');
270         }
271
272         /**
273          * Starts the accepted-object-types
274          *
275          * @return      void
276          */
277         private function startAcceptedObjectTypes () {
278                 // Push the node name on the stacker
279                 $this->getStackerInstance()->pushNamed('node_request_node_list', self::REQUEST_DATA_ACCEPTED_OBJECT_TYPES);
280         }
281
282         /**
283          * Starts the session-id
284          *
285          * @return      void
286          */
287         private function startSessionId () {
288                 // Push the node name on the stacker
289                 $this->getStackerInstance()->pushNamed('node_request_node_list', self::REQUEST_DATA_SESSION_ID);
290         }
291
292         /**
293          * Finishes the session-id
294          *
295          * @return      void
296          */
297         private function finishSessionId () {
298                 // Pop the last entry
299                 $this->getStackerInstance()->popNamed('node_request_node_list');
300         }
301
302         /**
303          * Finishes the accepted-object-types
304          *
305          * @return      void
306          */
307         private function finishAcceptedObjectTypes () {
308                 // Pop the last entry
309                 $this->getStackerInstance()->popNamed('node_request_node_list');
310         }
311
312         /**
313          * Finishes the node-request-node-list
314          *
315          * @return      void
316          */
317         private function finishNodeRequestNodeList () {
318                 // Pop the last entry
319                 $this->getStackerInstance()->popNamed('node_request_node_list');
320         }
321 }
322
323 // [EOF]
324 ?>