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 - 2012 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, Registerable {
27 const OBJECT_TYPE_DATA_NAME = 'object-name';
28 const OBJECT_TYPE_DATA_RECIPIENT_LIMITATION = 'object-recipient-limitation';
29 const OBJECT_TYPE_DATA_MAX_SPREAD = 'object-max-spread';
30 const OBJECT_TYPE_DATA_PROTOCOL = 'object-protocol';
31 const OBJECT_TYPE_DATA_RECIPIENT_TYPE = 'object-recipient-type';
34 * Instance for the object registry
36 private $objectRegistryInstance = NULL;
39 * Main nodes in the XML tree
41 private $mainNodes = array(
46 * Sub nodes in the XML tree
48 private $subNodes = array(
51 self::OBJECT_TYPE_DATA_NAME,
52 self::OBJECT_TYPE_DATA_RECIPIENT_LIMITATION,
53 self::OBJECT_TYPE_DATA_MAX_SPREAD,
54 self::OBJECT_TYPE_DATA_PROTOCOL,
55 self::OBJECT_TYPE_DATA_RECIPIENT_TYPE
61 private $curr = array();
64 * Content from dependency
66 private $dependencyContent = array();
69 * Protected constructor
73 protected function __construct () {
74 // Call parent constructor
75 parent::__construct(__CLASS__);
77 // Init object type registry instance
78 $this->objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance();
82 * Creates an instance of the class TemplateEngine and prepares it for usage
84 * @return $templateInstance An instance of TemplateEngine
85 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
86 * @throws InvalidBasePathStringException If $templateBasePath is no string
87 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
88 * directory or not found
89 * @throws BasePathReadProtectedException If $templateBasePath is
92 public static final function createXmlObjectRegistryTemplateEngine () {
94 $templateInstance = new XmlObjectRegistryTemplateEngine();
96 // Get the application instance from registry
97 $applicationInstance = Registry::getRegistry()->getInstance('app');
99 // Determine base path
100 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
102 // Is the base path valid?
103 if (empty($templateBasePath)) {
104 // Base path is empty
105 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
106 } elseif (!is_string($templateBasePath)) {
108 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
109 } elseif (!is_dir($templateBasePath)) {
111 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
112 } elseif (!is_readable($templateBasePath)) {
114 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
118 $templateInstance->setTemplateBasePath($templateBasePath);
120 // Set template extensions
121 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
122 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_object_registry_template_extension'));
124 // Absolute output path for compiled templates
125 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
127 // Init a variable stacker
128 $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_object_registry_stacker_class');
131 $templateInstance->setStackerInstance($stackerInstance);
133 // Return the prepared instance
134 return $templateInstance;
138 * Load a specified object_registry template into the engine
140 * @param $template The object_registry template we shall load which is
141 * located in 'object_registry' by default
144 public function loadObjectRegistryTemplate ($template) {
146 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_object_registry_template_type'));
148 // Load the special template
149 $this->loadTemplate($template);
153 * Getter for current main node
155 * @return $currMainNode Current main node
157 public final function getCurrMainNode () {
158 return $this->curr['main_node'];
162 * Setter for current main node
164 * @param $element Element name to set as current main node
165 * @return $currMainNode Current main node
167 private final function setCurrMainNode ($element) {
168 $this->curr['main_node'] = (string) $element;
172 * Getter for main node array
174 * @return $mainNodes Array with valid main node names
176 public final function getMainNodes () {
177 return $this->mainNodes;
181 * Getter for sub node array
183 * @return $subNodes Array with valid sub node names
185 public final function getSubNodes () {
186 return $this->subNodes;
190 * Handles the start element of an XML resource
192 * @param $resource XML parser resource (currently ignored)
193 * @param $element The element we shall handle
194 * @param $attributes All attributes
196 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
198 public function startElement ($resource, $element, array $attributes) {
199 // Initial method name which will never be called...
200 $methodName = 'initObjectRegistry';
202 // Make the element name lower-case
203 $element = strtolower($element);
205 // Is the element a main node?
206 //* DEBUG: */ echo "START: >".$element."<<br />\n";
207 if (in_array($element, $this->getMainNodes())) {
208 // Okay, main node found!
209 $methodName = 'start' . $this->convertToClassName($element);
212 $this->setCurrMainNode($element);
213 } elseif (in_array($element, $this->getSubNodes())) {
215 $methodName = 'start' . $this->convertToClassName($element);
217 // Invalid node name found
218 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
222 call_user_func_array(array($this, $methodName), $attributes);
226 * Ends the main or sub node by sending out the gathered data
228 * @param $resource An XML resource pointer (currently ignored)
229 * @param $nodeName Name of the node we want to finish
231 * @throws XmlNodeMismatchException If current main node mismatches the closing one
233 public function endElement ($resource, $nodeName) {
234 // Make all lower-case
235 $nodeName = strtolower($nodeName);
237 // Does this match with current main node?
238 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
239 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
241 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
244 // Construct method name
245 $methodName = 'finish' . $this->convertToClassName($nodeName);
247 // Call the corresponding method
248 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
249 call_user_func_array(array($this, $methodName), array());
255 * @param $resource XML parser resource (currently ignored)
256 * @param $characters Characters to handle
258 * @todo Find something useful with this!
260 public function characterHandler ($resource, $characters) {
261 // Trim all spaces away
262 $characters = trim($characters);
264 // Is this string empty?
265 if (empty($characters)) {
266 // Then skip it silently
270 // Get current XML node name as an array index
271 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
273 // Is the node name self::OBJECT_TYPE_DATA_NAME?
274 if ($nodeName == self::OBJECT_TYPE_DATA_NAME) {
275 // Output debug message
276 self::createDebugInstance(__CLASS__)->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
279 // Add it to the registry
280 $this->objectRegistryInstance->addEntry($nodeName, $characters);
284 * Handles the template dependency for given node
286 * @param $node The node we should load a dependency template
287 * @param $templateDependency A template to load to satisfy dependencies
290 private function handleTemplateDependency ($node, $templateDependency) {
291 // Is the template dependency set?
292 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
293 // Get a temporay template instance
294 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_object_registry_template_class');
297 $templateInstance->loadObjectRegistryTemplate($templateDependency);
299 // Parse the XML content
300 $templateInstance->renderXmlContent();
302 // Save the parsed raw content in our dependency array
303 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
308 * Getter for cache file (FQFN)
310 * @return $fqfn Full-qualified file name of the menu cache
312 public function getObjectRegistryCacheFqfn () {
313 $this->partialStub('Please implement this method.');
317 * Starts the object-registry
321 private function startObjectRegistry () {
322 // Push the node name on the stacker
323 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
327 * Starts the object-list
329 * @param $objectCount Count of all objects
331 * @todo Handle $objectCount
333 private function startObjectList ($objectCount) {
334 // Push the node name on the stacker
335 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
339 * Starts the object-list-entry
343 private function startObjectListEntry () {
344 // Push the node name on the stacker
345 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
349 * Starts the object-name
353 private function startObjectName () {
354 // Push the node name on the stacker
355 $this->getStackerInstance()->pushNamed('object_registry', self::OBJECT_TYPE_DATA_NAME);
359 * Starts the object-recipient-limitation
363 private function startObjectRecipientLimitation () {
364 // Push the node name on the stacker
365 $this->getStackerInstance()->pushNamed('object_registry', self::OBJECT_TYPE_DATA_RECIPIENT_LIMITATION);
369 * Starts the object-max-spread
373 private function startObjectMaxSpread () {
374 // Push the node name on the stacker
375 $this->getStackerInstance()->pushNamed('object_registry', self::OBJECT_TYPE_DATA_MAX_SPREAD);
379 * Starts the object-protocol
383 private function startObjectProtocol () {
384 // Push the node name on the stacker
385 $this->getStackerInstance()->pushNamed('object_registry', self::OBJECT_TYPE_DATA_PROTOCOL);
389 * Starts the object-recipient-type
393 private function startObjectRecipientType () {
394 // Push the node name on the stacker
395 $this->getStackerInstance()->pushNamed('object_registry', self::OBJECT_TYPE_DATA_RECIPIENT_TYPE);
399 * Finishes the object-recipient-type
403 private function finishObjectRecipientType () {
404 // Pop the last entry
405 $this->getStackerInstance()->popNamed('object_registry');
409 * Finishes the object-protocol
413 private function finishObjectProtocol () {
414 // Pop the last entry
415 $this->getStackerInstance()->popNamed('object_registry');
419 * Finishes the object-max-spread
423 private function finishObjectMaxSpread () {
424 // Pop the last entry
425 $this->getStackerInstance()->popNamed('object_registry');
429 * Finishes the object-recipient-limitation
433 private function finishObjectRecipientLimitation () {
434 // Pop the last entry
435 $this->getStackerInstance()->popNamed('object_registry');
439 * Finishes the object-name
443 private function finishObjectName () {
444 // Pop the last entry
445 $this->getStackerInstance()->popNamed('object_registry');
449 * Finishes the object-list-entry
453 private function finishObjectListEntry () {
454 // Pop the last entry
455 $this->getStackerInstance()->popNamed('object_registry');
459 * Finishes the object-list
463 private function finishObjectList () {
464 // Pop the last entry
465 $this->getStackerInstance()->popNamed('object_registry');
469 * Finishes the object-registry
473 private function finishObjectRegistry () {
474 // Pop the last entry
475 $this->getStackerInstance()->popNamed('object_registry');