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_Root $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 '.').
82 * @param string $fallback Fallback class name (none if empty).
83 * @param string $cache XML cache directory. Empty is no caching will be done.
88 function BIFE_Parser($fallback = '', $cache = '/tmp') // ~X2C
90 $this->__construct($fallback, $cache);
98 * @param string $fallback Fallback class name (none if empty).
99 * @param string $cache XML cache directory. Empty is no caching will be done.
104 function __construct($fallback = '', $cache = '/tmp') // ~X2C
106 $this->stack = array();
108 $this->parser = xml_parser_create();
109 $this->fallback = $fallback;
110 $this->cache = $cache;
111 xml_set_object($this->parser, $this);
112 xml_set_element_handler($this->parser, 'startElement', 'endElement');
113 xml_set_character_data_handler($this->parser, 'characterData');
124 function __destruct() // ~X2C
126 xml_parser_free($this->parser);
132 * XML parser start of element handler.
134 * @param resource $parser XML parser resource.
135 * @param string $name XML tag name.
136 * @param array $attrs XML tag attributes.
141 function startElement($parser, $name, $attrs) // ~X2C
143 $class = "BIFE_$name";
144 if (!class_exists($class)) {
145 @include_once 'BIFE/' . ucfirst(strtolower($name)) . '.php';
147 if (class_exists($class)) {
148 $obj =& new $class($attrs);
149 if (!is_a($obj, 'bife_widget')) {
150 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
152 $this->stack[] =& $obj;
154 if ($this->fallback) {
155 $class = $this->fallback;
156 $obj =& new $class($name, $attrs);
157 if (!is_a($obj, 'bife_fallback')) {
158 trigger_error("Class '$class' is not a BIFE_Fallback.", E_USER_WARNING);
160 $this->stack[] =& $obj;
162 trigger_error("Class not found '$class'.", E_USER_ERROR);
170 * XML parser end of element handler.
172 * @param resource $parser XML parser resource.
173 * @param string $name XML tag name.
178 function endElement($parser, $name) // ~X2C
181 $current =& $this->stack[key($this->stack)];
182 array_pop($this->stack);
184 $parent =& $this->stack[key($this->stack)];
186 $parent->addContents($current);
188 $this->root =& $current;
195 * XML parser character data handler.
197 * @param resource $parser XML parser resource.
198 * @param string $data XML character data.
203 function characterData($parser, $data) // ~X2C
206 $current =& $this->stack[key($this->stack)];
207 $current->addContents($data);
213 * Parse a string with XML data.
215 * @param string $data XML string to parse.
216 * @param bool $final Indicates if is the last string to parse.
221 function parse($data, $final = true) // ~X2C
223 if (!xml_parse($this->parser, $data, $final)) {
225 sprintf('XML error: %s at line %d.',
226 xml_error_string(xml_get_error_code($this->parser)),
227 xml_get_current_line_number($this->parser)
237 * Parse a XML file with a complete and valid XML document.
239 * @param string $filename Filename to parse.
244 function &parseFile($filename) // ~X2C
247 $cache = $this->cache . '/' . 'bife_parser_cache' . strtr(realpath($filename), '/', '_');
248 if (@filemtime($cache) > @filemtime($filename)) {
249 // FIXME - replace with file_get_contents()
250 $file = file($cache);
251 foreach(unserialize(trim(array_shift($file))) as $required) {
252 require_once $required;
254 return unserialize(join('', $file));
257 if ($fp = @fopen($filename, 'r')) {
258 while ($data = fread($fp, 4096)) {
259 $this->parse($data, feof($fp));
262 trigger_error("Could not open BIFE XML input file '$filename'.",
267 $fp = fopen($cache, 'w');
268 fputs($fp, serialize($this->root->getRequiredFiles()) . "\n");
269 fputs($fp, serialize($this->root));
279 * Parse a XML string with a complete and valid XML document.
281 * @param string $data XML data to parse.
286 function &parseString($data) // ~X2C
288 $this->parse($data, true);
293 } // -X2C Class :Parser