3 namespace Org\Mxchange\CoreFramework\Template\Engine\Xml;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Factory\Template\XmlTemplateEngineFactory;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
11 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
12 use Org\Mxchange\CoreFramework\Traits\Stacker\StackableTrait;
13 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
16 use \InvalidArgumentException;
19 * A generic XML template engine class
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
26 * @todo This template engine does not make use of setTemplateType()
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
46 * Main nodes in the XML tree
48 private $mainNodes = [];
51 * Sub nodes in the XML tree
53 private $subNodes = [];
63 private $xmlTemplateType = 'xml';
68 private $typePrefix = 'xml';
73 private $stackerName = '';
76 * Content from dependency
78 protected $dependencyContent = [];
81 * Template engine instance
83 private $templateInstance = NULL;
86 * Protected constructor
88 * @param $className Name of the class
91 protected function __construct (string $className) {
92 // Call parent constructor
93 parent::__construct($className);
97 * Does a generic initialization of the template engine
99 * @param $typePrefix Type prefix
100 * @param $xmlTemplateType Type of XML template
101 * @return $templateInstance An instance of TemplateEngine
102 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
103 * @throws InvalidBasePathStringException If $templateBasePath is no string
104 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
105 * directory or not found
106 * @throws BasePathReadProtectedException If $templateBasePath is
109 protected function initXmlTemplateEngine (string $typePrefix, string $xmlTemplateType) {
110 // Set XML template type and prefix
111 $this->xmlTemplateType = $xmlTemplateType;
112 $this->typePrefix = $typePrefix;
114 // Get template instance
115 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
117 // Determine base path
118 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . FrameworkBootstrap::getRequestInstance()->getRequestElement('app') . '/';
120 // Is the base path valid?
121 if (empty($templateBasePath)) {
122 // Base path is empty
123 throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
124 } elseif (!is_string($templateBasePath)) {
126 throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
127 } elseif (!is_dir($templateBasePath)) {
129 throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
130 } elseif (!is_readable($templateBasePath)) {
132 throw new BasePathReadProtectedException(array($this, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
136 $this->setTemplateBasePath($templateBasePath);
138 // Set template extensions
139 $this->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
140 $this->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($typePrefix . '_message_template_extension'));
142 // Absolute output path for compiled templates
143 $this->setCompileOutputPath(sprintf('%s%s',
145 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
148 // Init a variable stacker
149 $stackInstance = ObjectFactory::createObjectByConfiguredName($typePrefix . '_' . $xmlTemplateType . '_stacker_class');
152 $this->stackerName = $typePrefix . '_' . $xmlTemplateType;
155 $stackInstance->initStack($this->stackerName);
158 $this->setStackInstance($stackInstance);
160 // Set it in main nodes
161 array_push($this->mainNodes, str_replace('_', '-', $xmlTemplateType));
165 * Load a specified XML template into the engine
167 * @param $templateName Optional name of template
170 public function loadXmlTemplate (string $templateName = '') {
171 // Is the template name empty?
172 if (empty($templateName)) {
173 // Set generic template name
174 $templateName = $this->typePrefix . '_' . $this->xmlTemplateType . '_template_type';
178 $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($templateName));
180 // Load the special template
181 $this->loadTemplate($this->xmlTemplateType);
185 * Getter for current main node
187 * @return $currMainNode Current main node
189 public final function getCurrMainNode () {
190 return $this->curr['main_node'];
194 * Setter for current main node
196 * @param $element Element name to set as current main node
197 * @return $currMainNode Current main node
199 private final function setCurrMainNode ($element) {
200 $this->curr['main_node'] = (string) $element;
204 * Getter for main node array
206 * @return $mainNodes Array with valid main node names
208 public final function getMainNodes () {
209 return $this->mainNodes;
213 * Getter for stacker name
215 * @return $stackerName Name of stacker of this class
217 protected final function getStackerName () {
218 return $this->stackerName;
222 * Setter for sub node array
224 * @param $subNodes Array with valid sub node names
227 public final function setSubNodes (array $subNodes) {
228 $this->subNodes = $subNodes;
232 * Getter for sub node array
234 * @return $subNodes Array with valid sub node names
236 public final function getSubNodes () {
237 return $this->subNodes;
241 * Setter for template engine instances
243 * @param $templateInstance An instance of a template engine class
246 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
247 $this->templateInstance = $templateInstance;
251 * Getter for template engine instances
253 * @return $templateInstance An instance of a template engine class
255 protected final function getTemplateInstance () {
256 return $this->templateInstance;
260 * Read XML variables by calling readVariable() with 'general' as
263 * @param $key Key to read from
264 * @return $value Value from variable
266 public function readXmlData (string $key) {
267 // Is key parameter valid?
268 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: key=%s - CALLED!', $key));
271 throw new InvalidArgumentException('Parameter key is empty');
275 $value = parent::readVariable($key, 'general');
278 if (is_null($value)) {
279 // Bah, needs fixing.
280 $this->debugInstance(sprintf('[%s:%d]: key=%s returns NULL', __METHOD__, __LINE__, $key));
288 * Handles the template dependency for given XML node
290 * @param $node The XML node we should load a dependency template
291 * @param $templateDependency A template to load to satisfy dependencies
294 protected function handleTemplateDependency ($node, $templateDependency) {
295 // Check that the XML node is not empty
296 assert(!empty($node));
298 // Is the template dependency set?
299 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
300 // Get a temporay template instance
301 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance($this->typePrefix . '_' . self::convertDashesToUnderscores($node) . '_' . $this->xmlTemplateType . '_template_class');
304 $templateInstance->loadXmlTemplate($templateDependency);
306 // Parse the XML content
307 $templateInstance->renderXmlContent();
309 // Save the parsed raw content in our dependency array
310 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
315 * Handles the start element of an XML resource
317 * @param $resource XML parser resource (currently ignored)
318 * @param $element The element we shall handle
319 * @param $attributes All attributes
321 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
323 public final function startElement ($resource, $element, array $attributes) {
324 // Initial method name which will never be called...
325 $methodName = 'init' . StringUtils::convertToClassName($this->xmlTemplateType);
327 // Make the element name lower-case
328 $element = strtolower($element);
330 // Is the element a main node?
331 //* DEBUG: */ echo "START: >".$element."<<br />\n";
332 if (in_array($element, $this->getMainNodes())) {
333 // Okay, main node found!
334 $methodName = 'start' . StringUtils::convertToClassName($element);
337 $this->setCurrMainNode($element);
338 } elseif (in_array($element, $this->getSubNodes())) {
340 $methodName = 'start' . StringUtils::convertToClassName($element);
342 // Invalid node name found
343 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
347 call_user_func_array(array($this, $methodName), $attributes);
351 * Ends the main or sub node by sending out the gathered data
353 * @param $resource An XML resource pointer (currently ignored)
354 * @param $nodeName Name of the node we want to finish
356 * @throws XmlNodeMismatchException If current main node mismatches the closing one
358 public final function finishElement ($resource, $nodeName) {
359 // Make all lower-case
360 $nodeName = strtolower($nodeName);
362 // Does this match with current main node?
363 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
364 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
366 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
369 // Construct method name
370 $methodName = 'finish' . StringUtils::convertToClassName($nodeName);
372 // Call the corresponding method
373 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
374 call_user_func_array(array($this, $methodName), array());