]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/template/answer/announcement/class_XmlAnnouncementAnswerTemplateEngine.php
Continued with hub:
[hub.git] / application / hub / main / template / answer / announcement / class_XmlAnnouncementAnswerTemplateEngine.php
index f24014b8ea6ebb031413283403666ea23bc3ce79..d4f0dc738feb5291a01b3d09437e84388d45e16f 100644 (file)
@@ -1,12 +1,12 @@
 <?php
 /**
- * An ??? template engine class for XML templates
+ * An AnnouncementAnswer template engine class for XML templates
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  * @todo               This template engine does not make use of setTemplateType()
  *
  * This program is free software: you can redistribute it and/or modify
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class XmlAnnouncementAnswerTemplateEngine extends BaseTemplateEngine implements CompileableTemplate, Registerable {
+class XmlAnnouncementAnswerTemplateEngine extends BaseXmlAnswerTemplateEngine implements CompileableTemplate, Registerable {
        /**
-        * Main nodes in the XML tree
+        * Some XML nodes must be available for later data extraction
         */
-       private $mainNodes = array(
-               '|||'
-       );
-
-       /**
-        * Sub nodes in the XML tree
-        */
-       private $subNodes = array(
-       );
-
-       /**
-        * Current main node
-        */
-       private $curr = array();
+       const ANNOUNCEMENT_DATA_SESSION_ID    = 'my-session-id';
+       const ANNOUNCEMENT_DATA_NODE_STATUS   = 'my-status';
+       const ANNOUNCEMENT_DATA_EXTERNAL_IP   = 'my-external-ip';
+       const ANNOUNCEMENT_DATA_INTERNAL_IP   = 'my-internal-ip';
+       const ANNOUNCEMENT_DATA_LISTEN_PORT      = 'my-listen-port';
 
        /**
         * Protected constructor
@@ -49,6 +40,25 @@ class XmlAnnouncementAnswerTemplateEngine extends BaseTemplateEngine implements
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Init sub-nodes array
+               $this->subNodes = array(
+                       // These nodes don't contain any data
+                       'my-data',
+                       'your-data',
+                       // Data from *this* node
+                       self::ANNOUNCEMENT_DATA_EXTERNAL_IP,
+                       self::ANNOUNCEMENT_DATA_INTERNAL_IP,
+                       self::ANNOUNCEMENT_DATA_LISTEN_PORT,
+                       self::ANNOUNCEMENT_DATA_NODE_STATUS,
+                       self::ANNOUNCEMENT_DATA_SESSION_ID,
+                       // Data from other node
+                       'your-external-ip',
+                       'your-internal-ip',
+                       'your-session-id',
+                       // Answer status (generic field)
+                       self::ANSWER_STATUS,
+               );
        }
 
        /**
@@ -66,213 +76,265 @@ class XmlAnnouncementAnswerTemplateEngine extends BaseTemplateEngine implements
                // Get a new instance
                $templateInstance = new XmlAnnouncementAnswerTemplateEngine();
 
-               // Get application instance from registry
-               $applicationInstance = Registry::getRegistry()->getInstance('app');
+               // Init instance
+               $templateInstance->initXmlTemplateEngine('node', 'announcement_answer');
+
+               // Return the prepared instance
+               return $templateInstance;
+       }
+
+       /**
+        * Currently not used
+        *
+        * @param       $resource               XML parser resource (currently ignored)
+        * @param       $characters             Characters to handle
+        * @return      void
+        * @todo        Find something useful with this!
+        */
+       public function characterHandler ($resource, $characters) {
+               // Trim all spaces away
+               $characters = trim($characters);
 
-               // Determine base path
-               $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
+               // Is this string empty?
+               if (empty($characters)) {
+                       // Then skip it silently
+                       return;
+               } // END - if
 
-               // Is the base path valid?
-               if (empty($templateBasePath)) {
-                       // Base path is empty
-                       throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!is_string($templateBasePath)) {
-                       // Is not a string
-                       throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
-               } elseif (!is_dir($templateBasePath)) {
-                       // Is not a path
-                       throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
-               } elseif (!is_readable($templateBasePath)) {
-                       // Is not readable
-                       throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
-               }
+               /*
+                * Assign the found characters to variable and use the last entry from
+                * stack as the name.
+                */
+               parent::assignVariable($this->getStackInstance()->getNamed('node_announcement_answer'), $characters);
+       }
 
