Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / filter / class_BaseHubFilter.php
1 <?php
2 /**
3  * A generic filter for hub project
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseHubFilter extends BaseFilter {
25         /**
26          * Array with all data XML nodes (which hold the actual data) and their values
27          */
28         protected $dataXmlNodes = array();
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Real name of class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39         }
40
41         /**
42          * Processes the given raw message content. The method renderXmlContent
43          * may throw (not the method itself) several exceptions:
44          *
45          * InvalidXmlNodeException  - If an invalid XML node has been found (e.g.
46          *                            wrong/out-dated template used)
47          * XmlNodeMismatchException - Again might be caused by invalid XML node
48          *                            usage
49          * XmlParserException       - If the XML message is damaged or not
50          *                            well-formed
51          *
52          * @param       $messageType            Type of message
53          * @param       $messageData            Raw message data array
54          * @param       $packageInstance        An instance of a Receivable class
55          * @return      void
56          * @todo        Exceptions from renderXmlContent() are currently unhandled
57          */
58         protected function genericProcessMessage ($messageType, array $messageData, Receivable $packageInstance) {
59                 // Make sure the wanted element is there
60                 assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE]));
61                 assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_SENDER]));
62                 assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_HASH]));
63                 assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_TAGS]));
64
65                 // Get a template instance from the factory
66                 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_' . $messageType . '_template_class');
67
68                 // Get message content
69                 $messageContent = $messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE];
70
71                 // And render the XML content (aka message)
72                 $templateInstance->renderXmlContent($messageContent);
73
74                 // Debug message
75                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Handling ' . strlen($messageContent) . ' bytes: ' . $messageContent);
76
77                 /*
78                  * The template system now stores all required data as 'general'
79                  * variables, so simply get them. If there is an invalid XML node
80                  * inside the message, the above method call will cause exceptions.
81                  */
82                 foreach ($this->dataXmlNodes as $key => $dummy) {
83                         // Call it
84                         $value = $templateInstance->readXmlData($key);
85
86                         /*
87                          * If value is NULL, a variable hasn't been found. This could mean
88                          * that *this* node is running an out-dated software or the other
89                          * peer is using an out-dated $messageType.xml template.
90                          */
91                         if (is_null($value)) {
92                                 // Output a warning
93                                 self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Found not fully supported variable ' . $key . ' - skipping.');
94
95                                 // Skip this part, don't write NULLs to the array
96                                 continue;
97                         } // END - if
98
99                         // Debug message
100                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: key=' . $key . ',value=' . $value);
101
102                         // Set it now
103                         $this->dataXmlNodes[$key] = $value;
104                 } // END - foreach
105
106                 // Construct an array for pushing it on next stack
107                 $messageArray = array(
108                         // Message data itself
109                         NetworkPackage::MESSAGE_ARRAY_DATA   => $this->dataXmlNodes,
110                         // Message type (which is $messageType)
111                         NetworkPackage::MESSAGE_ARRAY_TYPE   => $messageType,
112                         // Message sender
113                         NetworkPackage::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackage::PACKAGE_CONTENT_SENDER],
114                         // Package hash
115                         NetworkPackage::MESSAGE_ARRAY_HASH   => $messageData[NetworkPackage::PACKAGE_CONTENT_HASH],
116                         // Package tags
117                         NetworkPackage::MESSAGE_ARRAY_TAGS   => $messageData[NetworkPackage::PACKAGE_CONTENT_TAGS],
118                 );
119
120                 // Push the processed message back on stack
121                 $packageInstance->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE, $messageArray);
122         }
123 }
124
125 // [EOF]
126 ?>