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("type", "resolution", "background-color", "foreground-color", "image-string");
31 * Sub nodes in the XML tree
33 private $subNodes = array("width", "height", "red", "green", "blue", "text");
38 private $imageInstance = null;
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
49 // Set part description
50 $this->setObjectDescription("Image template engine");
52 // Create unique ID number
53 $this->generateUniqueId();
56 $this->removeNumberFormaters();
57 $this->removeSystemArray();
61 * Creates an instance of the class TemplateEngine and prepares it for usage
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
74 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
76 $tplInstance = new ImageTemplateEngine();
78 // Is the base path valid?
79 if (empty($basePath)) {
81 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
82 } elseif (!is_string($basePath)) {
84 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
85 } elseif (!is_dir($basePath)) {
87 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
88 } elseif (!is_readable($basePath)) {
90 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
93 // Get configuration instance
94 $cfgInstance = FrameworkConfiguration::getInstance();
97 $tplInstance->setBasePath($basePath);
99 // Initialize the variable stack
100 $tplInstance->initVariableStack();
102 // Set the language and IO instances
103 $tplInstance->setLanguageInstance($langInstance);
104 $tplInstance->setFileIoInstance($ioInstance);
106 // Set template extensions
107 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
108 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
110 // Absolute output path for compiled templates
111 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
113 // Return the prepared instance
118 * Renders the given image content
120 * @param $imageContent A valid XML image content
122 * @throws XmlParserException If an XML error was found
124 public function renderImageContent ($imageContent) {
126 $xmlParser = xml_parser_create();
128 // Force case-folding to on
129 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
132 xml_set_object($xmlParser, $this);
134 // Set handler call-backs
135 xml_set_element_handler($xmlParser, "startElement", "endElement");
136 xml_set_character_data_handler($xmlParser, "characterHandler");
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);
145 xml_parser_free($xmlParser);
149 * Handles the start element of an XML resource
151 * @param $resource XML parser resource
152 * @param $element The element we shall handle
153 * @param $attributes All attributes
155 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
157 public function startElement ($resource, $element, array $attributes) {
158 // Initial method name which will never be called...
159 $methodName = "initImage";
161 // Make the element name lower-case
162 $element = strtolower($element);
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)) {
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);
177 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
178 call_user_func_array(array($this, $methodName), $attributes);
182 * Intializes the image
185 * @todo Add cache creation here
187 private function initImage () {
194 * @param $imageType Code fragment or direct value holding the image type
197 private function setImageType ($imageType) {
198 // Try to compile it first to get the value from variable stack
199 $imageType = $this->compileRawCode($imageType);
201 // Now make a class name of it
202 $className = $this->convertToClassName($imageType."_image");
204 // And try to initiate it
205 $this->imageInstance = ObjectFactory::createObjectByName($className);