3 * A decorator for XML template engines which rewrites the XML for compacting
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
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 XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
27 * Protected constructor
31 protected function __construct () {
32 // Call parent constructor
33 parent::__construct(__CLASS__);
37 * Creates an instance of the class TemplateEngine and prepares it for usage
39 * @param $innerTemplateInstance A CompileableTemplate instance
40 * @return $templateInstance An instance of TemplateEngine
42 public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
44 $templateInstance = new XmlRewriterTemplateDecorator();
46 // Set the inner template engine
47 $templateInstance->setTemplateInstance($innerTemplateInstance);
49 // Return the prepared instance
50 return $templateInstance;
54 * Settter for variable group
56 * @param $groupName Name of variable group
57 * @param $add Whether add this group
60 public function setVariableGroup ($groupName, $add = true) {
61 // Call the inner class' method
62 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
66 * Adds a variable to current group
68 * @param $var Variable to set
69 * @param $value Value to store in variable
72 public function addGroupVariable ($var, $value) {
73 // Call the inner class' method
74 $this->getTemplateInstance()->addGroupVariable($var, $value);
78 * Setter for base path
80 * @param $templateBasePath The relative base path for all templates
83 public final function setTemplateBasePath ($templateBasePath) {
84 // Call the inner class' method
85 $this->getTemplateInstance()->setTemplateBasePath($templateBasePath);
89 * Getter for base path
91 * @return $templateBasePath The relative base path for all templates
93 public final function getTemplateBasePath () {
94 // Call the inner class' method
95 return $this->getTemplateInstance()->getTemplateBasePath();
99 * Getter for generic base path
101 * @return $templateBasePath The relative base path for all templates
103 public final function getGenericBasePath () {
104 // Call the inner class' method
105 return $this->getTemplateInstance()->getGenericBasePath();
109 * Setter for template extension
111 * @param $templateExtension The file extension for all uncompiled templates
114 public final function setRawTemplateExtension ($templateExtension) {
115 // Call the inner class' method
116 $this->getTemplateInstance()->setRawTemplateExtension($templateExtension);
120 * Setter for code template extension
122 * @param $codeExtension The file extension for all uncompiled templates
125 public final function setCodeTemplateExtension ($codeExtension) {
126 // Call the inner class' method
127 $this->getTemplateInstance()->setCodeTemplateExtension($codeExtension);
131 * Getter for template extension
133 * @return $templateExtension The file extension for all uncompiled templates
135 public final function getRawTemplateExtension () {
136 // Call the inner class' method
137 return $this->getTemplateInstance()->getRawTemplateExtension();
141 * Getter for code-template extension
143 * @return $codeExtension The file extension for all code templates
145 public final function getCodeTemplateExtension () {
146 // Call the inner class' method
147 return $this->getTemplateInstance()->getCodeTemplateExtension();
151 * Setter for path of compiled templates
153 * @param $compileOutputPath The local base path for all compiled templates
156 public final function setCompileOutputPath ($compileOutputPath) {
157 // Call the inner class' method
158 $this->getTemplateInstance()->setCompileOutputPath($compileOutputPath);
162 * Getter for template type
164 * @return $templateType The current template's type
166 public final function getTemplateType () {
167 // Call the inner class' method
168 return $this->getTemplateInstance()->getTemplateType();
172 * Assign (add) a given variable with a value
174 * @param $var The variable we are looking for
175 * @param $value The value we want to store in the variable
177 * @throws EmptyVariableException If the variable name is left empty
179 public final function assignVariable ($var, $value) {
180 // Call the inner class' method
181 $this->getTemplateInstance()->assignVariable($var, $value);
185 * Removes a given variable
187 * @param $var The variable we are looking for
190 public final function removeVariable ($var) {
191 // Call the inner class' method
192 $this->getTemplateInstance()->removeVariable($var);
196 * Getter for compiled templates
198 * @return $compiledData Compiled template data
200 public final function getCompiledData () {
201 // Call the inner class' method
202 return $this->getTemplateInstance()->getCompiledData();
206 * Load a specified web template into the engine
208 * @param $template The web template we shall load which is located in
212 public function loadWebTemplate ($template) {
213 // Call the inner class' method
214 $this->getTemplateInstance()->loadWebTemplate($template);
218 * Assign a given congfiguration variable with a value
220 * @param $var The configuration variable we want to assign
223 public function assignConfigVariable ($var) {
224 // Call the inner class' method
225 $this->getTemplateInstance()->assignConfigVariable($var);
229 * Load a specified code template into the engine
231 * @param $template The code template we shall load which is
232 * located in 'code' by default
235 public function loadCodeTemplate ($template) {
236 // Call the inner class' method
237 $this->getTemplateInstance()->loadCodeTemplate($template);
241 * Compiles configuration place-holders in all variables. This 'walks'
242 * through the variable stack 'general'. It interprets all values from that
243 * variables as configuration entries after compiling them.
247 public final function compileConfigInVariables () {
248 // Call the inner class' method
249 $this->getTemplateInstance()->compileConfigInVariables();
253 * Compile all variables by inserting their respective values
257 public final function compileVariables () {
258 // Call the inner class' method
259 $this->getTemplateInstance()->compileVariables();
263 * Compile all required templates into the current loaded one
267 public function compileTemplate () {
268 // Call the inner class' method
269 $this->getTemplateInstance()->compileTemplate();
273 * Assigns the last loaded raw template content with a given variable
275 * @param $templateName Name of the template we want to assign
276 * @param $variableName Name of the variable we want to assign
279 public function assignTemplateWithVariable ($templateName, $variableName) {
280 // Call the inner class' method
281 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
285 * Transfers the content of this template engine to a given response instance
287 * @param $responseInstance An instance of a response class
290 public function transferToResponse (Responseable $responseInstance) {
291 // Call the inner class' method
292 $this->getTemplateInstance()->transportToResponse($responseInstance);
296 * Assigns all the application data with template variables
298 * @param $applicationInstance A manageable application instance
301 public function assignApplicationData (ManageableApplication $applicationInstance) {
302 // Call the inner class' method
303 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
307 * "Compiles" a variable by replacing {?var?} with it's content
309 * @param $rawCode Raw code to compile
310 * @param $setMatchAsCode Sets $match if readVariable() returns empty result
311 * @return $rawCode Compile code with inserted variable value
313 public function compileRawCode ($rawCode, $setMatchAsCode=false) {
314 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
318 * Getter for variable group array
320 * @return $vargroups All variable groups
322 public final function getVariableGroups () {
323 // Call the inner class' method
324 return $this->getTemplateInstance()->getVariableGroups();
328 * Renames a variable in code and in stack
330 * @param $oldName Old name of variable
331 * @param $newName New name of variable
334 public function renameVariable ($oldName, $newName) {
335 // Call the inner class' method
336 $this->getTemplateInstance()->renameVariable($oldName, $newName);
340 * Renders the given XML content
342 * @param $content Valid XML content or if not set the current loaded raw content
344 * @throws XmlParserException If an XML error was found
346 public function renderXmlContent ($content = NULL) {
347 // Call the inner class' method
348 $this->getTemplateInstance()->renderXmlContent($content);
352 * Enables or disables language support
354 * @param $languageSupport New language support setting
357 public final function enableLanguageSupport ($languageSupport = true) {
358 // Call the inner class' method
359 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
363 * Checks whether language support is enabled
365 * @return $languageSupport Whether language support is enabled or disabled
367 public final function isLanguageSupportEnabled () {
368 // Call the inner class' method
369 return $this->getTemplateInstance()->isLanguageSupportEnabled();
373 * Enables or disables XML compacting
375 * @param $xmlCompacting New XML compacting setting
378 public final function enableXmlCompacting ($xmlCompacting = true) {
379 // Call the inner class' method
380 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
384 * Checks whether XML compacting is enabled
386 * @return $xmlCompacting Whether XML compacting is enabled or disabled
388 public final function isXmlCompactingEnabled () {
389 // Call the inner class' method
390 return $this->getTemplateInstance()->isXmlCompactingEnabled();
394 * Handles the start element of an XML resource
396 * @param $resource XML parser resource (currently ignored)
397 * @param $element The element we shall handle
398 * @param $attributes All attributes
400 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
402 public function startElement ($resource, $element, array $attributes) {
403 // Call the inner class' method
404 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
408 * Ends the main or sub node by sending out the gathered data
410 * @param $resource An XML resource pointer (currently ignored)
411 * @param $nodeName Name of the node we want to finish
413 * @throws XmlNodeMismatchException If current main node mismatches the closing one
415 public function endElement ($resource, $nodeName) {
416 // Call the inner class' method
417 $this->getTemplateInstance()->endElement($resource, $nodeName);
423 * @param $resource XML parser resource (currently ignored)
424 * @param $characters Characters to handle
426 * @todo Find something useful with this!
428 public function characterHandler ($resource, $characters) {
429 // Call the inner class' method but trim the characters before
430 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
434 * Removes all comments, tabs and new-line charcters to compact the content
436 * @param $uncompactedContent The uncompacted content
437 * @return $compactedContent The compacted content
439 public function compactContent ($uncompactedContent) {
441 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
444 return $compactedContent;