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 * @var string $fallback
72 * @param string $fallback Fallback class name (none if empty).
77 function BIFE_Parser($fallback = '') // ~X2C
79 $this->__construct($fallback);
87 * @param string $fallback Fallback class name (none if empty).
92 function __construct($fallback = '') // ~X2C
94 $this->stack = array();
96 $this->parser = xml_parser_create();
97 $this->fallback = $fallback;
98 xml_set_object($this->parser, $this);
99 xml_set_element_handler($this->parser, 'startElement', 'endElement');
100 xml_set_character_data_handler($this->parser, 'characterData');
111 function __destruct() // ~X2C
113 xml_parser_free($this->parser);
119 * XML parser start of element handler.
121 * @param resource $parser XML parser resource.
122 * @param string $name XML tag name.
123 * @param array $attrs XML tag attributes.
128 function startElement($parser, $name, $attrs) // ~X2C
130 $class = "BIFE_$name";
131 if (class_exists($class)) {
132 $obj =& new $class($attrs);
133 if (!is_a($obj, 'bife_widget')) {
134 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
136 $this->stack[] =& $obj;
138 if ($this->fallback) {
139 $class = $this->fallback;
140 $obj =& new $class($name, $attrs);
141 if (!is_a($obj, 'bife_fallback')) {
142 trigger_error("Class '$class' is not a BIFE_Fallback.", E_USER_WARNING);
144 $this->stack[] =& $obj;
146 trigger_error("Class not found '$class'.", E_USER_ERROR);
154 * XML parser end of element handler.
156 * @param resource $parser XML parser resource.
157 * @param string $name XML tag name.
162 function endElement($parser, $name) // ~X2C
165 $current =& $this->stack[key($this->stack)];
166 array_pop($this->stack);
168 $parent =& $this->stack[key($this->stack)];
170 $parent->addContents($current);
172 $this->root =& $current;
179 * XML parser character data handler.
181 * @param resource $parser XML parser resource.
182 * @param string $data XML character data.
187 function characterData($parser, $data) // ~X2C
190 $current =& $this->stack[key($this->stack)];
191 $current->addContents($data);
197 * Parse a string with XML data.
199 * @param string $data XML string to parse.
200 * @param bool $final Indicates if is the last string to parse.
205 function parse($data, $final = true) // ~X2C
207 if (!xml_parse($this->parser, $data, $final)) {
209 sprintf('XML error: %s at line %d.',
210 xml_error_string(xml_get_error_code($this->parser)),
211 xml_get_current_line_number($this->parser)
221 * Parse a XML file with a complete and valid XML document.
223 * @param string $filename Filename to parse.
225 * @return &BIFE_Widget
228 function &parseFile($filename) // ~X2C
230 if ($fp = @fopen($filename, "r")) {
231 while ($data = fread($fp, 4096)) {
232 $this->parse($data, feof($fp));
235 trigger_error("Could not open BIFE XML input file '$filename'.",
246 * Parse a XML string with a complete and valid XML document.
248 * @param string $data XML data to parse.
250 * @return &BIFE_Widget
253 function &parseString($data) // ~X2C
255 $this->parse($data, true);
260 } // -X2C Class :Parser