- <?
- /**
- * @package sax
- * @filesource
- * @see HTML_SAX_COMPONENT_PATH.'/Sax.php'
- * @copyright (c) http://Finn-Rasmussen.com
- * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
- * @author http://Finn-Rasmussen.com
- * @version 1.9
- * @since 21-oct-2005
- */
-
- /**
- * The required files
- */
- require_once(HTML_PATH.'/Object.php');
- if (defined('HTML_LOG_UTIL_PATH')) {
- require_once(HTML_LOG_UTIL_PATH.'/Log.php');
- }
-
- /**
- * Used to parse an xml file utilizing the SAX model
- * <code>
- * Usage:
- * $sax = new Sax($file); // call parse() from constructor
- * or:
- * $sax = new Sax();
- * $sax->parse($file);
- * or:
- * Sax::display($file);
- * </code>
- * @package sax
- */
-
- class Sax extends Object {
- /**
- * @var String $xml_parser The xml parser instance
- */
- var $xml_parser = NULL;
-
- /**
- * @var String $currentTag The current tag
- */
- var $currentTag = '';
-
- /**
- * @var String $currentAttribs The current attibute
- */
- var $currentAttribs = '';
-
- /**
- * @var String $xml The parsed xml
- */
- var $xml = '';
-
- /**
- * Constructor
- * @param String $file The xml file to parse
- */
- function Sax($file='') {
- $this->Object();
- if ($file!='') {
- $this->parse($file);
- }
- }
-
- /**
- * Parse the specified xml file, and call the handlers
- * @param String $file The file to read and parse as xml
- */
- function parse($file) {
- $this->xml_parser = xml_parser_create();
- xml_set_object($this->xml_parser, $this);
- $casefold = xml_parser_get_option($this->xml_parser, XML_OPTION_CASE_FOLDING);
- if ($casefold == 1) {
- xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true);
- }
- $targetEnc = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING);
- xml_parser_set_option($this->xml_parser,XML_OPTION_SKIP_WHITE,1);
- xml_set_element_handler($this->xml_parser, 'startElement', 'endElement');
- xml_set_character_data_handler($this->xml_parser, 'characterData');
- xml_set_default_handler($this->xml_parser, 'defaultData');
-
- // Open xml file, silently ignore any errors
- $fp = @fopen($file,'r');
- if (!$fp) {
- if (defined('ENIRO_DK_PINCODE')) {
- $file = str_replace(ENIRO_DK_PINCODE, 'REMOVED=', $file);
- $msg = $this->getClassName().'->parse(), Cannot open Eniro xml file='.$file;
- if (defined('HTML_LOG_UTIL_PATH')) {
- Log::warn(__FILE__,__LINE__,$msg);
- $this->xml .= $this->getMsg(LOG_LEVEL_WARN, $msg);
- } else {
- $this->xml .= $msg;
- }
- } else {
- $msg = $this->getClassName().'->parse(), Cannot open xml file='.$file;
- if (defined('HTML_LOG_UTIL_PATH')) {
- Log::warn(__FILE__,__LINE__,$msg);
- $this->xml .= $this->getMsg(LOG_LEVEL_WARN, $msg);
- } else {
- $this->xml .= $msg;
- }
- }
- } else {
- // Read and parse data
- $data = "";
- while (!feof($fp)) {
- $data .= fgets($fp, 4096);
- }
- // error handler
- if (!xml_parse($this->xml_parser, $data, feof($fp))) {
- die(sprintf("XML error: %s at line %d<br />\r\n$data",
- xml_error_string(xml_get_error_code($this->xml_parser)),
- xml_get_current_line_number($this->xml_parser)));
- }
- }
-
- $this->destroy();
- }
-
- /**
- * Destructor
- * Free the resources
- */
- function destroy() {
- if ($this->xml_parser!=NULL) {
- xml_parser_free($this->xml_parser);
- $this->xml_parser = NULL;
- }
- }
-
- /**
- * Used to catch the events for working with or handling
- * the start and end tags within the xml file.
- * Element events are issued whenever the xml parser encounters
- * start or end tags.
- * @param String $parser The xml parser
- * @param String $name The name of the element
- * @param String $attribs An array of attributes
- */
- function startElement($parser, $name, $attribs) {
- $this->currentTag = $name;
- $this->xml .= '<'.$name;
- foreach ($attribs as $key=>$value) {
- $this->xml .= ' '.$key.'="'.$value.'"';
- }
- $this->xml .= '>';
-
- }
-
- /**
- * Used to catch the event for handling the data.
- * Character data is rougly all the non-markup content
- * of xml documents.
- * @param String $parser The xml parser
- * @param String $name The data
- */
- function characterData($parser, $data) {
- $this->xml .= htmlspecialchars($data);
- }
-
- /**
- * Used to catch the event for handling the default data.
- * It will be called for each piece of xml that doesn't
- * have a set handler
- * @param String $parser The xml parser
- * @param String $data The default data
- */
- function defaultData($parser, $data) {
- $this->xml .= htmlspecialchars($data);
- }
-
- /**
- * Used to catch the event for end of element.
- * @param String $parser The xml parser
- * @param String $name The name of the element
- */
- function endElement($parser, $name) {
- $this->xml .= '</'.$name.">";
- }
-
- /**
- * Returns the xml file as html
- * @return String The xml converted to html
- */
- function getHtml() {
- $html = $this->html;
- if ($this->xml!='') {
- $html .= "<pre>\r\n";
- $html .= $this->xml;
- $html .= "</pre>\r\n";
- } else {
- if (defined('HTML_LOG_UTIL_PATH') && LOG_LEVEL & LOG_LEVEL_DEBUG) {
- $html .= '<!-- '.$this->getClassName()."->getHtml() No data found -->\r\n";
- } else {
- $html .= '<!-- '.$this->getClassName()."->getHtml() No data found -->\r\n";
- }
- }
- return $html;
- }
-
- /**
- * Display html
- * <code>
- * Usage:
- * Sax::display($file);
- * </code>
- * @static
- * @param String $file The file to parse
- */
- function display($file='') {
- $html = new Sax($file);
- $html->addHtml();
- }
-
- }
- ?>