3 * An ObjectRegistry template engine class
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 ObjectRegistryTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
27 * Instance for the object registry
29 private $objectRegistryInstance = null;
32 * Main nodes in the XML tree ('menu' is ignored)
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 $appInstance 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 createObjectRegistryTemplateEngine (ManageableApplication $appInstance) {
88 $templateInstance = new ObjectRegistryTemplateEngine();
90 // Get language and file I/O instances from application
91 $langInstance = $appInstance->getLanguageInstance();
92 $ioInstance = $appInstance->getFileIoInstance();
94 // Determine base path
95 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
97 // Is the base path valid?
98 if (empty($templateBasePath)) {
100 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
101 } elseif (!is_string($templateBasePath)) {
103 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
104 } elseif (!is_dir($templateBasePath)) {
106 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107 } elseif (!is_readable($templateBasePath)) {
109 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
113 $templateInstance->setTemplateBasePath($templateBasePath);
115 // Set the language and IO instances
116 $templateInstance->setLanguageInstance($langInstance);
117 $templateInstance->setFileIoInstance($ioInstance);
119 // Set template extensions
120 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
121 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('object_registry_template_extension'));
123 // Absolute output path for compiled templates
124 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
126 // Init a variable stacker
127 $stackerInstance = ObjectFactory::createObjectByConfiguredName('object_registry_stacker_class');
130 $templateInstance->setStackerInstance($stackerInstance);
132 // Return the prepared instance
133 return $templateInstance;
137 * Load a specified object_registry template into the engine
139 * @param $template The object_registry template we shall load which is
140 * located in 'object_registry' by default
143 public function loadObjectRegistryTemplate ($template) {
145 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('object_registry_template_type'));
147 // Load the special template
148 $this->loadTemplate($template);
152 * Getter for current main node
154 * @return $currMainNode Current main node
156 public final function getCurrMainNode () {
157 return $this->curr['main_node'];
161 * Setter for current main node
163 * @param $element Element name to set as current main node
164 * @return $currMainNode Current main node
166 private final function setCurrMainNode ($element) {
167 $this->curr['main_node'] = (string) $element;
171 * Getter for main node array
173 * @return $mainNodes Array with valid main node names
175 public final function getMainNodes () {
176 return $this->mainNodes;
180 * Getter for sub node array
182 * @return $subNodes Array with valid sub node names
184 public final function getSubNodes () {
185 return $this->subNodes;
189 * Handles the start element of an XML resource
191 * @param $resource XML parser resource (currently ignored)
192 * @param $element The element we shall handle
193 * @param $attributes All attributes
195 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
197 public function startElement ($resource, $element, array $attributes) {
198 // Initial method name which will never be called...
199 $methodName = 'initObjectRegistry';
201 // Make the element name lower-case
202 $element = strtolower($element);
204 // Is the element a main node?
205 //* DEBUG: */ echo "START: >".$element."<<br />\n";
206 if (in_array($element, $this->getMainNodes())) {
207 // Okay, main node found!
208 $methodName = 'start' . $this->convertToClassName($element);
211 $this->setCurrMainNode($element);
212 } elseif (in_array($element, $this->getSubNodes())) {
214 $methodName = 'start' . $this->convertToClassName($element);
216 // Invalid node name found
217 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
221 call_user_func_array(array($this, $methodName), $attributes);
225 * Ends the main or sub node by sending out the gathered data
227 * @param $resource An XML resource pointer (currently ignored)
228 * @param $nodeName Name of the node we want to finish
230 * @throws XmlNodeMismatchException If current main node mismatches the closing one
232 public function endElement ($resource, $nodeName) {
233 // Make all lower-case
234 $nodeName = strtolower($nodeName);
236 // Does this match with current main node?
237 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
238 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
240 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
243 // Construct method name
244 $methodName = 'finish' . $this->convertToClassName($nodeName);
246 // Call the corresponding method
247 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
248 call_user_func_array(array($this, $methodName), array());
254 * @param $resource XML parser resource (currently ignored)
255 * @param $characters Characters to handle
257 * @todo Find something useful with this!
259 public function characterHandler ($resource, $characters) {
260 // Trim all spaces away
261 $characters = trim($characters);
263 // Is this string empty?
264 if (empty($characters)) {
265 // Then skip it silently
269 // Get current XML node name as an array index
270 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
272 // Is the node name 'object-name'?
273 if ($nodeName == 'object-name') {
274 // Output debug message
275 $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
278 // Add it to the registry
279 $this->objectRegistryInstance->addEntry($nodeName, $characters);
283 * Handles the template dependency for given node
285 * @param $node The node we should load a dependency template
286 * @param $templateDependency A template to load to satisfy dependencies
289 private function handleTemplateDependency ($node, $templateDependency) {
290 // Is the template dependency set?
291 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
292 // Get a temporay menu template instance
293 $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($this->getApplicationInstance()));
296 $templateInstance->loadObjectRegistryTemplate($templateDependency);
298 // Get an XmlParser instance
299 $templateInstance->renderXmlContent();
301 // Parse the template's content contents
302 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
307 * Getter for cache file (FQFN)
309 * @return $fqfn Full-qualified file name of the menu cache
311 public function getObjectRegistryCacheFqfn () {
312 $this->partialStub('Please implement this method.');
316 * Starts the object-registry
320 private function startObjectRegistry () {
321 // Push the node name on the stacker
322 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
326 * Starts the object-list
330 private function startObjectList () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
336 * Starts the object-list-entry
340 private function startObjectListEntry () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
346 * Starts the object-name
350 private function startObjectName () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
356 * Starts the object-recipient-limitation
360 private function startObjectRecipientLimitation () {
361 // Push the node name on the stacker
362 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
366 * Starts the object-max-spread
370 private function startObjectMaxSpread () {
371 // Push the node name on the stacker
372 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
376 * Starts the object-protocol
380 private function startObjectProtocol () {
381 // Push the node name on the stacker
382 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
386 * Starts the object-recipient-type
390 private function startObjectRecipientType () {
391 // Push the node name on the stacker
392 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-type');
396 * Finishes the object-recipient-type
400 private function finishObjectRecipientType () {
401 // Pop the last entry
402 $this->getStackerInstance()->popNamed('object_registry');
406 * Finishes the object-protocol
410 private function finishObjectProtocol () {
411 // Pop the last entry
412 $this->getStackerInstance()->popNamed('object_registry');
416 * Finishes the object-max-spread
420 private function finishObjectMaxSpread () {
421 // Pop the last entry
422 $this->getStackerInstance()->popNamed('object_registry');
426 * Finishes the object-recipient-limitation
430 private function finishObjectRecipientLimitation () {
431 // Pop the last entry
432 $this->getStackerInstance()->popNamed('object_registry');
436 * Finishes the object-name
440 private function finishObjectName () {
441 // Pop the last entry
442 $this->getStackerInstance()->popNamed('object_registry');
446 * Finishes the object-list-entry
450 private function finishObjectListEntry () {
451 // Pop the last entry
452 $this->getStackerInstance()->popNamed('object_registry');
456 * Finishes the object-list
460 private function finishObjectList () {
461 // Pop the last entry
462 $this->getStackerInstance()->popNamed('object_registry');
466 * Finishes the object-registry
470 private function finishObjectRegistry () {
471 // Pop the last entry
472 $this->getStackerInstance()->popNamed('object_registry');