-               // Set the base path
-               $templateInstance->setTemplateBasePath($templateBasePath);
+       /**
+        * Getter for cache file (FQFN)
+        *
+        * @return      $fqfn   Full-qualified file name of the menu cache
+        */
+       public function getAnnouncementAnswerCacheFqfn () {
+               $this->partialStub('Please implement this method.');
+       }
 
-               // Set template extensions
-               $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
-               $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('node_answer_template_extension'));
+       /**
+        * Starts the announcement-answer
+        *
+        * @return      void
+        */
+       protected function startAnnouncementAnswer () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'announcement-answer');
+       }
 
-               // Absolute output path for compiled templates
-               $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
+       /**
+        * Starts the my-data
+        *
+        * @return      void
+        */
+       protected function startMyData () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'my-data');
+       }
 
-               // Init a variable stacker
-               $stackerInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_answer_stacker_class');
+       /**
+        * Starts the my-external-ip
+        *
+        * @return      void
+        */
+       protected function startMyExternalIp () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', self::ANNOUNCEMENT_DATA_EXTERNAL_IP);
+       }
 
-               // Set it
-               $templateInstance->setStackerInstance($stackerInstance);
+       /**
+        * Starts the my-internal-ip
+        *
+        * @return      void
+        */
+       protected function startMyInternalIp () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', self::ANNOUNCEMENT_DATA_INTERNAL_IP);
+       }
 
-               // Return the prepared instance
-               return $templateInstance;
+       /**
+        * Starts the my-tcp-port
+        *
+        * @return      void
+        */
+       protected function startMyListenPort () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', self::ANNOUNCEMENT_DATA_LISTEN_PORT);
        }
 
        /**
-        * Load a specified announcement-answer template into the engine
+        * Starts the my-session-id
         *
-        * @param       $template       The announcement-answer template we shall load which is
-        *                                              located in 'announcement-answer' by default
         * @return      void
         */
-       public function loadAnnouncementAnswerTemplate ($template = 'announcement_answer') {
-               // Set template type
-               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('node_announcement_answer_template_type'));
+       protected function startMySessionId () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', self::ANNOUNCEMENT_DATA_SESSION_ID);
+       }
 
-               // Load the special template
-               $this->loadTemplate($template);
+       /**
+        * Starts the my-status
+        *
+        * @return      void
+        */
+       protected function startMyStatus () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', self::ANNOUNCEMENT_DATA_NODE_STATUS);
        }
 
        /**
-        * Getter for current main node
+        * Finishes the my-status
         *
-        * @return      $currMainNode   Current main node
+        * @return      void
         */
