6e80fd1a15b4b7598dc700a1135b57d075c2e06f
[shipsimu.git] / inc / classes / main / template / image / class_ImageTemplateEngine.php
1 <?php
2 /**
3  * The own template engine for loading caching and sending out images
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree ("image" is ignored)
27          */
28         private $mainNodes = array("type", "resolution", "background-color", "foreground-color", "image-string");
29
30         /**
31          * Sub nodes in the XML tree
32          */
33         private $subNodes = array("width", "height", "red", "green", "blue", "text");
34
35         /**
36          * Image instance
37          */
38         private $imageInstance = null;
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48
49                 // Set part description
50                 $this->setObjectDescription("Image template engine");
51
52                 // Create unique ID number
53                 $this->generateUniqueId();
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Creates an instance of the class TemplateEngine and prepares it for usage
62          *
63          * @param       $basePath               The local base path for all templates
64          * @param       $langInstance   An instance of LanguageSystem (default)
65          * @param       $ioInstance             An instance of FileIoHandler (default, middleware!)
66          * @return      $tplInstance    An instance of TemplateEngine
67          * @throws      BasePathIsEmptyException                If the provided $basePath is empty
68          * @throws      InvalidBasePathStringException  If $basePath is no string
69          * @throws      BasePathIsNoDirectoryException  If $basePath is no
70          *                                                                                      directory or not found
71          * @throws      BasePathReadProtectedException  If $basePath is
72          *                                                                                      read-protected
73          */
74         public final static function createImageTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
75                 // Get a new instance
76                 $tplInstance = new ImageTemplateEngine();
77
78                 // Is the base path valid?
79                 if (empty($basePath)) {
80                         // Base path is empty
81                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
82                 } elseif (!is_string($basePath)) {
83                         // Is not a string
84                         throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
85                 } elseif (!is_dir($basePath)) {
86                         // Is not a path
87                         throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
88                 } elseif (!is_readable($basePath)) {
89                         // Is not readable
90                         throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
91                 }
92
93                 // Get configuration instance
94                 $cfgInstance = FrameworkConfiguration::getInstance();
95
96                 // Set the base path
97                 $tplInstance->setBasePath($basePath);
98
99                 // Initialize the variable stack
100                 $tplInstance->initVariableStack();
101
102                 // Set the language and IO instances
103                 $tplInstance->setLanguageInstance($langInstance);
104                 $tplInstance->setFileIoInstance($ioInstance);
105
106                 // Set template extensions
107                 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
108                 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
109
110                 // Absolute output path for compiled templates
111                 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
112
113                 // Return the prepared instance
114                 return $tplInstance;
115         }
116
117         /**
118          * Renders the given image content
119          *
120          * @param       $imageContent           A valid XML image content
121          * @return      void
122          * @throws      XmlParserException      If an XML error was found
123          */
124         public function renderImageContent ($imageContent) {
125                 // Get an XML parser
126                 $xmlParser = xml_parser_create();
127
128                 // Force case-folding to on
129                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
130
131                 // Set object
132                 xml_set_object($xmlParser, $this);
133
134                 // Set handler call-backs
135                 xml_set_element_handler($xmlParser, "startElement", "endElement");
136                 xml_set_character_data_handler($xmlParser, "characterHandler");
137
138                 // Now parse the XML tree
139                 if (!xml_parse($xmlParser, $imageContent)) {
140                         // Error found in XML!
141                         throw new XmlParserException(array($this, $xmlParser), BaseHelper::EXCEPTION_XML_PARSER_ERROR);
142                 } // END - if
143
144                 // Free the parser
145                 xml_parser_free($xmlParser);
146         }
147
148         /**
149          * Handles the start element of an XML resource
150          *
151          * @param       $resource               XML parser resource
152          * @param       $element                The element we shall handle
153          * @param       $attributes             All attributes
154          * @return      void
155          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
156          */
157         public function startElement ($resource, $element, array $attributes) {
158                 // Initial method name which will never be called...
159                 $methodName = "initImage";
160
161                 // Make the element name lower-case
162                 $element = strtolower($element);
163
164                 // Is the element a main node?
165                 if (in_array($element, $this->mainNodes)) {
166                         // Okay, main node found!
167                         $methodName = "setImage" . $this->convertToClassName($element);
168                 } elseif (in_array($element, $this->subNodes)) {
169                         // Sub node found
170                         $methodName = "setImageProperty" . $this->convertToClassName($element);
171                 } elseif ($element != "image") {
172                         // Invalid node name found
173                         throw new InvalidXmlNodeException(array($this, $element, $attributes), self::EXCEPTION_XML_NODE_UNKNOWN);
174                 }
175
176                 // Call method
177                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
178                 call_user_func_array(array($this, $methodName), $attributes);
179         }
180
181         /**
182          * Intializes the image
183          *
184          * @return      void
185          * @todo        Add cache creation here
186          */
187         private function initImage () {
188                 // Unfinished work!
189         }
190
191         /**
192          * Set the image type
193          *
194          * @param       $imageType      Code fragment or direct value holding the image type
195          * @return      void
196          */
197         private function setImageType ($imageType) {
198                 // Try to compile it first to get the value from variable stack
199                 $imageType = $this->compileRawCode($imageType);
200
201                 // Now make a class name of it
202                 $className = $this->convertToClassName($imageType."_image");
203
204                 // And try to initiate it
205                 $this->imageInstance = ObjectFactory::createObjectByName($className);
206         }
207 }
208
209 // [EOF]
210 ?>