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