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 @include_once 'BIFE/' . ucfirst(strtolower($name)) . '.php';
134 if (class_exists($class)) {
135 $obj =& new $class($attrs);
136 if (!is_a($obj, 'bife_widget')) {
137 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
139 $this->stack[] =& $obj;
141 if ($this->fallback) {
142 $class = $this->fallback;
143 $obj =& new $class($name, $attrs);
144 if (!is_a($obj, 'bife_fallback')) {
145 trigger_error("Class '$class' is not a BIFE_Fallback.", E_USER_WARNING);
147 $this->stack[] =& $obj;
149 trigger_error("Class not found '$class'.", E_USER_ERROR);
157 * XML parser end of element handler.
159 * @param resource $parser XML parser resource.
160 * @param string $name XML tag name.
165 function endElement($parser, $name) // ~X2C
168 $current =& $this->stack[key($this->stack)];
169 array_pop($this->stack);
171 $parent =& $this->stack[key($this->stack)];
173 $parent->addContents($current);
175 $this->root =& $current;
182 * XML parser character data handler.
184 * @param resource $parser XML parser resource.
185 * @param string $data XML character data.
190 function characterData($parser, $data) // ~X2C
193 $current =& $this->stack[key($this->stack)];
194 $current->addContents($data);
200 * Parse a string with XML data.
202 * @param string $data XML string to parse.
203 * @param bool $final Indicates if is the last string to parse.
208 function parse($data, $final = true) // ~X2C
210 if (!xml_parse($this->parser, $data, $final)) {
212 sprintf('XML error: %s at line %d.',
213 xml_error_string(xml_get_error_code($this->parser)),
214 xml_get_current_line_number($this->parser)
224 * Parse a XML file with a complete and valid XML document.
226 * @param string $filename Filename to parse.
228 * @return &BIFE_Widget
231 function &parseFile($filename) // ~X2C
233 if ($fp = @fopen($filename, "r")) {
234 while ($data = fread($fp, 4096)) {
235 $this->parse($data, feof($fp));
238 trigger_error("Could not open BIFE XML input file '$filename'.",
249 * Parse a XML string with a complete and valid XML document.
251 * @param string $data XML data to parse.
253 * @return &BIFE_Widget
256 function &parseString($data) // ~X2C
258 $this->parse($data, true);
263 } // -X2C Class :Parser