phpDocumentor sax
[ class tree: sax ] [ index: sax ] [ all elements ]

Source for file Sax.php

Documentation is available at Sax.php

  1. <?
  2. /**
  3. * @package sax
  4. * @filesource
  5. * @see HTML_SAX_COMPONENT_PATH.'/Sax.php'
  6. * @copyright (c) http://Finn-Rasmussen.com
  7. * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
  8. * @author http://Finn-Rasmussen.com
  9. * @version 1.9
  10. * @since 21-oct-2005
  11. */
  12.  
  13. /**
  14. * The required files
  15. */
  16. require_once(HTML_PATH.'/Object.php');
  17. if (defined('HTML_LOG_UTIL_PATH')) {
  18. require_once(HTML_LOG_UTIL_PATH.'/Log.php');
  19. }
  20.  
  21. /**
  22. * Used to parse an xml file utilizing the SAX model
  23. * <code>
  24. * Usage:
  25. * $sax = new Sax($file); // call parse() from constructor
  26. * or:
  27. * $sax = new Sax();
  28. * $sax->parse($file);
  29. * or:
  30. * Sax::display($file);
  31. * </code>
  32. * @package sax
  33. */
  34.  
  35. class Sax extends Object {
  36. /**
  37. * @var String $xml_parser The xml parser instance
  38. */
  39. var $xml_parser = NULL;
  40. /**
  41. * @var String $currentTag The current tag
  42. */
  43. var $currentTag = '';
  44. /**
  45. * @var String $currentAttribs The current attibute
  46. */
  47. var $currentAttribs = '';
  48. /**
  49. * @var String $xml The parsed xml
  50. */
  51. var $xml = '';
  52.  
  53. /**
  54. * Constructor
  55. * @param String $file The xml file to parse
  56. */
  57. function Sax($file='') {
  58. $this->Object();
  59. if ($file!='') {
  60. $this->parse($file);
  61. }
  62. }
  63.  
  64. /**
  65. * Parse the specified xml file, and call the handlers
  66. * @param String $file The file to read and parse as xml
  67. */
  68. function parse($file) {
  69. $this->xml_parser = xml_parser_create();
  70. xml_set_object($this->xml_parser, $this);
  71. $casefold = xml_parser_get_option($this->xml_parser, XML_OPTION_CASE_FOLDING);
  72. if ($casefold == 1) {
  73. xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true);
  74. }
  75. $targetEnc = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING);
  76. xml_parser_set_option($this->xml_parser,XML_OPTION_SKIP_WHITE,1);
  77. xml_set_element_handler($this->xml_parser, 'startElement', 'endElement');
  78. xml_set_character_data_handler($this->xml_parser, 'characterData');
  79. xml_set_default_handler($this->xml_parser, 'defaultData');
  80.  
  81. // Open xml file, silently ignore any errors
  82. $fp = @fopen($file,'r');
  83. if (!$fp) {
  84. if (defined('ENIRO_DK_PINCODE')) {
  85. $file = str_replace(ENIRO_DK_PINCODE, 'REMOVED=', $file);
  86. $msg = $this->getClassName().'->parse(), Cannot open Eniro xml file='.$file;
  87. if (defined('HTML_LOG_UTIL_PATH')) {
  88. Log::warn(__FILE__,__LINE__,$msg);
  89. $this->xml .= $this->getMsg(LOG_LEVEL_WARN, $msg);
  90. } else {
  91. $this->xml .= $msg;
  92. }
  93. } else {
  94. $msg = $this->getClassName().'->parse(), Cannot open xml file='.$file;
  95. if (defined('HTML_LOG_UTIL_PATH')) {
  96. Log::warn(__FILE__,__LINE__,$msg);
  97. $this->xml .= $this->getMsg(LOG_LEVEL_WARN, $msg);
  98. } else {
  99. $this->xml .= $msg;
  100. }
  101. }
  102. } else {
  103. // Read and parse data
  104. $data = "";
  105. while (!feof($fp)) {
  106. $data .= fgets($fp, 4096);
  107. }
  108. // error handler
  109. if (!xml_parse($this->xml_parser, $data, feof($fp))) {
  110. die(sprintf("XML error: %s at line %d<br />\r\n$data",
  111. xml_error_string(xml_get_error_code($this->xml_parser)),
  112. xml_get_current_line_number($this->xml_parser)));
  113. }
  114. }
  115.  
  116. $this->destroy();
  117. }
  118.  
  119. /**
  120. * Destructor
  121. * Free the resources
  122. */
  123. function destroy() {
  124. if ($this->xml_parser!=NULL) {
  125. xml_parser_free($this->xml_parser);
  126. $this->xml_parser = NULL;
  127. }
  128. }
  129.  
  130. /**
  131. * Used to catch the events for working with or handling
  132. * the start and end tags within the xml file.
  133. * Element events are issued whenever the xml parser encounters
  134. * start or end tags.
  135. * @param String $parser The xml parser
  136. * @param String $name The name of the element
  137. * @param String $attribs An array of attributes
  138. */
  139. function startElement($parser, $name, $attribs) {
  140. $this->currentTag = $name;
  141. $this->xml .= '&lt;'.$name;
  142. foreach ($attribs as $key=>$value) {
  143. $this->xml .= ' '.$key.'="'.$value.'"';
  144. }
  145. $this->xml .= '&gt;';
  146.  
  147. }
  148.  
  149. /**
  150. * Used to catch the event for handling the data.
  151. * Character data is rougly all the non-markup content
  152. * of xml documents.
  153. * @param String $parser The xml parser
  154. * @param String $name The data
  155. */
  156. function characterData($parser, $data) {
  157. $this->xml .= htmlspecialchars($data);
  158. }
  159.  
  160. /**
  161. * Used to catch the event for handling the default data.
  162. * It will be called for each piece of xml that doesn't
  163. * have a set handler
  164. * @param String $parser The xml parser
  165. * @param String $data The default data
  166. */
  167. function defaultData($parser, $data) {
  168. $this->xml .= htmlspecialchars($data);
  169. }
  170.  
  171. /**
  172. * Used to catch the event for end of element.
  173. * @param String $parser The xml parser
  174. * @param String $name The name of the element
  175. */
  176. function endElement($parser, $name) {
  177. $this->xml .= '&lt;/'.$name."&gt;";
  178. }
  179.  
  180. /**
  181. * Returns the xml file as html
  182. * @return String The xml converted to html
  183. */
  184. function getHtml() {
  185. $html = $this->html;
  186. if ($this->xml!='') {
  187. $html .= "<pre>\r\n";
  188. $html .= $this->xml;
  189. $html .= "</pre>\r\n";
  190. } else {
  191. if (defined('HTML_LOG_UTIL_PATH') && LOG_LEVEL & LOG_LEVEL_DEBUG) {
  192. $html .= '<!-- '.$this->getClassName()."->getHtml() No data found -->\r\n";
  193. } else {
  194. $html .= '<!-- '.$this->getClassName()."->getHtml() No data found -->\r\n";
  195. }
  196. }
  197. return $html;
  198. }
  199.  
  200. /**
  201. * Display html
  202. * <code>
  203. * Usage:
  204. * Sax::display($file);
  205. * </code>
  206. * @static
  207. * @param String $file The file to parse
  208. */
  209. function display($file='') {
  210. $html = new Sax($file);
  211. $html->addHtml();
  212. }
  213.  
  214. }
  215. ?>

Documentation generated on Thu, 22 Dec 2005 17:17:02 +0100 by phpDocumentor 1.3.0RC3