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.
45 * XML parser resource.
47 * @var resource $parser
53 * Template to use to render the parsed file.
55 * @var HTML_Template_Sigma $template
74 * @param HTML_Template_Sigma &$template Template to use to render the widgets.
79 function BIFE_Parser(&$template) // ~X2C
81 $this->__construct($template);
89 * @param HTML_Template_Sigma &$template Template to use to render the widgets.
94 function __construct(&$template) // ~X2C
96 $this->stack = array();
98 $this->parser = xml_parser_create();
99 $this->template =& $template;
100 xml_set_object($this->parser, $this);
101 xml_set_element_handler($this->parser, 'startElement', 'endElement');
102 xml_set_character_data_handler($this->parser, 'characterData');
113 function __destruct() // ~X2C
115 xml_parser_free($this->parser);
121 * XML parser start of element handler.
123 * @param resource $parser XML parser resource.
124 * @param string $name XML tag name.
125 * @param array $attrs XML tag attributes.
130 function startElement($parser, $name, $attrs) // ~X2C
132 $class = "BIFE_$name";
133 if (!class_exists($class)) {
134 include_once 'BIFE/' . ucfirst(strtolower($name)) . '.php';
136 if (class_exists($class)) {
137 $obj =& new $class($attrs);
138 if (!is_a($obj, 'bife_widget')) {
139 trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
141 $this->stack[] =& $obj;
143 trigger_error("Class not found '$class'.", E_USER_ERROR);
150 * XML parser end of element handler.
152 * @param resource $parser XML parser resource.
153 * @param string $name XML tag name.
158 function endElement($parser, $name) // ~X2C
161 $current =& $this->stack[key($this->stack)];
162 array_pop($this->stack);
164 $parent =& $this->stack[key($this->stack)];
166 $parent->addContents($current->render($this->template));
168 $this->output = $current->render($this->template);
175 * XML parser character data handler.
177 * @param resource $parser XML parser resource.
178 * @param string $data XML character data.
183 function characterData($parser, $data) // ~X2C
186 $current =& $this->stack[key($this->stack)];
187 $current->addContents($data);
193 * Parse a string with XML data.
195 * @param string $data XML string to parse.
196 * @param bool $final Indicates if is the last string to parse.
201 function parse($data, $final = true) // ~X2C
203 if (!xml_parse($this->parser, $data, $final)) {
205 sprintf('XML error: %s at line %d.',
206 xml_error_string(xml_get_error_code($this->parser)),
207 xml_get_current_line_number($this->parser)
217 * Parse a XML file returning the rendered output.
219 * @param string $filename Filename to parse.
224 function parseFile($filename) // ~X2C
226 if ($fp = @fopen($filename, "r")) {
227 while ($data = fread($fp, 4096)) {
228 $this->parse($data, feof($fp));
231 trigger_error("Could not open BIFE XML input file '$filename'.",
240 * Get rendered output.
245 function getOutput() // ~X2C
247 return $this->output;
251 } // -X2C Class :Parser