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