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(
42 private $curr = array();
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates an instance of the class TemplateEngine and prepares it for usage
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
65 public static final function createXmlCruncherTestUnitTemplateEngine () {
67 $templateInstance = new XmlCruncherTestUnitTemplateEngine();
69 // Get application instance from registry
70 $applicationInstance = Registry::getRegistry()->getInstance('app');
72 // Determine base path
73 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
75 // Is the base path valid?
76 if (empty($templateBasePath)) {
78 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
79 } elseif (!is_string($templateBasePath)) {
81 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
82 } elseif (!is_dir($templateBasePath)) {
84 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
85 } elseif (!is_readable($templateBasePath)) {
87 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
91 $templateInstance->setTemplateBasePath($templateBasePath);
93 // Set template extensions
94 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
95 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('cruncher_test_unit_template_extension'));
97 // Absolute output path for compiled templates
98 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
100 // Init a variable stacker
101 $stackerInstance = ObjectFactory::createObjectByConfiguredName('cruncher_test_unit_stacker_class');
104 $templateInstance->setStackerInstance($stackerInstance);
106 // Return the prepared instance
107 return $templateInstance;
111 * Load a specified cruncher_test_unit template into the engine
113 * @param $template The cruncher_test_unit template we shall load which is
114 * located in 'cruncher_test_unit' by default
117 public function loadCruncherTestUnitTemplate ($template) {
119 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('cruncher_test_unit_template_type'));
121 // Load the special template
122 $this->loadTemplate($template);
126 * Getter for current main node
128 * @return $currMainNode Current main node
130 public final function getCurrMainNode () {
131 return $this->curr['main_node'];
135 * Setter for current main node
137 * @param $element Element name to set as current main node
138 * @return $currMainNode Current main node
140 private final function setCurrMainNode ($element) {
141 $this->curr['main_node'] = (string) $element;
145 * Getter for main node array
147 * @return $mainNodes Array with valid main node names
149 public final function getMainNodes () {
150 return $this->mainNodes;
154 * Getter for sub node array
156 * @return $subNodes Array with valid sub node names
158 public final function getSubNodes () {
159 return $this->subNodes;
163 * Handles the start element of an XML resource
165 * @param $resource XML parser resource (currently ignored)
166 * @param $element The element we shall handle
167 * @param $attributes All attributes
169 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
171 public function startElement ($resource, $element, array $attributes) {
172 // Initial method name which will never be called...
173 $methodName = 'initTestUnit';
175 // Make the element name lower-case
176 $element = strtolower($element);
178 // Is the element a main node?
179 //* DEBUG: */ echo "START: >".$element."<<br />\n";
180 if (in_array($element, $this->getMainNodes())) {
181 // Okay, main node found!
182 $methodName = 'start' . $this->convertToClassName($element);
185 $this->setCurrMainNode($element);
186 } elseif (in_array($element, $this->getSubNodes())) {
188 $methodName = 'start' . $this->convertToClassName($element);
190 // Invalid node name found
191 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
195 call_user_func_array(array($this, $methodName), $attributes);
199 * Ends the main or sub node by sending out the gathered data
201 * @param $resource An XML resource pointer (currently ignored)
202 * @param $nodeName Name of the node we want to finish
204 * @throws XmlNodeMismatchException If current main node mismatches the closing one
206 public function endElement ($resource, $nodeName) {
207 // Make all lower-case
208 $nodeName = strtolower($nodeName);
210 // Does this match with current main node?
211 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
212 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
214 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
217 // Construct method name
218 $methodName = 'finish' . $this->convertToClassName($nodeName);
220 // Call the corresponding method
221 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
222 call_user_func_array(array($this, $methodName), array());
228 * @param $resource XML parser resource (currently ignored)
229 * @param $characters Characters to handle
231 * @todo Find something useful with this!
233 public function characterHandler ($resource, $characters) {
234 // Trim all spaces away
235 $characters = trim($characters);
237 // Is this string empty?
238 if (empty($characters)) {
239 // Then skip it silently
243 // Get current XML node name as an array index
244 $nodeName = $this->getStackerInstance()->getNamed('cruncher_test_unit');
246 $this->partialStub('TODO: Do something with the gathered data.');
250 * Getter for cache file (FQFN)
252 * @return $fqfn Full-qualified file name of the menu cache
254 public function getCruncherTestUnitCacheFqfn () {
255 $this->partialStub('Please implement this method.');
259 * Starts the test-unit
263 private function startCruncherTestUnit () {
264 // Push the node name on the stacker
265 $this->getStackerInstance()->pushNamed('cruncher_test_unit', 'cruncher-test-unit');
269 * Finishes the test-unit
273 private function finishCruncherTestUnit () {
274 // Pop the last entry
275 $this->getStackerInstance()->popNamed('cruncher_test_unit');