]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/template/xml/class_BaseXmlTemplateEngine.php
Continued:
[core.git] / framework / main / classes / template / xml / class_BaseXmlTemplateEngine.php
index bf63126660a544485f14b14202657023f06f78ba..cbbf4a736a026a290ef65fe393ab7d1330c0d66a 100644 (file)
@@ -4,13 +4,16 @@ namespace Org\Mxchange\CoreFramework\Template\Engine\Xml;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Factory\Template\XmlTemplateEngineFactory;
+use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
-use Org\Mxchange\CoreFramework\Traits\Stacker\StackableTrait;
-use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
+use Org\Mxchange\CoreFramework\Template\Xml\CompileableXmlTemplate;
+use Org\Mxchange\CoreFramework\Traits\Stack\StackableTrait;
+use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
+use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
 // Import SPL stuff
 use \InvalidArgumentException;
@@ -38,8 +41,9 @@ use \InvalidArgumentException;
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
+abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements CompileableXmlTemplate {
        // Load traits
+       use CompileableTemplateTrait;
        use StackableTrait;
 
        /**
@@ -78,9 +82,14 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
        protected $dependencyContent = [];
 
        /**
-        * Template engine instance
+        * XML compacting is disabled by default
         */
-       private $templateInstance = NULL;
+       private $xmlCompacting = false;
+
+       /**
+        * Method name for XML template type
+        */
+       private $initMethodName = 'invalid';
 
        /**
         * Protected constructor
@@ -110,9 +119,10 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                // Set XML template type and prefix
                $this->xmlTemplateType = $xmlTemplateType;
                $this->typePrefix      = $typePrefix;
+               $this->initMethodName = sprintf('init%s', StringUtils::convertToClassName($this->xmlTemplateType));
 
                // Get template instance
-               $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
+               $applicationInstance = ApplicationHelper::getSelfInstance();
 
                // Determine base path
                $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . FrameworkBootstrap::getRequestInstance()->getRequestElement('app') . '/';
@@ -121,9 +131,6 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                if (empty($templateBasePath)) {
                        // Base path is empty
                        throw new BasePathIsEmptyException($this, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!is_string($templateBasePath)) {
-                       // Is not a string
-                       throw new InvalidBasePathStringException(array($this, $templateBasePath), self::EXCEPTION_INVALID_STRING);
                } elseif (!is_dir($templateBasePath)) {
                        // Is not a path
                        throw new BasePathIsNoDirectoryException(array($this, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
@@ -172,7 +179,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                if (empty($templateName)) {
                        // Set generic template name
                        $templateName = $this->typePrefix . '_' . $this->xmlTemplateType . '_template_type';
-               } // END - if
+               }
 
                // Set template type
                $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($templateName));
@@ -196,8 +203,8 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
         * @param       $element                Element name to set as current main node
         * @return      $currMainNode   Current main node
         */
-       private final function setCurrMainNode ($element) {
-               $this->curr['main_node'] = (string) $element;
+       private final function setCurrMainNode (string $element) {
+               $this->curr['main_node'] = $element;
        }
 
        /**
@@ -237,25 +244,6 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                return $this->subNodes;
        }
 
-       /**
-        * Setter for template engine instances
-        *
-        * @param       $templateInstance       An instance of a template engine class
-        * @return      void
-        */
-       protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
-               $this->templateInstance = $templateInstance;
-       }
-
-       /**
-        * Getter for template engine instances
-        *
-        * @return      $templateInstance       An instance of a template engine class
-        */
-       protected final function getTemplateInstance () {
-               return $this->templateInstance;
-       }
-
        /**
         * Read XML variables by calling readVariable() with 'general' as
         * variable stack.
@@ -278,7 +266,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                if (is_null($value)) {
                        // Bah, needs fixing.
                        $this->debugInstance(sprintf('[%s:%d]: key=%s returns NULL', __METHOD__, __LINE__, $key));
-               } // END - if
+               }
 
                // Return value
                return $value;
@@ -291,7 +279,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
         * @param       $templateDependency             A template to load to satisfy dependencies
         * @return      void
         */
-       protected function handleTemplateDependency ($node, $templateDependency) {
+       protected function handleTemplateDependency (string $node, string $templateDependency) {
                // Check that the XML node is not empty
                assert(!empty($node));
 
@@ -308,7 +296,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
 
                        // Save the parsed raw content in our dependency array
                        $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
-               } // END - if
+               }
        }
 
        /**
@@ -320,9 +308,9 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
         * @return      void
         * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
         */
-       public final function startElement ($resource, $element, array $attributes) {
+       public final function startElement ($resource, string $element, array $attributes) {
                // Initial method name which will never be called...
-               $methodName = 'init' . StringUtils::convertToClassName($this->xmlTemplateType);
+               $methodName = $this->initMethodName;
 
                // Make the element name lower-case
                $element = strtolower($element);
@@ -355,7 +343,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
         * @return      void
         * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
         */
-       public final function finishElement ($resource, $nodeName) {
+       public final function finishElement ($resource, string $nodeName) {
                // Make all lower-case
                $nodeName = strtolower($nodeName);
 
@@ -364,7 +352,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
                        // Did not match!
                        throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
-               } // END - if
+               }
 
                // Construct method name
                $methodName = 'finish' . StringUtils::convertToClassName($nodeName);
@@ -374,4 +362,50 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine {
                call_user_func_array(array($this, $methodName), array());
        }
 
+       /**
+        * Renders the given XML content
+        *
+        * @param       $content        Valid XML content or if not set the current loaded raw content
+        * @return      void
+        * @throws      XmlParserException      If an XML error was found
+        */
+       public function renderXmlContent (string $content = NULL) {
+               // Is the content set?
+               if (is_null($content)) {
+                       // Get current content
+                       $content = $this->getRawTemplateData();
+               }
+
+               // Get a XmlParser instance
+               $parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', [$this]);
+
+               // Check if XML compacting is enabled
+               if ($this->isXmlCompactingEnabled()) {
+                       // Yes, so get a decorator class for transparent compacting
+                       $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));
+               }
+
+               // Parse the XML document
+               $parserInstance->parseXmlContent($content);
+       }
+
+       /**
+        * Enables or disables XML compacting
+        *
+        * @param       $xmlCompacting  New XML compacting setting
+        * @return      void
+        */
+       public final function enableXmlCompacting (bool $xmlCompacting = true) {
+               $this->xmlCompacting = $xmlCompacting;
+       }
+
+       /**
+        * Checks whether XML compacting is enabled
+        *
+        * @return      $xmlCompacting  Whether XML compacting is enabled or disabled
+        */
+       public final function isXmlCompactingEnabled () {
+               return $this->xmlCompacting;
+       }
+
 }