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
53 * Template to use to render the parsed file.
55 * @var HTML_Template_Sigma $template
77 function BIFE_Parser() // ~X2C
79 $this->__construct($template);
90 function __construct() // ~X2C
92 $this->stack = array();
94 $this->parser = xml_parser_create();
95 $this->template =& $template;
96 xml_set_object($this->parser, $this);
97 xml_set_element_handler($this->parser, 'startElement', 'endElement');
98 xml_set_character_data_handler($this->parser, 'characterData');
109 function __destruct() // ~X2C
111 xml_parser_free($this->parser);
117 * XML parser start of element handler.
119 * @param resource $parser XML parser resource.
120 * @param string $name XML tag name.
121 * @param array $attrs XML tag attributes.
126 function startElement($parser, $name, $attrs) // ~X2C
128 $class = "BIFE_$name";
129 if (!class_exists($class)) {
130 include_once 'BIFE/' . ucfirst(strtolower($name)) . '.php';
132 if (class_exists($class)) {
133 $obj =& new $class($attrs);
134 if (!is_a($obj, 'bife_widget')) {
135 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
137 $this->stack[] =& $obj;
139 trigger_error("Class not found '$class'.", E_USER_ERROR);
146 * XML parser end of element handler.
148 * @param resource $parser XML parser resource.
149 * @param string $name XML tag name.
154 function endElement($parser, $name) // ~X2C
157 $current =& $this->stack[key($this->stack)];
158 array_pop($this->stack);
160 $parent =& $this->stack[key($this->stack)];
162 $parent->addContents($current);
164 $this->root =& $current;
171 * XML parser character data handler.
173 * @param resource $parser XML parser resource.
174 * @param string $data XML character data.
179 function characterData($parser, $data) // ~X2C
182 $current =& $this->stack[key($this->stack)];
183 $current->addContents($data);
189 * Parse a string with XML data.
191 * @param string $data XML string to parse.
192 * @param bool $final Indicates if is the last string to parse.
197 function parse($data, $final = true) // ~X2C
199 if (!xml_parse($this->parser, $data, $final)) {
201 sprintf('XML error: %s at line %d.',
202 xml_error_string(xml_get_error_code($this->parser)),
203 xml_get_current_line_number($this->parser)
213 * Parse a XML file with a complete and valid XML document.
215 * @param string $filename Filename to parse.
217 * @return &BIFE_Widget
220 function &parseFile($filename) // ~X2C
222 if ($fp = @fopen($filename, "r")) {
223 while ($data = fread($fp, 4096)) {
224 $this->parse($data, feof($fp));
227 trigger_error("Could not open BIFE XML input file '$filename'.",
238 * Parse a XML string with a complete and valid XML document.
240 * @param string $data XML data to parse.
242 * @return &BIFE_Widget
245 function &parseString($data) // ~X2C
247 $this->parse($data, true);
252 } // -X2C Class :Parser