| // +--------------------------------------------------------------------+ // // $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 ?>