3 * An ??? 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 die('YOU NEED TO REPLACE ???, !!! AND ||| IN THIS CLASS BEFORE YOU CAN USE IT.');
26 class Xml???TemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
28 * Main nodes in the XML tree
30 private $mainNodes = array(
35 * Sub nodes in the XML tree
37 private $subNodes = array(
43 private $curr = array();
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of the class TemplateEngine and prepares it for usage
58 * @return $templateInstance An instance of TemplateEngine
59 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
60 * @throws InvalidBasePathStringException If $templateBasePath is no string
61 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
62 * directory or not found
63 * @throws BasePathReadProtectedException If $templateBasePath is
66 public static final function createXml???TemplateEngine () {
68 $templateInstance = new Xml???TemplateEngine();
70 // Get application instance from registry
71 $applicationInstance = Registry::getRegistry()->getInstance('app');
73 // Determine base path
74 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
76 // Is the base path valid?
77 if (empty($templateBasePath)) {
79 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
80 } elseif (!is_string($templateBasePath)) {
82 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
83 } elseif (!is_dir($templateBasePath)) {
85 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
86 } elseif (!is_readable($templateBasePath)) {
88 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
92 $templateInstance->setTemplateBasePath($templateBasePath);
94 // Set template extensions
95 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
96 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_message_template_extension'));
98 // Absolute output path for compiled templates
99 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
101 // Init a variable stacker
102 $stackerInstance = ObjectFactory::createObjectByConfiguredName('!!!_stacker_class');
105 $templateInstance->setStackerInstance($stackerInstance);
107 // Return the prepared instance
108 return $templateInstance;
112 * Load a specified !!! template into the engine
114 * @param $template The !!! template we shall load which is
115 * located in '!!!' by default
118 public function load???Template ($template) {
120 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('!!!_template_type'));
122 // Load the special template
123 $this->loadTemplate($template);
127 * Getter for current main node
129 * @return $currMainNode Current main node
131 public final function getCurrMainNode () {
132 return $this->curr['main_node'];
136 * Setter for current main node
138 * @param $element Element name to set as current main node
139 * @return $currMainNode Current main node
141 private final function setCurrMainNode ($element) {
142 $this->curr['main_node'] = (string) $element;
146 * Getter for main node array
148 * @return $mainNodes Array with valid main node names
150 public final function getMainNodes () {
151 return $this->mainNodes;
155 * Getter for sub node array
157 * @return $subNodes Array with valid sub node names
159 public final function getSubNodes () {
160 return $this->subNodes;
164 * Handles the start element of an XML resource
166 * @param $resource XML parser resource (currently ignored)
167 * @param $element The element we shall handle
168 * @param $attributes All attributes
170 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
172 public function startElement ($resource, $element, array $attributes) {
173 // Initial method name which will never be called...
174 $methodName = 'init???';
176 // Make the element name lower-case
177 $element = strtolower($element);
179 // Is the element a main node?
180 //* DEBUG: */ echo "START: >".$element."<<br />\n";
181 if (in_array($element, $this->getMainNodes())) {
182 // Okay, main node found!
183 $methodName = 'start' . $this->convertToClassName($element);
186 $this->setCurrMainNode($element);
187 } elseif (in_array($element, $this->getSubNodes())) {
189 $methodName = 'start' . $this->convertToClassName($element);
191 // Invalid node name found
192 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
196 call_user_func_array(array($this, $methodName), $attributes);
200 * Ends the main or sub node by sending out the gathered data
202 * @param $resource An XML resource pointer (currently ignored)
203 * @param $nodeName Name of the node we want to finish
205 * @throws XmlNodeMismatchException If current main node mismatches the closing one
207 public function endElement ($resource, $nodeName) {
208 // Make all lower-case
209 $nodeName = strtolower($nodeName);
211 // Does this match with current main node?
212 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
213 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
215 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
218 // Construct method name
219 $methodName = 'finish' . $this->convertToClassName($nodeName);
221 // Call the corresponding method
222 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
223 call_user_func_array(array($this, $methodName), array());
229 * @param $resource XML parser resource (currently ignored)
230 * @param $characters Characters to handle
232 * @todo Find something useful with this!
234 public function characterHandler ($resource, $characters) {
235 // Trim all spaces away
236 $characters = trim($characters);
238 // Is this string empty?
239 if (empty($characters)) {
240 // Then skip it silently
244 // Get current XML node name as an array index
245 $nodeName = $this->getStackerInstance()->getNamed('!!!');
247 $this->partialStub('TODO: Do something with the gathered data.');
251 * Getter for cache file (FQFN)
253 * @return $fqfn Full-qualified file name of the menu cache
255 public function get???CacheFqfn () {
256 $this->partialStub('Please implement this method.');
264 private function start??? () {
265 // Push the node name on the stacker
266 $this->getStackerInstance()->pushNamed('!!!', '|||');
274 private function finish??? () {
275 // Pop the last entry
276 $this->getStackerInstance()->popNamed('!!!');