3 * An ObjectRegistry 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 XmlObjectRegistryTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
27 * Instance for the object registry
29 private $objectRegistryInstance = null;
32 * Main nodes in the XML tree
34 private $mainNodes = array(
39 * Sub nodes in the XML tree
41 private $subNodes = array(
45 'object-recipient-limitation',
48 'object-recipient-type'
54 private $curr = array();
57 * Content from dependency
59 private $dependencyContent = array();
62 * Protected constructor
66 protected function __construct () {
67 // Call parent constructor
68 parent::__construct(__CLASS__);
70 // Init object type registry instance
71 $this->objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('object_type_registry_class');
75 * Creates an instance of the class TemplateEngine and prepares it for usage
77 * @param $applicationInstance A manageable application
78 * @return $templateInstance An instance of TemplateEngine
79 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
80 * @throws InvalidBasePathStringException If $templateBasePath is no string
81 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
82 * directory or not found
83 * @throws BasePathReadProtectedException If $templateBasePath is
86 public static final function createXmlObjectRegistryTemplateEngine (ManageableApplication $applicationInstance) {
88 $templateInstance = new XmlObjectRegistryTemplateEngine();
90 // Determine base path
91 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
93 // Is the base path valid?
94 if (empty($templateBasePath)) {
96 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
97 } elseif (!is_string($templateBasePath)) {
99 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
100 } elseif (!is_dir($templateBasePath)) {
102 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
103 } elseif (!is_readable($templateBasePath)) {
105 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
109 $templateInstance->setTemplateBasePath($templateBasePath);
111 // Set template extensions
112 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
113 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('object_registry_template_extension'));
115 // Absolute output path for compiled templates
116 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
118 // Init a variable stacker
119 $stackerInstance = ObjectFactory::createObjectByConfiguredName('object_registry_stacker_class');
122 $templateInstance->setStackerInstance($stackerInstance);
124 // Return the prepared instance
125 return $templateInstance;
129 * Load a specified object_registry template into the engine
131 * @param $template The object_registry template we shall load which is
132 * located in 'object_registry' by default
135 public function loadObjectRegistryTemplate ($template) {
137 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('object_registry_template_type'));
139 // Load the special template
140 $this->loadTemplate($template);
144 * Getter for current main node
146 * @return $currMainNode Current main node
148 public final function getCurrMainNode () {
149 return $this->curr['main_node'];
153 * Setter for current main node
155 * @param $element Element name to set as current main node
156 * @return $currMainNode Current main node
158 private final function setCurrMainNode ($element) {
159 $this->curr['main_node'] = (string) $element;
163 * Getter for main node array
165 * @return $mainNodes Array with valid main node names
167 public final function getMainNodes () {
168 return $this->mainNodes;
172 * Getter for sub node array
174 * @return $subNodes Array with valid sub node names
176 public final function getSubNodes () {
177 return $this->subNodes;
181 * Handles the start element of an XML resource
183 * @param $resource XML parser resource (currently ignored)
184 * @param $element The element we shall handle
185 * @param $attributes All attributes
187 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
189 public function startElement ($resource, $element, array $attributes) {
190 // Initial method name which will never be called...
191 $methodName = 'initObjectRegistry';
193 // Make the element name lower-case
194 $element = strtolower($element);
196 // Is the element a main node?
197 //* DEBUG: */ echo "START: >".$element."<<br />\n";
198 if (in_array($element, $this->getMainNodes())) {
199 // Okay, main node found!
200 $methodName = 'start' . $this->convertToClassName($element);
203 $this->setCurrMainNode($element);
204 } elseif (in_array($element, $this->getSubNodes())) {
206 $methodName = 'start' . $this->convertToClassName($element);
208 // Invalid node name found
209 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
213 call_user_func_array(array($this, $methodName), $attributes);
217 * Ends the main or sub node by sending out the gathered data
219 * @param $resource An XML resource pointer (currently ignored)
220 * @param $nodeName Name of the node we want to finish
222 * @throws XmlNodeMismatchException If current main node mismatches the closing one
224 public function endElement ($resource, $nodeName) {
225 // Make all lower-case
226 $nodeName = strtolower($nodeName);
228 // Does this match with current main node?
229 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
230 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
232 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
235 // Construct method name
236 $methodName = 'finish' . $this->convertToClassName($nodeName);
238 // Call the corresponding method
239 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
240 call_user_func_array(array($this, $methodName), array());
246 * @param $resource XML parser resource (currently ignored)
247 * @param $characters Characters to handle
249 * @todo Find something useful with this!
251 public function characterHandler ($resource, $characters) {
252 // Trim all spaces away
253 $characters = trim($characters);
255 // Is this string empty?
256 if (empty($characters)) {
257 // Then skip it silently
261 // Get current XML node name as an array index
262 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
264 // Is the node name 'object-name'?
265 if ($nodeName == 'object-name') {
266 // Output debug message
267 $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
270 // Add it to the registry
271 $this->objectRegistryInstance->addEntry($nodeName, $characters);
275 * Handles the template dependency for given node
277 * @param $node The node we should load a dependency template
278 * @param $templateDependency A template to load to satisfy dependencies
281 private function handleTemplateDependency ($node, $templateDependency) {
282 // Is the template dependency set?
283 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
284 // Get a temporay template instance
285 $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($this->getApplicationInstance()));
288 $templateInstance->loadObjectRegistryTemplate($templateDependency);
290 // Parse the XML content
291 $templateInstance->renderXmlContent();
293 // Save the parsed raw content in our dependency array
294 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
299 * Getter for cache file (FQFN)
301 * @return $fqfn Full-qualified file name of the menu cache
303 public function getObjectRegistryCacheFqfn () {
304 $this->partialStub('Please implement this method.');
308 * Starts the object-registry
312 private function startObjectRegistry () {
313 // Push the node name on the stacker
314 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
318 * Starts the object-list
322 private function startObjectList () {
323 // Push the node name on the stacker
324 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
328 * Starts the object-list-entry
332 private function startObjectListEntry () {
333 // Push the node name on the stacker
334 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
338 * Starts the object-name
342 private function startObjectName () {
343 // Push the node name on the stacker
344 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
348 * Starts the object-recipient-limitation
352 private function startObjectRecipientLimitation () {
353 // Push the node name on the stacker
354 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
358 * Starts the object-max-spread
362 private function startObjectMaxSpread () {
363 // Push the node name on the stacker
364 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
368 * Starts the object-protocol
372 private function startObjectProtocol () {
373 // Push the node name on the stacker
374 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
378 * Starts the object-recipient-type
382 private function startObjectRecipientType () {
383 // Push the node name on the stacker
384 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-type');
388 * Finishes the object-recipient-type
392 private function finishObjectRecipientType () {
393 // Pop the last entry
394 $this->getStackerInstance()->popNamed('object_registry');
398 * Finishes the object-protocol
402 private function finishObjectProtocol () {
403 // Pop the last entry
404 $this->getStackerInstance()->popNamed('object_registry');
408 * Finishes the object-max-spread
412 private function finishObjectMaxSpread () {
413 // Pop the last entry
414 $this->getStackerInstance()->popNamed('object_registry');
418 * Finishes the object-recipient-limitation
422 private function finishObjectRecipientLimitation () {
423 // Pop the last entry
424 $this->getStackerInstance()->popNamed('object_registry');
428 * Finishes the object-name
432 private function finishObjectName () {
433 // Pop the last entry
434 $this->getStackerInstance()->popNamed('object_registry');
438 * Finishes the object-list-entry
442 private function finishObjectListEntry () {
443 // Pop the last entry
444 $this->getStackerInstance()->popNamed('object_registry');
448 * Finishes the object-list
452 private function finishObjectList () {
453 // Pop the last entry
454 $this->getStackerInstance()->popNamed('object_registry');
458 * Finishes the object-registry
462 private function finishObjectRegistry () {
463 // Pop the last entry
464 $this->getStackerInstance()->popNamed('object_registry');