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 - 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 die('YOU NEED TO REPLACE ???, !!! AND ||| IN THIS CLASS BEFORE YOU CAN USE IT.');
26 class Xml???TemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
28 * Main nodes in the XML tree
30 private $mainNodes = array(
34 * Sub nodes in the XML tree
36 private $subNodes = array(
42 private $curr = array();
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates an instance of the class TemplateEngine and prepares it for usage
57 * @param $applicationInstance A manageable application
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 (ManageableApplication $applicationInstance) {
68 $templateInstance = new Xml???TemplateEngine();
70 // Determine base path
71 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
73 // Is the base path valid?
74 if (empty($templateBasePath)) {
76 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77 } elseif (!is_string($templateBasePath)) {
79 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
80 } elseif (!is_dir($templateBasePath)) {
82 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
83 } elseif (!is_readable($templateBasePath)) {
85 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
89 $templateInstance->setTemplateBasePath($templateBasePath);
91 // Set template extensions
92 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
93 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('!!!_template_extension'));
95 // Absolute output path for compiled templates
96 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
98 // Init a variable stacker
99 $stackerInstance = ObjectFactory::createObjectByConfiguredName('!!!_stacker_class');
102 $templateInstance->setStackerInstance($stackerInstance);
104 // Return the prepared instance
105 return $templateInstance;
109 * Load a specified !!! template into the engine
111 * @param $template The !!! template we shall load which is
112 * located in '!!!' by default
115 public function load???Template ($template) {
117 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('!!!_template_type'));
119 // Load the special template
120 $this->loadTemplate($template);
124 * Getter for current main node
126 * @return $currMainNode Current main node
128 public final function getCurrMainNode () {
129 return $this->curr['main_node'];
133 * Setter for current main node
135 * @param $element Element name to set as current main node
136 * @return $currMainNode Current main node
138 private final function setCurrMainNode ($element) {
139 $this->curr['main_node'] = (string) $element;
143 * Getter for main node array
145 * @return $mainNodes Array with valid main node names
147 public final function getMainNodes () {
148 return $this->mainNodes;
152 * Getter for sub node array
154 * @return $subNodes Array with valid sub node names
156 public final function getSubNodes () {
157 return $this->subNodes;
161 * Handles the start element of an XML resource
163 * @param $resource XML parser resource (currently ignored)
164 * @param $element The element we shall handle
165 * @param $attributes All attributes
167 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
169 public function startElement ($resource, $element, array $attributes) {
170 // Initial method name which will never be called...
171 $methodName = 'init???';
173 // Make the element name lower-case
174 $element = strtolower($element);
176 // Is the element a main node?
177 //* DEBUG: */ echo "START: >".$element."<<br />\n";
178 if (in_array($element, $this->getMainNodes())) {
179 // Okay, main node found!
180 $methodName = 'start' . $this->convertToClassName($element);
183 $this->setCurrMainNode($element);
184 } elseif (in_array($element, $this->getSubNodes())) {
186 $methodName = 'start' . $this->convertToClassName($element);
188 // Invalid node name found
189 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
193 call_user_func_array(array($this, $methodName), $attributes);
197 * Ends the main or sub node by sending out the gathered data
199 * @param $resource An XML resource pointer (currently ignored)
200 * @param $nodeName Name of the node we want to finish
202 * @throws XmlNodeMismatchException If current main node mismatches the closing one
204 public function endElement ($resource, $nodeName) {
205 // Make all lower-case
206 $nodeName = strtolower($nodeName);
208 // Does this match with current main node?
209 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
210 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
212 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
215 // Construct method name
216 $methodName = 'finish' . $this->convertToClassName($nodeName);
218 // Call the corresponding method
219 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
220 call_user_func_array(array($this, $methodName), array());
226 * @param $resource XML parser resource (currently ignored)
227 * @param $characters Characters to handle
229 * @todo Find something useful with this!
231 public function characterHandler ($resource, $characters) {
232 // Trim all spaces away
233 $characters = trim($characters);
235 // Is this string empty?
236 if (empty($characters)) {
237 // Then skip it silently
241 // Get current XML node name as an array index
242 $nodeName = $this->getStackerInstance()->getNamed('!!!');
244 $this->partialStub('Do something with the gathered data.');
248 * Getter for cache file (FQFN)
250 * @return $fqfn Full-qualified file name of the menu cache
252 public function get???CacheFqfn () {
253 $this->partialStub('Please implement this method.');
261 private function start??? () {
262 // Push the node name on the stacker
263 $this->getStackerInstance()->pushNamed('!!!', '|||');
271 private function finish??? () {
272 // Pop the last entry
273 $this->getStackerInstance()->popNamed('!!!');