3 * A decorator for XML template engines which rewrites the XML for compacting
6 * @author Roland Haeder <webmaster@shipsimu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.shipsimu.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 * Getter for base path
80 * @return $templateBasePath The relative base path for all templates
82 public final function getTemplateBasePath () {
83 // Call the inner class' method
84 return $this->getTemplateInstance()->getTemplateBasePath();
88 * Getter for generic base path
90 * @return $templateBasePath The relative base path for all templates
92 public final function getGenericBasePath () {
93 // Call the inner class' method
94 return $this->getTemplateInstance()->getGenericBasePath();
98 * Getter for template extension
100 * @return $templateExtension The file extension for all uncompiled templates
102 public final function getRawTemplateExtension () {
103 // Call the inner class' method
104 return $this->getTemplateInstance()->getRawTemplateExtension();
108 * Getter for given variable group
110 * @param $variableGroup Variable group to check
111 * @return $varStack Found variable group
113 public function getVarStack ($variableGroup) {
114 // Call the inner class' method
115 return $this->getTemplateInstance()->getVarStack($variableGroup);
119 * Getter for code-template extension
121 * @return $codeExtension The file extension for all code templates
123 public final function getCodeTemplateExtension () {
124 // Call the inner class' method
125 return $this->getTemplateInstance()->getCodeTemplateExtension();
129 * Getter for template type
131 * @return $templateType The current template's type
133 public final function getTemplateType () {
134 // Call the inner class' method
135 return $this->getTemplateInstance()->getTemplateType();
139 * Assign (add) a given variable with a value
141 * @param $var The variable we are looking for
142 * @param $value The value we want to store in the variable
144 * @throws EmptyVariableException If the variable name is left empty
146 public function assignVariable ($var, $value) {
147 // Call the inner class' method
148 $this->getTemplateInstance()->assignVariable($var, $value);
152 * Removes a given variable
154 * @param $variableName The variable we are looking for
155 * @param $variableGroup Name of variable group (default: 'general')
158 public function removeVariable ($variableName, $variableGroup = 'general') {
159 // Call the inner class' method
160 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
164 * Load a specified HTML template into the engine
166 * @param $template The web template we shall load which is located in
170 public function loadHtmlTemplate ($template) {
171 // Call the inner class' method
172 $this->getTemplateInstance()->loadHtmlTemplate($template);
176 * Assign a given congfiguration variable with a value
178 * @param $variableName The configuration variable we want to assign
181 public function assignConfigVariable ($variableName) {
182 // Call the inner class' method
183 $this->getTemplateInstance()->assignConfigVariable($variableName);
187 * Load a specified code template into the engine
189 * @param $template The code template we shall load which is
190 * located in 'code' by default
193 public function loadCodeTemplate ($template) {
194 // Call the inner class' method
195 $this->getTemplateInstance()->loadCodeTemplate($template);
199 * Load a specified email template into the engine for later compilation
200 * with other code/web/email templates.
202 * @param $template The email template we shall load which is
203 * located in "html" by default
206 public function loadEmailTemplate ($template) {
207 // Call the inner class' method
208 $this->getTemplateInstance()->loadEmailTemplate($template);
212 * Compiles configuration place-holders in all variables. This 'walks'
213 * through the variable stack 'general'. It interprets all values from that
214 * variables as configuration entries after compiling them.
218 public function compileConfigInVariables () {
219 // Call the inner class' method
220 $this->getTemplateInstance()->compileConfigInVariables();
224 * Compile all variables by inserting their respective values
228 public function compileVariables () {
229 // Call the inner class' method
230 $this->getTemplateInstance()->compileVariables();
234 * Compile all required templates into the current loaded one
238 public function compileTemplate () {
239 // Call the inner class' method
240 $this->getTemplateInstance()->compileTemplate();
244 * Assigns the last loaded raw template content with a given variable
246 * @param $templateName Name of the template we want to assign
247 * @param $variableName Name of the variable we want to assign
250 public function assignTemplateWithVariable ($templateName, $variableName) {
251 // Call the inner class' method
252 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
256 * Transfers the content of this template engine to a given response instance
258 * @param $responseInstance An instance of a response class
261 public function transferToResponse (Responseable $responseInstance) {
262 // Call the inner class' method
263 $this->getTemplateInstance()->transportToResponse($responseInstance);
267 * Assigns all the application data with template variables
269 * @param $applicationInstance A manageable application instance
272 public function assignApplicationData (ManageableApplication $applicationInstance) {
273 // Call the inner class' method
274 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
278 * "Compiles" a variable by replacing {?var?} with it's content
280 * @param $rawCode Raw code to compile
281 * @param $setMatchAsCode Sets $match if readVariable() returns empty result
282 * @return $rawCode Compile code with inserted variable value
284 public function compileRawCode ($rawCode, $setMatchAsCode = FALSE) {
285 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
289 * Getter for variable group array
291 * @return $variableGroups All variable groups
293 public final function getVariableGroups () {
294 // Call the inner class' method
295 return $this->getTemplateInstance()->getVariableGroups();
299 * Getter for raw template data
301 * @return $rawTemplateData The raw data from the template
303 public function getRawTemplateData () {
304 // Call the inner class' method
305 return $this->getTemplateInstance()->getRawTemplateData();
309 * Renames a variable in code and in stack
311 * @param $oldName Old name of variable
312 * @param $newName New name of variable
315 public function renameVariable ($oldName, $newName) {
316 // Call the inner class' method
317 $this->getTemplateInstance()->renameVariable($oldName, $newName);
321 * Renders the given XML content
323 * @param $content Valid XML content or if not set the current loaded raw content
325 * @throws XmlParserException If an XML error was found
327 public function renderXmlContent ($content = NULL) {
328 // Call the inner class' method
329 $this->getTemplateInstance()->renderXmlContent($content);
333 * Enables or disables language support
335 * @param $languageSupport New language support setting
338 public function enableLanguageSupport ($languageSupport = TRUE) {
339 // Call the inner class' method
340 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
344 * Checks whether language support is enabled
346 * @return $languageSupport Whether language support is enabled or disabled
348 public function isLanguageSupportEnabled () {
349 // Call the inner class' method
350 return $this->getTemplateInstance()->isLanguageSupportEnabled();
354 * Enables or disables XML compacting
356 * @param $xmlCompacting New XML compacting setting
359 public function enableXmlCompacting ($xmlCompacting = TRUE) {
360 // Call the inner class' method
361 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
365 * Checks whether XML compacting is enabled
367 * @return $xmlCompacting Whether XML compacting is enabled or disabled
369 public function isXmlCompactingEnabled () {
370 // Call the inner class' method
371 return $this->getTemplateInstance()->isXmlCompactingEnabled();
375 * Handles the start element of an XML resource
377 * @param $resource XML parser resource (currently ignored)
378 * @param $element The element we shall handle
379 * @param $attributes All attributes
381 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
383 public function startElement ($resource, $element, array $attributes) {
384 // Call the inner class' method
385 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
389 * Ends the main or sub node by sending out the gathered data
391 * @param $resource An XML resource pointer (currently ignored)
392 * @param $nodeName Name of the node we want to finish
394 * @throws XmlNodeMismatchException If current main node mismatches the closing one
396 public function finishElement ($resource, $nodeName) {
397 // Call the inner class' method
398 $this->getTemplateInstance()->finishElement($resource, $nodeName);
404 * @param $resource XML parser resource (currently ignored)
405 * @param $characters Characters to handle
407 * @todo Find something useful with this!
409 public function characterHandler ($resource, $characters) {
410 // Call the inner class' method but trim the characters before
411 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
415 * Removes all comments, tabs and new-line charcters to compact the content
417 * @param $uncompactedContent The uncompacted content
418 * @return $compactedContent The compacted content
420 public function compactContent ($uncompactedContent) {
422 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
425 return $compactedContent;
429 * Assigns a lot variables into the stack of currently loaded template.
430 * This method should only be used in very rare circumstances, e.g. when
431 * you have to copy a whole set of variables into the template engine.
432 * Before you use this method, please make sure you have considered all
433 * other possiblities.
435 * @param $variables An array with variables to be assigned
438 public function assignMultipleVariables (array $variables) {
439 // Call the inner class' method but trim the characters before
440 $this->getTemplateInstance()->assignMultipleVariables($variables);