3 namespace Org\Mxchange\CoreFramework\Template\Xml;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\BaseDecorator;
7 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
8 use Org\Mxchange\CoreFramework\Response\Responseable;
9 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
12 * A decorator for XML template engines which rewrites the XML for compacting
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 <<<<<<< HEAD:framework/main/classes/decorator/template/class_XmlRewriterTemplateDecorator.php
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
21 >>>>>>> Some updates::inc/main/classes/decorator/template/class_XmlRewriterTemplateDecorator.php
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of the class TemplateEngine and prepares it for usage
52 * @param $innerTemplateInstance A CompileableTemplate instance
53 * @return $templateInstance An instance of TemplateEngine
55 public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
57 $templateInstance = new XmlRewriterTemplateDecorator();
59 // Set the inner template engine
60 $templateInstance->setTemplateInstance($innerTemplateInstance);
62 // Return the prepared instance
63 return $templateInstance;
67 * Settter for variable group
69 * @param $groupName Name of variable group
70 * @param $add Whether add this group
73 public function setVariableGroup ($groupName, $add = true) {
74 // Call the inner class' method
75 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
79 * Adds a variable to current group
81 * @param $var Variable to set
82 * @param $value Value to store in variable
85 public function addGroupVariable ($var, $value) {
86 // Call the inner class' method
87 $this->getTemplateInstance()->addGroupVariable($var, $value);
91 * Getter for base path
93 * @return $templateBasePath The relative base path for all templates
95 public final function getTemplateBasePath () {
96 // Call the inner class' method
97 return $this->getTemplateInstance()->getTemplateBasePath();
101 * Getter for generic base path
103 * @return $templateBasePath The relative base path for all templates
105 public final function getGenericBasePath () {
106 // Call the inner class' method
107 return $this->getTemplateInstance()->getGenericBasePath();
111 * Getter for template extension
113 * @return $templateExtension The file extension for all uncompiled templates
115 public final function getRawTemplateExtension () {
116 // Call the inner class' method
117 return $this->getTemplateInstance()->getRawTemplateExtension();
121 * Getter for given variable group
123 * @param $variableGroup Variable group to check
124 * @return $varStack Found variable group
126 public function getVarStack ($variableGroup) {
127 // Call the inner class' method
128 return $this->getTemplateInstance()->getVarStack($variableGroup);
132 * Getter for code-template extension
134 * @return $codeExtension The file extension for all code templates
136 public final function getCodeTemplateExtension () {
137 // Call the inner class' method
138 return $this->getTemplateInstance()->getCodeTemplateExtension();
142 * Getter for template type
144 * @return $templateType The current template's type
146 public final function getTemplateType () {
147 // Call the inner class' method
148 return $this->getTemplateInstance()->getTemplateType();
152 * Assign (add) a given variable with a value
154 * @param $var The variable we are looking for
155 * @param $value The value we want to store in the variable
158 public function assignVariable ($var, $value) {
159 // Call the inner class' method
160 $this->getTemplateInstance()->assignVariable($var, $value);
164 * Removes a given variable
166 * @param $variableName The variable we are looking for
167 * @param $variableGroup Name of variable group (default: 'general')
170 public function removeVariable ($variableName, $variableGroup = 'general') {
171 // Call the inner class' method
172 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
176 * Load a specified HTML template into the engine
178 * @param $template The web template we shall load which is located in
182 public function loadHtmlTemplate ($template) {
183 // Call the inner class' method
184 $this->getTemplateInstance()->loadHtmlTemplate($template);
188 * Assign a given congfiguration variable with a value
190 * @param $variableName The configuration variable we want to assign
193 public function assignConfigVariable ($variableName) {
194 // Call the inner class' method
195 $this->getTemplateInstance()->assignConfigVariable($variableName);
199 * Load a specified code template into the engine
201 * @param $template The code template we shall load which is
202 * located in 'code' by default
205 public function loadCodeTemplate ($template) {
206 // Call the inner class' method
207 $this->getTemplateInstance()->loadCodeTemplate($template);
211 * Load a specified email template into the engine for later compilation
212 * with other code/web/email templates.
214 * @param $template The email template we shall load which is
215 * located in "html" by default
218 public function loadEmailTemplate ($template) {
219 // Call the inner class' method
220 $this->getTemplateInstance()->loadEmailTemplate($template);
224 * Compiles configuration place-holders in all variables. This 'walks'
225 * through the variable stack 'general'. It interprets all values from that
226 * variables as configuration entries after compiling them.
230 public function compileConfigInVariables () {
231 // Call the inner class' method
232 $this->getTemplateInstance()->compileConfigInVariables();
236 * Compile all variables by inserting their respective values
240 public function compileVariables () {
241 // Call the inner class' method
242 $this->getTemplateInstance()->compileVariables();
246 * Compile all required templates into the current loaded one
250 public function compileTemplate () {
251 // Call the inner class' method
252 $this->getTemplateInstance()->compileTemplate();
256 * Assigns the last loaded raw template content with a given variable
258 * @param $templateName Name of the template we want to assign
259 * @param $variableName Name of the variable we want to assign
262 public function assignTemplateWithVariable ($templateName, $variableName) {
263 // Call the inner class' method
264 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
268 * Transfers the content of this template engine to a given response instance
270 * @param $responseInstance An instance of a Responseable class
273 public function transferToResponse (Responseable $responseInstance) {
274 // Call the inner class' method
275 $this->getTemplateInstance()->transportToResponse($responseInstance);
279 * Assigns all the application data with template variables
281 * @param $applicationInstance A manageable application instance
284 public function assignApplicationData (ManageableApplication $applicationInstance) {
285 // Call the inner class' method
286 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
290 * "Compiles" a variable by replacing {?var?} with it's content
292 * @param $rawCode Raw code to compile
293 * @param $setMatchAsCode Sets $match if readVariable() returns empty result
294 * @return $rawCode Compile code with inserted variable value
296 public function compileRawCode ($rawCode, $setMatchAsCode = false) {
297 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
301 * Getter for variable group array
303 * @return $variableGroups All variable groups
305 public final function getVariableGroups () {
306 // Call the inner class' method
307 return $this->getTemplateInstance()->getVariableGroups();
311 * Getter for raw template data
313 * @return $rawTemplateData The raw data from the template
315 public function getRawTemplateData () {
316 // Call the inner class' method
317 return $this->getTemplateInstance()->getRawTemplateData();
321 * Renames a variable in code and in stack
323 * @param $oldName Old name of variable
324 * @param $newName New name of variable
327 public function renameVariable ($oldName, $newName) {
328 // Call the inner class' method
329 $this->getTemplateInstance()->renameVariable($oldName, $newName);
333 * Renders the given XML content
335 * @param $content Valid XML content or if not set the current loaded raw content
337 * @throws XmlParserException If an XML error was found
339 public function renderXmlContent ($content = NULL) {
340 // Call the inner class' method
341 $this->getTemplateInstance()->renderXmlContent($content);
345 * Enables or disables language support
347 * @param $languageSupport New language support setting
350 public function enableLanguageSupport ($languageSupport = true) {
351 // Call the inner class' method
352 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
356 * Checks whether language support is enabled
358 * @return $languageSupport Whether language support is enabled or disabled
360 public function isLanguageSupportEnabled () {
361 // Call the inner class' method
362 return $this->getTemplateInstance()->isLanguageSupportEnabled();
366 * Enables or disables XML compacting
368 * @param $xmlCompacting New XML compacting setting
371 public function enableXmlCompacting ($xmlCompacting = true) {
372 // Call the inner class' method
373 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
377 * Checks whether XML compacting is enabled
379 * @return $xmlCompacting Whether XML compacting is enabled or disabled
381 public function isXmlCompactingEnabled () {
382 // Call the inner class' method
383 return $this->getTemplateInstance()->isXmlCompactingEnabled();
387 * Handles the start element of an XML resource
389 * @param $resource XML parser resource (currently ignored)
390 * @param $element The element we shall handle
391 * @param $attributes All attributes
393 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
395 public function startElement ($resource, $element, array $attributes) {
396 // Call the inner class' method
397 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
401 * Ends the main or sub node by sending out the gathered data
403 * @param $resource An XML resource pointer (currently ignored)
404 * @param $nodeName Name of the node we want to finish
406 * @throws XmlNodeMismatchException If current main node mismatches the closing one
408 public function finishElement ($resource, $nodeName) {
409 // Call the inner class' method
410 $this->getTemplateInstance()->finishElement($resource, $nodeName);
416 * @param $resource XML parser resource (currently ignored)
417 * @param $characters Characters to handle
419 * @todo Find something useful with this!
421 public function characterHandler ($resource, $characters) {
422 // Call the inner class' method but trim the characters before
423 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
427 * Removes all comments, tabs and new-line charcters to compact the content
429 * @param $uncompactedContent The uncompacted content
430 * @return $compactedContent The compacted content
432 public function compactContent ($uncompactedContent) {
434 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
437 return $compactedContent;
441 * Assigns a lot variables into the stack of currently loaded template.
442 * This method should only be used in very rare circumstances, e.g. when
443 * you have to copy a whole set of variables into the template engine.
444 * Before you use this method, please make sure you have considered all
445 * other possiblities.
447 * @param $variables An array with variables to be assigned
450 public function assignMultipleVariables (array $variables) {
451 // Call the inner class' method but trim the characters before
452 $this->getTemplateInstance()->assignMultipleVariables($variables);