3 * An TestUnit template engine class for XML templates
5 * @author Roland Haeder <webmaster@ship-simu.org>
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()
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.
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.
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/>.
25 class XmlCruncherTestUnitTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
27 * Main nodes in the XML tree
29 private $mainNodes = array(
34 * Sub nodes in the XML tree
36 private $subNodes = array(
38 'global-project-identifier',
54 private $curr = array();
57 * Protected constructor
61 protected function __construct () {
62 // Call parent constructor
63 parent::__construct(__CLASS__);
67 * Creates an instance of the class TemplateEngine and prepares it for usage
69 * @return $templateInstance An instance of TemplateEngine
70 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
71 * @throws InvalidBasePathStringException If $templateBasePath is no string
72 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
73 * directory or not found
74 * @throws BasePathReadProtectedException If $templateBasePath is
77 public static final function createXmlCruncherTestUnitTemplateEngine () {
79 $templateInstance = new XmlCruncherTestUnitTemplateEngine();
81 // Get application instance from registry
82 $applicationInstance = Registry::getRegistry()->getInstance('app');
84 // Determine base path
85 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
87 // Is the base path valid?
88 if (empty($templateBasePath)) {
90 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
91 } elseif (!is_string($templateBasePath)) {
93 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
94 } elseif (!is_dir($templateBasePath)) {
96 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
97 } elseif (!is_readable($templateBasePath)) {
99 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
103 $templateInstance->setTemplateBasePath($templateBasePath);
105 // Set template extensions
106 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
107 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('cruncher_test_unit_template_extension'));
109 // Absolute output path for compiled templates
110 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
112 // Init a variable stacker
113 $stackerInstance = ObjectFactory::createObjectByConfiguredName('cruncher_test_unit_stacker_class');
116 $templateInstance->setStackerInstance($stackerInstance);
118 // Return the prepared instance
119 return $templateInstance;
123 * Load a specified cruncher_test_unit template into the engine
125 * @param $template The cruncher_test_unit template we shall load which is
126 * located in 'cruncher_test_unit' by default
129 public function loadCruncherTestUnitTemplate ($template) {
131 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('cruncher_test_unit_template_type'));
133 // Load the special template
134 $this->loadTemplate($template);
138 * Getter for current main node
140 * @return $currMainNode Current main node
142 public final function getCurrMainNode () {
143 return $this->curr['main_node'];
147 * Setter for current main node
149 * @param $element Element name to set as current main node
150 * @return $currMainNode Current main node
152 private final function setCurrMainNode ($element) {
153 $this->curr['main_node'] = (string) $element;
157 * Getter for main node array
159 * @return $mainNodes Array with valid main node names
161 public final function getMainNodes () {
162 return $this->mainNodes;
166 * Getter for sub node array
168 * @return $subNodes Array with valid sub node names
170 public final function getSubNodes () {
171 return $this->subNodes;
175 * Handles the start element of an XML resource
177 * @param $resource XML parser resource (currently ignored)
178 * @param $element The element we shall handle
179 * @param $attributes All attributes
181 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
183 public function startElement ($resource, $element, array $attributes) {
184 // Initial method name which will never be called...
185 $methodName = 'initTestUnit';
187 // Make the element name lower-case
188 $element = strtolower($element);
190 // Is the element a main node?
191 //* DEBUG: */ echo "START: >".$element."<<br />\n";
192 if (in_array($element, $this->getMainNodes())) {
193 // Okay, main node found!
194 $methodName = 'start' . $this->convertToClassName($element);
197 $this->setCurrMainNode($element);
198 } elseif (in_array($element, $this->getSubNodes())) {
200 $methodName = 'start' . $this->convertToClassName($element);
202 // Invalid node name found
203 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
207 call_user_func_array(array($this, $methodName), $attributes);
211 * Ends the main or sub node by sending out the gathered data
213 * @param $resource An XML resource pointer (currently ignored)
214 * @param $nodeName Name of the node we want to finish
216 * @throws XmlNodeMismatchException If current main node mismatches the closing one
218 public function endElement ($resource, $nodeName) {
219 // Make all lower-case
220 $nodeName = strtolower($nodeName);
222 // Does this match with current main node?
223 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
224 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
226 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
229 // Construct method name
230 $methodName = 'finish' . $this->convertToClassName($nodeName);
232 // Call the corresponding method
233 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
234 call_user_func_array(array($this, $methodName), array());
240 * @param $resource XML parser resource (currently ignored)
241 * @param $characters Characters to handle
244 public function characterHandler ($resource, $characters) {
245 // Trim all spaces away
246 $characters = trim($characters);
248 // Is this string empty?
249 if (empty($characters)) {
250 // Then skip it silently
254 // Assign the found characters to variable and use the last entry from
256 parent::assignVariable($this->getStackerInstance()->getNamed('cruncher_test_unit'), $characters);
260 * Getter for cache file (FQFN)
262 * @return $fqfn Full-qualified file name of the menu cache
264 public function getCruncherTestUnitCacheFqfn () {
265 $this->partialStub('Please implement this method.');
269 * Starts the test-unit
273 private function startCruncherTestUnit () {
274 // Push the node name on the stacker
275 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cruncher-test-unit');
279 * Starts the meta-data
283 private function startMetaData () {
284 // Push the node name on the stacker
285 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'meta-data');
289 * Starts the global-project-identifier
293 private function startGlobalProjectIdentifier () {
294 // Push the node name on the stacker
295 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'global-project-identifier');
299 * Finishes the global-project-identifier
303 private function finishGlobalProjectIdentifier () {
304 // Pop the last entry
305 $this->getStackerInstance()->popNamed('cruncher_test_unit');
309 * Starts the test-unit-created
313 private function startTestUnitCreated () {
314 // Push the node name on the stacker
315 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'test-unit-created');
319 * Finishes the test-unit-created
323 private function finishTestUnitCreated () {
324 // Pop the last entry
325 $this->getStackerInstance()->popNamed('cruncher_test_unit');
333 private function startCipher () {
334 // Push the node name on the stacker
335 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cipher');
339 * Starts the cipher-function
343 private function startCipherFunction () {
344 // Push the node name on the stacker
345 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cipher-function');
349 * Finishes the cipher-function
353 private function finishCipherFunction () {
354 // Pop the last entry
355 $this->getStackerInstance()->popNamed('cruncher_test_unit');
359 * Starts the cipher-name
363 private function startCipherName () {
364 // Push the node name on the stacker
365 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cipher-name');
369 * Finishes the cipher-name
373 private function finishCipherName () {
374 // Pop the last entry
375 $this->getStackerInstance()->popNamed('cruncher_test_unit');
379 * Finishes the cipher
383 private function finishCipher () {
384 // Pop the last entry
385 $this->getStackerInstance()->popNamed('cruncher_test_unit');
389 * Finishes the meta-data
393 private function finishMetaData () {
394 // Pop the last entry
395 $this->getStackerInstance()->popNamed('cruncher_test_unit');
399 * Starts the unprocessed-data
403 private function startUnprocessedData () {
404 // Push the node name on the stacker
405 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'unprocessed-data');
409 * Starts the encrypted-message
413 private function startEncryptedMessage () {
414 // Push the node name on the stacker
415 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'encrypted-message');
419 * Finishes the encrypted-message
423 private function finishEncryptedMessage () {
424 // Pop the last entry
425 $this->getStackerInstance()->popNamed('cruncher_test_unit');
429 * Starts the cruncher-key-list
431 * @param $keyCount Count of keys in this test unit
433 * @todo Handle $keyCount
435 private function startCruncherKeyList ($keyCount) {
436 // Push the node name on the stacker
437 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cruncher-key-list');
441 * Starts the cruncher-key
445 private function startCruncherKey () {
446 // Push the node name on the stacker
447 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cruncher-key');
455 private function startKeyId () {
456 // Push the node name on the stacker
457 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'key-id');
461 * Finishes the key-id
465 private function finishKeyId () {
466 // Pop the last entry
467 $this->getStackerInstance()->popNamed('cruncher_test_unit');
471 * Starts the key-value
475 private function startKeyValue () {
476 // Push the node name on the stacker
477 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'key-value');
481 * Finishes the key-value
485 private function finishKeyValue () {
486 // Pop the last entry
487 $this->getStackerInstance()->popNamed('cruncher_test_unit');
491 * Finishes the cruncher-key
495 private function finishCruncherKey () {
496 // Pop the last entry
497 $this->getStackerInstance()->popNamed('cruncher_test_unit');
501 * Finishes the cruncher-key-list
505 private function finishCruncherKeyList () {
506 // Pop the last entry
507 $this->getStackerInstance()->popNamed('cruncher_test_unit');
511 * Finishes the unprocessed-data
515 private function finishUnprocessedData () {
516 // Pop the last entry
517 $this->getStackerInstance()->popNamed('cruncher_test_unit');
521 * Finishes the test-unit
525 private function finishCruncherTestUnit () {
526 // Pop the last entry
527 $this->getStackerInstance()->popNamed('cruncher_test_unit');