+<?php
+// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
+// +--------------------------------------------------------------------+
+// | BIFE - Buil It Fast & Easy |
+// +--------------------------------------------------------------------+
+// | This file is part of BIFE. |
+// | |
+// | BIFE is free software; you can redistribute it and/or modify it |
+// | under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation; either version 2 of the License, or |
+// | (at your option) any later version. |
+// | |
+// | BIFE is distributed in the hope that it will be useful, but |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with Hooks; if not, write to the Free Software Foundation, |
+// | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
+// +--------------------------------------------------------------------+
+// | Created: @date
+// | Authors: @author
+// +--------------------------------------------------------------------+
+//
+// $Id$
+//
+
+// +X2C Class 25 :Parser
+/**
+ * This is the XML Parser.
+ *
+ * @access public
+ */
+class BIFE_Parser {
+ /**
+ * Output string.
+ *
+ * @var string $output
+ * @access public
+ */
+ var $output;
+
+ /**
+ * XML parser resource.
+ *
+ * @var resource $parser
+ * @access public
+ */
+ var $parser;
+
+ /**
+ * Template to use to render the parsed file.
+ *
+ * @var HTML_Template_Sigma $template
+ * @access public
+ */
+ var $template;
+
+ /**
+ * BIFE widgets stack.
+ *
+ * @var array $stack
+ * @access public
+ */
+ var $stack;
+
+ // ~X2C
+
+ // +X2C Operation 30
+ /**
+ * Constructor.
+ *
+ * @param HTML_Template_Sigma &$template Template to use to render the widgets.
+ *
+ * @return void
+ * @access public
+ */
+ function BIFE_Parser(&$template) // ~X2C
+ {
+ $this->__construct($template);
+ }
+ // -X2C
+
+ // +X2C Operation 31
+ /**
+ * Constructor.
+ *
+ * @param HTML_Template_Sigma &$template Template to use to render the widgets.
+ *
+ * @return void
+ * @access public
+ */
+ function __construct(&$template) // ~X2C
+ {
+ $this->stack = array();
+ $this->output = '';
+ $this->parser = xml_parser_create();
+ $this->template =& $template;
+ xml_set_object($this->parser, $this);
+ xml_set_element_handler($this->parser, 'startElement', 'endElement');
+ xml_set_character_data_handler($this->parser, 'characterData');
+ }
+ // -X2C
+
+ // +X2C Operation 32
+ /**
+ * Destructor.
+ *
+ * @return void
+ * @access public
+ */
+ function __destruct() // ~X2C
+ {
+ xml_parser_free($this->parser);
+ }
+ // -X2C
+
+ // +X2C Operation 33
+ /**
+ * XML parser start of element handler.
+ *
+ * @param resource $parser XML parser resource.
+ * @param string $name XML tag name.
+ * @param array $attrs XML tag attributes.
+ *
+ * @return void
+ * @access public
+ */
+ function startElement($parser, $name, $attrs) // ~X2C
+ {
+ $class = "BIFE_$name";
+ if (!class_exists($class)) {
+ include_once 'BIFE/' . ucfirst(strtolower($name)) . '.php';
+ }
+ if (class_exists($class)) {
+ $obj =& new $class($attrs);
+ if (!is_a($obj, 'bife_widget')) {
+ trigger_error("Class '$class' is not a BIFE_Widget.", E_USER_WARNING);
+ }
+ $this->stack[] =& $obj;
+ } else {
+ trigger_error("Class not found '$class'.", E_USER_ERROR);
+ }
+ }
+ // -X2C
+
+ // +X2C Operation 34
+ /**
+ * XML parser end of element handler.
+ *
+ * @param resource $parser XML parser resource.
+ * @param string $name XML tag name.
+ *
+ * @return void
+ * @access public
+ */
+ function endElement($parser, $name) // ~X2C
+ {
+ end($this->stack);
+ $current =& $this->stack[key($this->stack)];
+ array_pop($this->stack);
+ end($this->stack);
+ $parent =& $this->stack[key($this->stack)];
+ if ($parent) {
+ $parent->addContents($current->render($this->template));
+ } else {
+ $this->output = $current->render($this->template);
+ }
+ }
+ // -X2C
+
+ // +X2C Operation 35
+ /**
+ * XML parser character data handler.
+ *
+ * @param resource $parser XML parser resource.
+ * @param string $data XML character data.
+ *
+ * @return void
+ * @access public
+ */
+ function characterData($parser, $data) // ~X2C
+ {
+ end($this->stack);
+ $current =& $this->stack[key($this->stack)];
+ $current->addContents($data);
+ }
+ // -X2C
+
+ // +X2C Operation 36
+ /**
+ * Parse a string with XML data.
+ *
+ * @param string $data XML string to parse.
+ * @param bool $final Indicates if is the last string to parse.
+ *
+ * @return void
+ * @access public
+ */
+ function parse($data, $final = true) // ~X2C
+ {
+ if (!xml_parse($this->parser, $data, $final)) {
+ trigger_error(
+ sprintf('XML error: %s at line %d.',
+ xml_error_string(xml_get_error_code($this->parser)),
+ xml_get_current_line_number($this->parser)
+ ),
+ E_USER_WARNING
+ );
+ }
+ }
+ // -X2C
+
+ // +X2C Operation 37
+ /**
+ * Parse a XML file returning the rendered output.
+ *
+ * @param string $filename Filename to parse.
+ *
+ * @return void
+ * @access public
+ */
+ function parseFile($filename) // ~X2C
+ {
+ if ($fp = @fopen($filename, "r")) {
+ while ($data = fread($fp, 4096)) {
+ $this->parse($data, feof($fp));
+ }
+ } else {
+ trigger_error("Could not open BIFE XML input file '$filename'.",
+ E_USER_WARNING);
+ }
+ fclose($fp);
+ }
+ // -X2C
+
+ // +X2C Operation 38
+ /**
+ * Get rendered output.
+ *
+ * @return string
+ * @access public
+ */
+ function getOutput() // ~X2C
+ {
+ return $this->output;
+ }
+ // -X2C
+
+} // -X2C Class :Parser
+
+?>