]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/class_
Many XML-related templates engines renamed:
[hub.git] / application / hub / main / template / class_
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 - 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 die('YOU NEED TO REPLACE ???, !!! AND ||| IN THIS CLASS BEFORE YOU CAN USE IT.');
26 class Xml???TemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
27         /**
28          * Main nodes in the XML tree
29          */
30         private $mainNodes = array(
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37         );
38
39         /**
40          * Current main node
41          */
42         private $curr = array();
43
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         protected function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Creates an instance of the class TemplateEngine and prepares it for usage
56          *
57          * @param       $applicationInstance    A manageable application
58          * @return      $templateInstance               An instance of TemplateEngine
59          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
60          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
61          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
62          *                                                                                      directory or not found
63          * @throws      BasePathReadProtectedException  If $templateBasePath is
64          *                                                                                      read-protected
65          */
66         public static final function createXml???TemplateEngine (ManageableApplication $applicationInstance) {
67                 // Get a new instance
68                 $templateInstance = new Xml???TemplateEngine();
69
70                 // Determine base path
71                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
72
73                 // Is the base path valid?
74                 if (empty($templateBasePath)) {
75                         // Base path is empty
76                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77                 } elseif (!is_string($templateBasePath)) {
78                         // Is not a string
79                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
80                 } elseif (!is_dir($templateBasePath)) {
81                         // Is not a path
82                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
83                 } elseif (!is_readable($templateBasePath)) {
84                         // Is not readable
85                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
86                 }
87
88                 // Set the base path
89                 $templateInstance->setTemplateBasePath($templateBasePath);
90
91                 // Set template extensions
92                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
93                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('!!!_template_extension'));
94
95                 // Absolute output path for compiled templates
96                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
97
98                 // Init a variable stacker
99                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('!!!_stacker_class');
100
101                 // Set it
102                 $templateInstance->setStackerInstance($stackerInstance);
103
104                 // Return the prepared instance
105                 return $templateInstance;
106         }
107
108         /**
109          * Load a specified !!! template into the engine
110          *
111          * @param       $template       The !!! template we shall load which is
112          *                                              located in '!!!' by default
113          * @return      void
114          */
115         public function load???Template ($template) {
116                 // Set template type
117                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('!!!_template_type'));
118
119                 // Load the special template
120                 $this->loadTemplate($template);
121         }
122
123         /**
124          * Getter for current main node
125          *
126          * @return      $currMainNode   Current main node
127          */
128         public final function getCurrMainNode () {
129                 return $this->curr['main_node'];
130         }
131
132         /**
133          * Setter for current main node
134          *
135          * @param       $element                Element name to set as current main node
136          * @return      $currMainNode   Current main node
137          */
138         private final function setCurrMainNode ($element) {
139                 $this->curr['main_node'] = (string) $element;
140         }
141
142         /**
143          * Getter for main node array
144          *
145          * @return      $mainNodes      Array with valid main node names
146          */
147         public final function getMainNodes () {
148                 return $this->mainNodes;
149         }
150
151         /**
152          * Getter for sub node array
153          *
154          * @return      $subNodes       Array with valid sub node names
155          */
156         public final function getSubNodes () {
157                 return $this->subNodes;
158         }
159
160         /**
161          * Handles the start element of an XML resource
162          *
163          * @param       $resource               XML parser resource (currently ignored)
164          * @param       $element                The element we shall handle
165          * @param       $attributes             All attributes
166          * @return      void
167          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
168          */
169         public function startElement ($resource, $element, array $attributes) {
170                 // Initial method name which will never be called...
171                 $methodName = 'init???';
172
173                 // Make the element name lower-case
174                 $element = strtolower($element);
175
176                 // Is the element a main node?
177                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
178                 if (in_array($element, $this->getMainNodes())) {
179                         // Okay, main node found!
180                         $methodName = 'start' . $this->convertToClassName($element);
181
182                         // Set it
183                         $this->setCurrMainNode($element);
184                 } elseif (in_array($element, $this->getSubNodes())) {
185                         // Sub node found
186                         $methodName = 'start' . $this->convertToClassName($element);
187                 } else {
188                         // Invalid node name found
189                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
190                 }
191
192                 // Call method
193                 call_user_func_array(array($this, $methodName), $attributes);
194         }
195
196         /**
197          * Ends the main or sub node by sending out the gathered data
198          *
199          * @param       $resource       An XML resource pointer (currently ignored)
200          * @param       $nodeName       Name of the node we want to finish
201          * @return      void
202          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
203          */
204         public function endElement ($resource, $nodeName) {
205                 // Make all lower-case
206                 $nodeName = strtolower($nodeName);
207
208                 // Does this match with current main node?
209                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
210                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
211                         // Did not match!
212                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
213                 } // END - if
214
215                 // Construct method name
216                 $methodName = 'finish' . $this->convertToClassName($nodeName);
217
218                 // Call the corresponding method
219                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
220                 call_user_func_array(array($this, $methodName), array());
221         }
222
223         /**
224          * Currently not used
225          *
226          * @param       $resource               XML parser resource (currently ignored)
227          * @param       $characters             Characters to handle
228          * @return      void
229          * @todo        Find something useful with this!
230          */
231         public function characterHandler ($resource, $characters) {
232                 // Trim all spaces away
233                 $characters = trim($characters);
234
235                 // Is this string empty?
236                 if (empty($characters)) {
237                         // Then skip it silently
238                         return false;
239                 } // END - if
240
241                 // Get current XML node name as an array index
242                 $nodeName = $this->getStackerInstance()->getNamed('!!!');
243
244                 $this->partialStub('Do something with the gathered data.');
245         }
246
247         /**
248          * Getter for cache file (FQFN)
249          *
250          * @return      $fqfn   Full-qualified file name of the menu cache
251          */
252         public function get???CacheFqfn () {
253                 $this->partialStub('Please implement this method.');
254         }
255
256         /**
257          * Starts the |||
258          *
259          * @return      void
260          */
261         private function start??? () {
262                 // Push the node name on the stacker
263                 $this->getStackerInstance()->pushNamed('!!!', '|||');
264         }
265
266         /**
267          * Finishes the |||
268          *
269          * @return      void
270          */
271         private function finish??? () {
272                 // Pop the last entry
273                 $this->getStackerInstance()->popNamed('!!!');
274         }
275 }
276
277 // [EOF]
278 ?>