2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +--------------------------------------------------------------------+
4 // | BIFE - Buil It FastEr |
5 // +--------------------------------------------------------------------+
6 // | This file is part of BIFE. |
8 // | BIFE is free software; you can redistribute it and/or modify it |
9 // | under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
13 // | BIFE is distributed in the hope that it will be useful, but |
14 // | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 // | General Public License for more details. |
18 // | You should have received a copy of the GNU General Public License |
19 // | along with Hooks; if not, write to the Free Software Foundation, |
20 // | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 // +--------------------------------------------------------------------+
22 // | Created: Wed May 17 18:16:54 ART 2003 |
23 // | Authors: Leandro Lucarella <luca@lugmen.org.ar> |
24 // +--------------------------------------------------------------------+
29 // +X2C Class 25 :Parser
31 * This is the XML Parser.
39 * @var BIFE_Widget $root
45 * XML parser resource.
47 * @var resource $parser
61 * Fallback class to use in case that a widget class is not found.
63 * @var string $fallback
69 * XML cache directory. Empty if no cahching must be done (for current dir use '.').
77 * Files required by the parsed XML file.
79 * @var array $requires
82 var $requires = array();
90 * @param string $fallback Fallback class name (none if empty).
91 * @param string $cache XML cache directory. Empty is no caching will be done.
96 function BIFE_Parser($fallback = '', $cache = '/tmp') // ~X2C
98 $this->__construct($fallback, $cache);
106 * @param string $fallback Fallback class name (none if empty).
107 * @param string $cache XML cache directory. Empty is no caching will be done.
112 function __construct($fallback = '', $cache = '/tmp') // ~X2C
114 $this->parser = xml_parser_create();
115 $this->fallback = $fallback;
116 $this->cache = $cache;
117 xml_set_object($this->parser, $this);
118 xml_set_element_handler($this->parser, 'startElement', 'endElement');
119 xml_set_character_data_handler($this->parser, 'characterData');
130 function __destruct() // ~X2C
132 xml_parser_free($this->parser);
138 * XML parser start of element handler.
140 * @param resource $parser XML parser resource.
141 * @param string $name XML tag name.
142 * @param array $attrs XML tag attributes.
147 function startElement($parser, $name, $attrs) // ~X2C
149 $class = 'bife_' . strtolower(strtr($name, ':', '_'));
150 if (!class_exists($class)) {
152 strtr(ucwords(strtr(strtolower($name), ':', ' ')), ' ', '/') .
154 if (@include_once $inc) {
155 $this->includes[] = $inc;
158 if (class_exists($class)) {
159 $obj =& new $class($attrs);
160 // XXX - Does this check make sense?
161 if (!is_a($obj, 'bife_widget')) {
162 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
164 $this->stack[] =& $obj;
166 if ($this->fallback) {
167 $class = $this->fallback;
168 $obj =& new $class($name, $attrs);
169 if (!is_a($obj, 'bife_fallback')) {
170 trigger_error("Class '$class' is not a BIFE_Fallback.", E_USER_WARNING);
172 $this->stack[] =& $obj;
174 trigger_error("Class not found '$class'.", E_USER_ERROR);
182 * XML parser end of element handler.
184 * @param resource $parser XML parser resource.
185 * @param string $name XML tag name.
190 function endElement($parser, $name) // ~X2C
193 $current =& $this->stack[key($this->stack)];
194 array_pop($this->stack);
196 $parent =& $this->stack[key($this->stack)];
198 $parent->addContents($current);
200 $this->root =& $current;
207 * XML parser character data handler.
209 * @param resource $parser XML parser resource.
210 * @param string $data XML character data.
215 function characterData($parser, $data) // ~X2C
218 $current =& $this->stack[key($this->stack)];
219 $current->addContents($data);
225 * Parse a string with XML data.
227 * @param string $data XML string to parse.
228 * @param bool $final Indicates if is the last string to parse.
233 function parse($data, $final = true) // ~X2C
235 if (!xml_parse($this->parser, $data, $final)) {
237 sprintf('XML error: %s at line %d.',
238 xml_error_string(xml_get_error_code($this->parser)),
239 xml_get_current_line_number($this->parser)
249 * Parse a XML file with a complete and valid XML document.
251 * @param string $filename Filename to parse.
253 * @return &BIFE_Widget
256 function &parseFile($filename) // ~X2C
259 $cache = $this->cache . '/' . 'bife_parser_cache' . strtr(realpath($filename), '/', '_');
260 if (@filemtime($cache) > @filemtime($filename)) {
261 $file = file($cache);
262 foreach(unserialize(trim(array_shift($file))) as $required) {
263 include_once $required;
265 return unserialize(join('', $file));
268 if ($fp = @fopen($filename, 'r')) {
269 while ($data = fread($fp, 4096)) {
270 $this->parse($data, feof($fp));
273 trigger_error("Could not open BIFE XML input file '$filename'.",
278 $fp = fopen($cache, 'w');
279 fputs($fp, serialize($this->includes) . "\n");
280 fputs($fp, serialize($this->root));
289 * Parse a XML string with a complete and valid XML document.
291 * @param string $data XML data to parse.
293 * @return &BIFE_Widget
296 function &parseString($data) // ~X2C
298 $this->parse($data, true);
303 } // -X2C Class :Parser