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();
61 $this->removeNumberFormaters();
62 $this->removeSystemArray();
66 * Creates an instance of the class TemplateEngine and prepares it for usage
68 * @param $basePath The local base path for all templates
69 * @param $langInstance An instance of LanguageSystem (default)
70 * @param $ioInstance An instance of FileIoHandler (default, middleware!)
71 * @return $tplInstance An instance of TemplateEngine
72 * @throws BasePathIsEmptyException If the provided $basePath is empty
73 * @throws InvalidBasePathStringException If $basePath is no string
74 * @throws BasePathIsNoDirectoryException If $basePath is no
75 * directory or not found
76 * @throws BasePathReadProtectedException If $basePath is
79 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
81 $tplInstance = new ImageTemplateEngine();
83 // Is the base path valid?
84 if (empty($basePath)) {
86 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
87 } elseif (!is_string($basePath)) {
89 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
90 } elseif (!is_dir($basePath)) {
92 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
93 } elseif (!is_readable($basePath)) {
95 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
98 // Get configuration instance
99 $cfgInstance = FrameworkConfiguration::getInstance();
102 $tplInstance->setBasePath($basePath);
104 // Set the language and IO instances
105 $tplInstance->setLanguageInstance($langInstance);
106 $tplInstance->setFileIoInstance($ioInstance);
108 // Set template extensions
109 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
110 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
112 // Absolute output path for compiled templates
113 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
115 // Return the prepared instance
120 * Renders the given image content
122 * @param $imageContent A valid XML image content
124 * @throws XmlParserException If an XML error was found
126 public function renderImageContent ($imageContent) {
128 $xmlParser = xml_parser_create();
130 // Force case-folding to on
131 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
134 xml_set_object($xmlParser, $this);
136 // Set handler call-backs
137 xml_set_element_handler($xmlParser, "startElement", "endElement");
138 xml_set_character_data_handler($xmlParser, "characterHandler");
140 // Now parse the XML tree
141 if (!xml_parse($xmlParser, $imageContent)) {
142 // Error found in XML!
143 throw new XmlParserException(array($this, $xmlParser), BaseHelper::EXCEPTION_XML_PARSER_ERROR);
147 xml_parser_free($xmlParser);
151 * Handles the start element of an XML resource
153 * @param $resource XML parser resource (currently ignored)
154 * @param $element The element we shall handle
155 * @param $attributes All attributes
157 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
159 public function startElement ($resource, $element, array $attributes) {
160 // Initial method name which will never be called...
161 $methodName = "initImage";
163 // Make the element name lower-case
164 $element = strtolower($element);
166 // Is the element a main node?
167 //* DEBUG: */ echo "START: >".$element."<<br />\n";
168 if (in_array($element, $this->mainNodes)) {
169 // Okay, main node found!
170 $methodName = "setImage" . $this->convertToClassName($element);
171 } elseif (in_array($element, $this->subNodes)) {
173 $methodName = "setImageProperty" . $this->convertToClassName($element);
174 } elseif ($element != "image") {
175 // Invalid node name found
176 throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
180 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
181 call_user_func_array(array($this, $methodName), $attributes);
185 * Ends the main or sub node by sending out the gathered data
187 * @param $resource An XML resource pointer (currently ignored)
188 * @param $nodeName Name of the node we want to finish
190 * @throws XmlNodeMismatchException If current main node mismatches the closing one
192 public function endElement ($resource, $nodeName) {
193 // Make all lower-case
194 $nodeName = strtolower($nodeName);
196 // Does this match with current main node?
197 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
198 if (($nodeName != $this->currMainNode) && (in_array($nodeName, $this->mainNodes))) {
200 throw new XmlNodeMismatchException (array($this, $nodeName, $this->currMainNode), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
201 } elseif (in_array($nodeName, $this->subNodes)) {
202 // Silently ignore sub nodes
206 // Construct method name
207 $methodName = "finish" . $this->convertToClassName($nodeName);
209 // Call the corresponding method
210 call_user_func_array(array($this->imageInstance, $methodName), array());
216 * @param $resource XML parser resource (currently ignored)
217 * @param $characters Characters to handle
219 * @todo Find something usefull with this!
221 public function characterHandler ($resource, $characters) {
222 // Trim all spaces away
223 $characters = trim($characters);
225 // Is this string empty?
226 if (empty($characters)) {
227 // Then skip it silently
232 $this->partialStub("Handling extra characters is not yet supported!");
236 * Intializes the image
239 * @todo Add cache creation here
241 private function initImage () {
248 * @param $imageType Code fragment or direct value holding the image type
251 private function setImageType ($imageType) {
252 // Set group to general
253 $this->setVariableGroup("general");
255 // Try to compile it first to get the value from variable stack
256 $imageType = $this->compileRawCode($imageType);
258 // Now make a class name of it
259 $className = $this->convertToClassName($imageType."_image");
261 // And try to initiate it
262 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
264 // Set current main node to type
265 $this->currMainNode = "type";
269 * "Setter" for resolution, we first need to collect the resolution from the
270 * sub-nodes. So first, this method will prepare an array for it
274 private function setImageResolution () {
275 // Call the image class
276 $this->imageInstance->initResolution();
278 // Current main node is resolution
279 $this->currMainNode = "resolution";
283 * "Setter" for base information. For more details see above method!
286 * @see ImageTemplateEngine::setImageResolution
288 private function setImageBase () {
289 // Call the image class
290 $this->imageInstance->initBase();
292 // Current main node is resolution
293 $this->currMainNode = "base";
297 * "Setter" for background-color. For more details see above method!
300 * @see ImageTemplateEngine::setImageResolution
302 private function setImageBackgroundColor () {
303 // Call the image class
304 $this->imageInstance->initBackgroundColor();
306 // Current main node is background-color
307 $this->currMainNode = "background-color";
311 * "Setter" for foreground-color. For more details see above method!
314 * @see ImageTemplateEngine::setImageResolution
316 private function setImageForegroundColor () {
317 // Call the image class
318 $this->imageInstance->initForegroundColor();
320 // Current main node is foreground-color
321 $this->currMainNode = "foreground-color";
325 * "Setter" for image-string. For more details see above method!
327 * @param $groupable Wether this image string is groupable
329 * @see ImageTemplateEngine::setImageResolution
331 private function setImageImageString ($groupable = "single") {
332 // Call the image class
333 $this->imageInstance->initImageString($groupable);
335 // Current main node is foreground-color
336 $this->currMainNode = "image-string";
340 * Setter for image name
342 * @param $imageName Name of the image
345 private function setImagePropertyName ($imageName) {
346 // Call the image class
347 $this->imageInstance->setImageName($imageName);
351 * Setter for image width
353 * @param $width Width of the image or variable
356 private function setImagePropertyWidth ($width) {
357 // Call the image class
358 $this->imageInstance->setWidth($width);
362 * Setter for image height
364 * @param $height Height of the image or variable
367 private function setImagePropertyHeight ($height) {
368 // Call the image class
369 $this->imageInstance->setHeight($height);
373 * Setter for image red color
375 * @param $red Red color value
378 private function setImagePropertyRed ($red) {
379 // Call the image class
380 $this->imageInstance->setRed($red);
384 * Setter for image green color
386 * @param $green Green color value
389 private function setImagePropertyGreen ($green) {
390 // Call the image class
391 $this->imageInstance->setGreen($green);
395 * Setter for image blue color
397 * @param $blue Blue color value
400 private function setImagePropertyBlue ($blue) {
401 // Call the image class
402 $this->imageInstance->setBlue($blue);
406 * Setter for string name (identifier)
408 * @param $stringName String name (identifier)
411 private function setImagePropertyStringName ($stringName) {
412 // Call the image class
413 $this->imageInstance->setStringName($stringName);
417 * Setter for font size
419 * @param $fontSize Size of the font
422 private function setImagePropertyFontSize ($fontSize) {
423 // Call the image class
424 $this->imageInstance->setFontSize($fontSize);
428 * Setter for image string
430 * @param $imageString Image string to set
433 private function setImagePropertyText ($imageString) {
434 // Call the image class
435 $this->imageInstance->setString($imageString);
439 * Setter for X coordinate
441 * @param $x X coordinate
444 private function setImagePropertyX ($x) {
445 // Call the image class
446 $this->imageInstance->setX($x);
450 * Setter for Y coordinate
452 * @param $y Y coordinate
455 private function setImagePropertyY ($y) {
456 // Call the image class
457 $this->imageInstance->setY($y);
461 * Getter for image cache file (FQFN)
463 * @return $fqfn Full-qualified file name of the image cache
465 public function getImageCacheFqfn () {
466 // Get the FQFN ready
467 $fqfn = $this->getBasePath()."_cache/" . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
474 * Outputs the image to the world
476 * @param $responseInstance An instance of a Responseable class
479 public function transferToResponse (Responseable $responseInstance) {
480 // Set the image instance
481 $responseInstance->setImageInstance($this->imageInstance);