3 namespace Org\Mxchange\CoreFramework\Template\Xml;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\BaseDecorator;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
11 * A decorator for XML template engines which rewrites the XML for compacting
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of the class TemplateEngine and prepares it for usage
47 * @param $innerTemplateInstance A CompileableTemplate instance
48 * @return $templateInstance An instance of TemplateEngine
50 public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
52 $templateInstance = new XmlRewriterTemplateDecorator();
54 // Set the inner template engine
55 $templateInstance->setTemplateInstance($innerTemplateInstance);
57 // Return the prepared instance
58 return $templateInstance;
62 * Settter for variable group
64 * @param $groupName Name of variable group
65 * @param $add Whether add this group
68 public function setVariableGroup ($groupName, $add = true) {
69 // Call the inner class' method
70 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
74 * Adds a variable to current group
76 * @param $var Variable to set
77 * @param $value Value to store in variable
80 public function addGroupVariable ($var, $value) {
81 // Call the inner class' method
82 $this->getTemplateInstance()->addGroupVariable($var, $value);
86 * Getter for base path
88 * @return $templateBasePath The relative base path for all templates
90 public final function getTemplateBasePath () {
91 // Call the inner class' method
92 return $this->getTemplateInstance()->getTemplateBasePath();
96 * Getter for generic base path
98 * @return $templateBasePath The relative base path for all templates
100 public final function getGenericBasePath () {
101 // Call the inner class' method
102 return $this->getTemplateInstance()->getGenericBasePath();
106 * Getter for template extension
108 * @return $templateExtension The file extension for all uncompiled templates
110 public final function getRawTemplateExtension () {
111 // Call the inner class' method
112 return $this->getTemplateInstance()->getRawTemplateExtension();
116 * Getter for given variable group
118 * @param $variableGroup Variable group to check
119 * @return $varStack Found variable group
121 public function getVarStack ($variableGroup) {
122 // Call the inner class' method
123 return $this->getTemplateInstance()->getVarStack($variableGroup);
127 * Getter for code-template extension
129 * @return $codeExtension The file extension for all code templates
131 public final function getCodeTemplateExtension () {
132 // Call the inner class' method
133 return $this->getTemplateInstance()->getCodeTemplateExtension();
137 * Getter for template type
139 * @return $templateType The current template's type
141 public final function getTemplateType () {
142 // Call the inner class' method
143 return $this->getTemplateInstance()->getTemplateType();
147 * Assign (add) a given variable with a value
149 * @param $var The variable we are looking for
150 * @param $value The value we want to store in the variable
153 public function assignVariable ($var, $value) {
154 // Call the inner class' method
155 $this->getTemplateInstance()->assignVariable($var, $value);
159 * Removes a given variable
161 * @param $variableName The variable we are looking for
162 * @param $variableGroup Name of variable group (default: 'general')
165 public function removeVariable ($variableName, $variableGroup = 'general') {
166 // Call the inner class' method
167 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
171 * Load a specified HTML template into the engine
173 * @param $template The web template we shall load which is located in
177 public function loadHtmlTemplate ($template) {
178 // Call the inner class' method
179 $this->getTemplateInstance()->loadHtmlTemplate($template);
183 * Assign a given congfiguration variable with a value
185 * @param $variableName The configuration variable we want to assign
188 public function assignConfigVariable ($variableName) {
189 // Call the inner class' method
190 $this->getTemplateInstance()->assignConfigVariable($variableName);
194 * Load a specified code template into the engine
196 * @param $template The code template we shall load which is
197 * located in 'code' by default
200 public function loadCodeTemplate ($template) {
201 // Call the inner class' method
202 $this->getTemplateInstance()->loadCodeTemplate($template);
206 * Load a specified email template into the engine for later compilation
207 * with other code/web/email templates.
209 * @param $template The email template we shall load which is
210 * located in "html" by default
213 public function loadEmailTemplate ($template) {
214 // Call the inner class' method
215 $this->getTemplateInstance()->loadEmailTemplate($template);
219 * Compiles configuration place-holders in all variables. This 'walks'
220 * through the variable stack 'general'. It interprets all values from that
221 * variables as configuration entries after compiling them.
225 public function compileConfigInVariables () {
226 // Call the inner class' method
227 $this->getTemplateInstance()->compileConfigInVariables();
231 * Compile all variables by inserting their respective values
235 public function compileVariables () {
236 // Call the inner class' method
237 $this->getTemplateInstance()->compileVariables();
241 * Compile all required templates into the current loaded one
245 public function compileTemplate () {
246 // Call the inner class' method
247 $this->getTemplateInstance()->compileTemplate();
251 * Assigns the last loaded raw template content with a given variable
253 * @param $templateName Name of the template we want to assign
254 * @param $variableName Name of the variable we want to assign
257 public function assignTemplateWithVariable ($templateName, $variableName) {
258 // Call the inner class' method
259 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
263 * Transfers the content of this template engine to a given response instance
265 * @param $responseInstance An instance of a Responseable class
268 public function transferToResponse (Responseable $responseInstance) {
269 // Call the inner class' method
270 $this->getTemplateInstance()->transportToResponse($responseInstance);
274 * Assigns all the application data with template variables
278 public function assignApplicationData () {
279 // Call the inner class' method
280 $this->getTemplateInstance()->assignApplicationData();
284 * "Compiles" a variable by replacing {?var?} with it's content
286 * @param $rawCode Raw code to compile
287 * @param $setMatchAsCode Sets $match if readVariable() returns empty result
288 * @return $rawCode Compile code with inserted variable value
290 public function compileRawCode ($rawCode, $setMatchAsCode = false) {
291 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
295 * Getter for variable group array
297 * @return $variableGroups All variable groups
299 public final function getVariableGroups () {
300 // Call the inner class' method
301 return $this->getTemplateInstance()->getVariableGroups();
305 * Getter for raw template data
307 * @return $rawTemplateData The raw data from the template
309 public function getRawTemplateData () {
310 // Call the inner class' method
311 return $this->getTemplateInstance()->getRawTemplateData();
315 * Renames a variable in code and in stack
317 * @param $oldName Old name of variable
318 * @param $newName New name of variable
321 public function renameVariable ($oldName, $newName) {
322 // Call the inner class' method
323 $this->getTemplateInstance()->renameVariable($oldName, $newName);
327 * Renders the given XML content
329 * @param $content Valid XML content or if not set the current loaded raw content
331 * @throws XmlParserException If an XML error was found
333 public function renderXmlContent ($content = NULL) {
334 // Call the inner class' method
335 $this->getTemplateInstance()->renderXmlContent($content);
339 * Enables or disables language support
341 * @param $languageSupport New language support setting
344 public function enableLanguageSupport ($languageSupport = true) {
345 // Call the inner class' method
346 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
350 * Checks whether language support is enabled
352 * @return $languageSupport Whether language support is enabled or disabled
354 public function isLanguageSupportEnabled () {
355 // Call the inner class' method
356 return $this->getTemplateInstance()->isLanguageSupportEnabled();
360 * Enables or disables XML compacting
362 * @param $xmlCompacting New XML compacting setting
365 public function enableXmlCompacting ($xmlCompacting = true) {
366 // Call the inner class' method
367 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
371 * Checks whether XML compacting is enabled
373 * @return $xmlCompacting Whether XML compacting is enabled or disabled
375 public function isXmlCompactingEnabled () {
376 // Call the inner class' method
377 return $this->getTemplateInstance()->isXmlCompactingEnabled();
381 * Handles the start element of an XML resource
383 * @param $resource XML parser resource (currently ignored)
384 * @param $element The element we shall handle
385 * @param $attributes All attributes
387 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
389 public function startElement ($resource, $element, array $attributes) {
390 // Call the inner class' method
391 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
395 * Ends the main or sub node by sending out the gathered data
397 * @param $resource An XML resource pointer (currently ignored)
398 * @param $nodeName Name of the node we want to finish
400 * @throws XmlNodeMismatchException If current main node mismatches the closing one
402 public function finishElement ($resource, $nodeName) {
403 // Call the inner class' method
404 $this->getTemplateInstance()->finishElement($resource, $nodeName);
410 * @param $resource XML parser resource (currently ignored)
411 * @param $characters Characters to handle
413 * @todo Find something useful with this!
415 public function characterHandler ($resource, $characters) {
416 // Call the inner class' method but trim the characters before
417 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
421 * Removes all comments, tabs and new-line charcters to compact the content
423 * @param $uncompactedContent The uncompacted content
424 * @return $compactedContent The compacted content
426 public function compactContent ($uncompactedContent) {
428 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
431 return $compactedContent;
435 * Assigns a lot variables into the stack of currently loaded template.
436 * This method should only be used in very rare circumstances, e.g. when
437 * you have to copy a whole set of variables into the template engine.
438 * Before you use this method, please make sure you have considered all
439 * other possiblities.
441 * @param $variables An array with variables to be assigned
444 public function assignMultipleVariables (array $variables) {
445 // Call the inner class' method but trim the characters before
446 $this->getTemplateInstance()->assignMultipleVariables($variables);