3 * The own template engine for loading caching and sending out images
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('image' is ignored)
28 private $mainNodes = array("base", "type", "resolution", "background-color", "foreground-color", "image-string");
31 * Sub nodes in the XML tree
33 private $subNodes = array("name", "string-name", "x", "y", "font-size", "width", "height", "red", "green", "blue", "text");
38 private $imageInstance = null;
43 private $currMainNode = "";
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
54 // Set part description
55 $this->setObjectDescription("Image template engine");
57 // Create unique ID number
58 $this->generateUniqueId();
62 * Creates an instance of the class TemplateEngine and prepares it for usage
64 * @param $basePath The local base path for all templates
65 * @param $langInstance An instance of LanguageSystem (default)
66 * @param $ioInstance An instance of FileIoHandler (default, middleware!)
67 * @return $tplInstance An instance of TemplateEngine
68 * @throws BasePathIsEmptyException If the provided $basePath is empty
69 * @throws InvalidBasePathStringException If $basePath is no string
70 * @throws BasePathIsNoDirectoryException If $basePath is no
71 * directory or not found
72 * @throws BasePathReadProtectedException If $basePath is
75 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
77 $tplInstance = new ImageTemplateEngine();
79 // Is the base path valid?
80 if (empty($basePath)) {
82 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
83 } elseif (!is_string($basePath)) {
85 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
86 } elseif (!is_dir($basePath)) {
88 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
89 } elseif (!is_readable($basePath)) {
91 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
94 // Get configuration instance
95 $cfgInstance = FrameworkConfiguration::getInstance();
98 $tplInstance->setBasePath($basePath);
100 // Set the language and IO instances
101 $tplInstance->setLanguageInstance($langInstance);
102 $tplInstance->setFileIoInstance($ioInstance);
104 // Set template extensions
105 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
106 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
108 // Absolute output path for compiled templates
109 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
111 // Return the prepared instance
116 * Renders the given image content
118 * @param $imageContent A valid XML image content
120 * @throws XmlParserException If an XML error was found
122 public function renderImageContent ($imageContent) {
124 $xmlParser = xml_parser_create();
126 // Force case-folding to on
127 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
130 xml_set_object($xmlParser, $this);
132 // Set handler call-backs
133 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
134 xml_set_character_data_handler($xmlParser, 'characterHandler');
136 // Now parse the XML tree
137 if (!xml_parse($xmlParser, $imageContent)) {
138 // Error found in XML!
139 throw new XmlParserException(array($this, $xmlParser), BaseHelper::EXCEPTION_XML_PARSER_ERROR);
143 xml_parser_free($xmlParser);
147 * Handles the start element of an XML resource
149 * @param $resource XML parser resource (currently ignored)
150 * @param $element The element we shall handle
151 * @param $attributes All attributes
153 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
155 public function startElement ($resource, $element, array $attributes) {
156 // Initial method name which will never be called...
157 $methodName = 'initImage';
159 // Make the element name lower-case
160 $element = strtolower($element);
162 // Is the element a main node?
163 //* DEBUG: */ echo "START: >".$element."<<br />\n";
164 if (in_array($element, $this->mainNodes)) {
165 // Okay, main node found!
166 $methodName = 'setImage' . $this->convertToClassName($element);
167 } elseif (in_array($element, $this->subNodes)) {
169 $methodName = 'setImageProperty' . $this->convertToClassName($element);
170 } elseif ($element != 'image') {
171 // Invalid node name found
172 throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
176 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
177 call_user_func_array(array($this, $methodName), $attributes);
181 * Ends the main or sub node by sending out the gathered data
183 * @param $resource An XML resource pointer (currently ignored)
184 * @param $nodeName Name of the node we want to finish
186 * @throws XmlNodeMismatchException If current main node mismatches the closing one
188 public function endElement ($resource, $nodeName) {
189 // Make all lower-case
190 $nodeName = strtolower($nodeName);
192 // Does this match with current main node?
193 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
194 if (($nodeName != $this->currMainNode) && (in_array($nodeName, $this->mainNodes))) {
196 throw new XmlNodeMismatchException (array($this, $nodeName, $this->currMainNode), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
197 } elseif (in_array($nodeName, $this->subNodes)) {
198 // Silently ignore sub nodes
202 // Construct method name
203 $methodName = 'finish' . $this->convertToClassName($nodeName);
205 // Call the corresponding method
206 call_user_func_array(array($this->imageInstance, $methodName), array());
212 * @param $resource XML parser resource (currently ignored)
213 * @param $characters Characters to handle
215 * @todo Find something usefull with this!
217 public function characterHandler ($resource, $characters) {
218 // Trim all spaces away
219 $characters = trim($characters);
221 // Is this string empty?
222 if (empty($characters)) {
223 // Then skip it silently
228 $this->partialStub("Handling extra characters is not yet supported!");
232 * Intializes the image
235 * @todo Add cache creation here
237 private function initImage () {
244 * @param $imageType Code fragment or direct value holding the image type
247 private function setImageType ($imageType) {
248 // Set group to general
249 $this->setVariableGroup('general');
251 // Try to compile it first to get the value from variable stack
252 $imageType = $this->compileRawCode($imageType);
254 // Now make a class name of it
255 $className = $this->convertToClassName($imageType.'_image');
257 // And try to initiate it
258 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
260 // Set current main node to type
261 $this->currMainNode = 'type';
265 * "Setter" for resolution, we first need to collect the resolution from the
266 * sub-nodes. So first, this method will prepare an array for it
270 private function setImageResolution () {
271 // Call the image class
272 $this->imageInstance->initResolution();
274 // Current main node is resolution
275 $this->currMainNode = 'resolution';
279 * "Setter" for base information. For more details see above method!
282 * @see ImageTemplateEngine::setImageResolution
284 private function setImageBase () {
285 // Call the image class
286 $this->imageInstance->initBase();
288 // Current main node is resolution
289 $this->currMainNode = 'base';
293 * "Setter" for background-color. For more details see above method!
296 * @see ImageTemplateEngine::setImageResolution
298 private function setImageBackgroundColor () {
299 // Call the image class
300 $this->imageInstance->initBackgroundColor();
302 // Current main node is background-color
303 $this->currMainNode = 'background-color';
307 * "Setter" for foreground-color. For more details see above method!
310 * @see ImageTemplateEngine::setImageResolution
312 private function setImageForegroundColor () {
313 // Call the image class
314 $this->imageInstance->initForegroundColor();
316 // Current main node is foreground-color
317 $this->currMainNode = 'foreground-color';
321 * "Setter" for image-string. For more details see above method!
323 * @param $groupable Wether this image string is groupable
325 * @see ImageTemplateEngine::setImageResolution
327 private function setImageImageString ($groupable = 'single') {
328 // Call the image class
329 $this->imageInstance->initImageString($groupable);
331 // Current main node is foreground-color
332 $this->currMainNode = 'image-string';
336 * Setter for image name
338 * @param $imageName Name of the image
341 private function setImagePropertyName ($imageName) {
342 // Call the image class
343 $this->imageInstance->setImageName($imageName);
347 * Setter for image width
349 * @param $width Width of the image or variable
352 private function setImagePropertyWidth ($width) {
353 // Call the image class
354 $this->imageInstance->setWidth($width);
358 * Setter for image height
360 * @param $height Height of the image or variable
363 private function setImagePropertyHeight ($height) {
364 // Call the image class
365 $this->imageInstance->setHeight($height);
369 * Setter for image red color
371 * @param $red Red color value
374 private function setImagePropertyRed ($red) {
375 // Call the image class
376 $this->imageInstance->setRed($red);
380 * Setter for image green color
382 * @param $green Green color value
385 private function setImagePropertyGreen ($green) {
386 // Call the image class
387 $this->imageInstance->setGreen($green);
391 * Setter for image blue color
393 * @param $blue Blue color value
396 private function setImagePropertyBlue ($blue) {
397 // Call the image class
398 $this->imageInstance->setBlue($blue);
402 * Setter for string name (identifier)
404 * @param $stringName String name (identifier)
407 private function setImagePropertyStringName ($stringName) {
408 // Call the image class
409 $this->imageInstance->setStringName($stringName);
413 * Setter for font size
415 * @param $fontSize Size of the font
418 private function setImagePropertyFontSize ($fontSize) {
419 // Call the image class
420 $this->imageInstance->setFontSize($fontSize);
424 * Setter for image string
426 * @param $imageString Image string to set
429 private function setImagePropertyText ($imageString) {
430 // Call the image class
431 $this->imageInstance->setString($imageString);
435 * Setter for X coordinate
437 * @param $x X coordinate
440 private function setImagePropertyX ($x) {
441 // Call the image class
442 $this->imageInstance->setX($x);
446 * Setter for Y coordinate
448 * @param $y Y coordinate
451 private function setImagePropertyY ($y) {
452 // Call the image class
453 $this->imageInstance->setY($y);
457 * Getter for image cache file (FQFN)
459 * @return $fqfn Full-qualified file name of the image cache
461 public function getImageCacheFqfn () {
462 // Get the FQFN ready
463 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
470 * Outputs the image to the world
472 * @param $responseInstance An instance of a Responseable class
475 public function transferToResponse (Responseable $responseInstance) {
476 // Set the image instance
477 $responseInstance->setImageInstance($this->imageInstance);