-       public final function getCurrMainNode () {
-               return $this->curr['main_node'];
+       protected function finishMyStatus () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Setter for current main node
+        * Finishes the my-session-id
         *
-        * @param       $element                Element name to set as current main node
-        * @return      $currMainNode   Current main node
+        * @return      void
         */
-       private final function setCurrMainNode ($element) {
-               $this->curr['main_node'] = (string) $element;
+       protected function finishMySessionId () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Getter for main node array
+        * Finishes the my-tcp-port
         *
-        * @return      $mainNodes      Array with valid main node names
+        * @return      void
         */
-       public final function getMainNodes () {
-               return $this->mainNodes;
+       protected function finishMyListenPort () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Getter for sub node array
+        * Finishes the my-internal-ip
         *
-        * @return      $subNodes       Array with valid sub node names
+        * @return      void
         */
-       public final function getSubNodes () {
-               return $this->subNodes;
+       protected function finishMyInternalIp () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Handles the start element of an XML resource
+        * Finishes the my-external-ip
         *
-        * @param       $resource               XML parser resource (currently ignored)
-        * @param       $element                The element we shall handle
-        * @param       $attributes             All attributes
         * @return      void
-        * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
         */
-       public function startElement ($resource, $element, array $attributes) {
-               // Initial method name which will never be called...
-               $methodName = 'initAnnouncementAnswer';
-
-               // Make the element name lower-case
-               $element = strtolower($element);
-
-               // Is the element a main node?
-               //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
-               if (in_array($element, $this->getMainNodes())) {
-                       // Okay, main node found!
-                       $methodName = 'start' . $this->convertToClassName($element);
-
-                       // Set it
-                       $this->setCurrMainNode($element);
-               } elseif (in_array($element, $this->getSubNodes())) {
-                       // Sub node found
-                       $methodName = 'start' . $this->convertToClassName($element);
-               } else {
-                       // Invalid node name found
-                       throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
-               }
-
-               // Call method
-               call_user_func_array(array($this, $methodName), $attributes);
+       protected function finishMyExternalIp () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Ends the main or sub node by sending out the gathered data
+        * Finishes the my-data
         *
-        * @param       $resource       An XML resource pointer (currently ignored)
-        * @param       $nodeName       Name of the node we want to finish
         * @return      void
-        * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
         */
-       public function endElement ($resource, $nodeName) {
-               // Make all lower-case
-               $nodeName = strtolower($nodeName);
-
-               // Does this match with current main node?
-               //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
-               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
+       protected function finishMyData () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
+       }
 
-               // Construct method name
-               $methodName = 'finish' . $this->convertToClassName($nodeName);
+       /**
+        * Starts the your-data
+        *
+        * @return      void
+        */
+       protected function startYourData () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'your-data');
+       }
 
-               // Call the corresponding method
-               //* DEBUG: */ echo "call: ".$methodName."<br />\n";
-               call_user_func_array(array($this, $methodName), array());
+       /**
+        * Starts the your-external-ip
+        *
+        * @return      void
+        */
+       protected function startYourExternalIp () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'your-external-ip');
        }
 
        /**
-        * Currently not used
+        * Starts the your-internal-ip
         *
-        * @param       $resource               XML parser resource (currently ignored)
-        * @param       $characters             Characters to handle
         * @return      void
-        * @todo        Find something useful with this!
         */
-       public function characterHandler ($resource, $characters) {
-               // Trim all spaces away
-               $characters = trim($characters);
+       protected function startYourInternalIp () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'your-internal-ip');
+       }
 
-               // Is this string empty?
-               if (empty($characters)) {
-                       // Then skip it silently
-                       return false;
-               } // END - if
+       /**
+        * Starts the your-session-id
+        *
+        * @return      void
+        */
+       protected function startYourSessionId () {
+               // Push the node name on the stacker
+               $this->getStackInstance()->pushNamed('node_announcement_answer', 'your-session-id');
+       }
 
-               // Get current XML node name as an array index
-               $nodeName = $this->getStackerInstance()->getNamed('announcement-answer');
+       /**
+        * Finishes the your-session-id
+        *
+        * @return      void
+        */
+       protected function finishYourSessionId () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
+       }
 
-               $this->partialStub('TODO: Do something with the gathered data.');
+       /**
+        * Finishes the your-internal-ip
+        *
+        * @return      void
+        */
+       protected function finishYourInternalIp () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Getter for cache file (FQFN)
+        * Finishes the your-external-ip
         *
-        * @return      $fqfn   Full-qualified file name of the menu cache
+        * @return      void
         */
-       public function getAnnouncementAnswerCacheFqfn () {
-               $this->partialStub('Please implement this method.');
+       protected function finishYourExternalIp () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Starts the |||
+        * Finishes the your-data
         *
         * @return      void
         */
-       private function startAnnouncementAnswer () {
-               // Push the node name on the stacker
-               $this->getStackerInstance()->pushNamed('announcement-answer', '|||');
+       protected function finishYourData () {
+               // Pop the last entry
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 
        /**
-        * Finishes the |||
+        * Finishes the announcement-answer
         *
         * @return      void
         */
-       private function finishAnnouncementAnswer () {
+       protected function finishAnnouncementAnswer () {
                // Pop the last entry
-               $this->getStackerInstance()->popNamed('announcement-answer');
+               $this->getStackInstance()->popNamed('node_announcement_answer');
        }
 }