3 * An ObjectRegistry template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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',
53 private $curr = array();
56 * Content from dependency
58 private $dependencyContent = array();
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
69 // Init object type registry instance
70 $this->objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('object_type_registry_class');
74 * Creates an instance of the class TemplateEngine and prepares it for usage
76 * @param $appInstance A manageable application
77 * @return $templateInstance An instance of TemplateEngine
78 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
79 * @throws InvalidBasePathStringException If $templateBasePath is no string
80 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
81 * directory or not found
82 * @throws BasePathReadProtectedException If $templateBasePath is
85 public final static function createObjectRegistryTemplateEngine (ManageableApplication $appInstance) {
87 $templateInstance = new ObjectRegistryTemplateEngine();
89 // Get language and file I/O instances from application
90 $langInstance = $appInstance->getLanguageInstance();
91 $ioInstance = $appInstance->getFileIoInstance();
93 // Determine base path
94 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
96 // Is the base path valid?
97 if (empty($templateBasePath)) {
99 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
100 } elseif (!is_string($templateBasePath)) {
102 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
103 } elseif (!is_dir($templateBasePath)) {
105 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
106 } elseif (!is_readable($templateBasePath)) {
108 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
112 $templateInstance->setTemplateBasePath($templateBasePath);
114 // Set the language and IO instances
115 $templateInstance->setLanguageInstance($langInstance);
116 $templateInstance->setFileIoInstance($ioInstance);
118 // Set template extensions
119 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
120 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('object_registry_template_extension'));
122 // Absolute output path for compiled templates
123 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
125 // Init a variable stacker
126 $stackerInstance = ObjectFactory::createObjectByConfiguredName('object_registry_stacker_class');
129 $templateInstance->setStackerInstance($stackerInstance);
131 // Return the prepared instance
132 return $templateInstance;
136 * Load a specified object_registry template into the engine
138 * @param $template The object_registry template we shall load which is
139 * located in 'object_registry' by default
142 public function loadObjectRegistryTemplate ($template) {
144 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('object_registry_template_type'));
146 // Load the special template
147 $this->loadTemplate($template);
151 * Getter for current main node
153 * @return $currMainNode Current main node
155 public final function getCurrMainNode () {
156 return $this->curr['main_node'];
160 * Setter for current main node
162 * @param $element Element name to set as current main node
163 * @return $currMainNode Current main node
165 private final function setCurrMainNode ($element) {
166 $this->curr['main_node'] = (string) $element;
170 * Getter for main node array
172 * @return $mainNodes Array with valid main node names
174 public final function getMainNodes () {
175 return $this->mainNodes;
179 * Getter for sub node array
181 * @return $subNodes Array with valid sub node names
183 public final function getSubNodes () {
184 return $this->subNodes;
188 * Handles the start element of an XML resource
190 * @param $resource XML parser resource (currently ignored)
191 * @param $element The element we shall handle
192 * @param $attributes All attributes
194 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
196 public function startElement ($resource, $element, array $attributes) {
197 // Initial method name which will never be called...
198 $methodName = 'initObjectRegistry';
200 // Make the element name lower-case
201 $element = strtolower($element);
203 // Is the element a main node?
204 //* DEBUG: */ echo "START: >".$element."<<br />\n";
205 if (in_array($element, $this->getMainNodes())) {
206 // Okay, main node found!
207 $methodName = 'start' . $this->convertToClassName($element);
210 $this->setCurrMainNode($element);
211 } elseif (in_array($element, $this->getSubNodes())) {
213 $methodName = 'start' . $this->convertToClassName($element);
215 // Invalid node name found
216 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
220 call_user_func_array(array($this, $methodName), $attributes);
224 * Ends the main or sub node by sending out the gathered data
226 * @param $resource An XML resource pointer (currently ignored)
227 * @param $nodeName Name of the node we want to finish
229 * @throws XmlNodeMismatchException If current main node mismatches the closing one
231 public function endElement ($resource, $nodeName) {
232 // Make all lower-case
233 $nodeName = strtolower($nodeName);
235 // Does this match with current main node?
236 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
237 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
239 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
242 // Construct method name
243 $methodName = 'finish' . $this->convertToClassName($nodeName);
245 // Call the corresponding method
246 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
247 call_user_func_array(array($this, $methodName), array());
253 * @param $resource XML parser resource (currently ignored)
254 * @param $characters Characters to handle
256 * @todo Find something useful with this!
258 public function characterHandler ($resource, $characters) {
259 // Trim all spaces away
260 $characters = trim($characters);
262 // Is this string empty?
263 if (empty($characters)) {
264 // Then skip it silently
268 // Get current XML node name as an array index
269 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
271 // Is the node name 'object-name'?
272 if ($nodeName == 'object-name') {
273 // Output debug message
274 $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
277 // Add it to the registry
278 $this->objectRegistryInstance->addEntry($nodeName, $characters);
282 * Handles the template dependency for given node
284 * @param $node The node we should load a dependency template
285 * @param $templateDependency A template to load to satisfy dependencies
288 private function handleTemplateDependency ($node, $templateDependency) {
289 // Is the template dependency set?
290 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
291 // Get a temporay menu template instance
292 $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($this->getApplicationInstance()));
295 $templateInstance->loadObjectRegistryTemplate($templateDependency);
297 // Get an XmlParser instance
298 $templateInstance->renderXmlContent();
300 // Parse the template's content contents
301 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
306 * Getter for cache file (FQFN)
308 * @return $fqfn Full-qualified file name of the menu cache
310 public function getObjectRegistryCacheFqfn () {
311 $this->partialStub('Please implement this method.');
315 * Starts the object-registry
319 private function startObjectRegistry () {
320 // Push the node name on the stacker
321 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
325 * Starts the object-list
329 private function startObjectList () {
330 // Push the node name on the stacker
331 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
335 * Starts the object-list-entry
339 private function startObjectListEntry () {
340 // Push the node name on the stacker
341 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
345 * Starts the object-name
349 private function startObjectName () {
350 // Push the node name on the stacker
351 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
355 * Starts the object-recipient-limitation
359 private function startObjectRecipientLimitation () {
360 // Push the node name on the stacker
361 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
365 * Starts the object-max-spread
369 private function startObjectMaxSpread () {
370 // Push the node name on the stacker
371 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
375 * Starts the object-protocol
379 private function startObjectProtocol () {
380 // Push the node name on the stacker
381 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
385 * Finishes the object-protocol
389 private function finishObjectProtocol () {
390 // Pop the last entry
391 $this->getStackerInstance()->popNamed('object_registry');
395 * Finishes the object-max-spread
399 private function finishObjectMaxSpread () {
400 // Pop the last entry
401 $this->getStackerInstance()->popNamed('object_registry');
405 * Finishes the object-recipient-limitation
409 private function finishObjectRecipientLimitation () {
410 // Pop the last entry
411 $this->getStackerInstance()->popNamed('object_registry');
415 * Finishes the object-name
419 private function finishObjectName () {
420 // Pop the last entry
421 $this->getStackerInstance()->popNamed('object_registry');
425 * Finishes the object-list-entry
429 private function finishObjectListEntry () {
430 // Pop the last entry
431 $this->getStackerInstance()->popNamed('object_registry');
435 * Finishes the object-list
439 private function finishObjectList () {
440 // Pop the last entry
441 $this->getStackerInstance()->popNamed('object_registry');
445 * Finishes the object-registry
449 private function finishObjectRegistry () {
450 // Pop the last entry
451 $this->getStackerInstance()->popNamed('object_registry');