b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_ext/phpsvnclient/xml_parser.php - 377 lines - 12314 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /*
   4   * xml_parser.php
   5   *
   6   * @(#) $Header: /home/mlemos/cvsroot/xmlparser/xml_parser.php,v 1.19 2006/11/22 01:25:05 mlemos Exp $
   7   *
   8   */
   9  
  10  /*
  11   * Parser error numbers:
  12   *
  13   * 1 - Could not create the XML parser
  14   * 2 - Could not parse data
  15   * 3 - Could not read from input stream
  16   *
  17   */
  18  if( ! defined( 'EVO_MAIN_INIT' ) ) die( 'Please, do not access this page directly.' );
  19  
  20  $xml_parser_handlers = array();
  21  
  22  Function xml_parser_start_element_handler($parser, $name, $attrs) {
  23      global $xml_parser_handlers;
  24  
  25      if (!strcmp($xml_parser_handlers[$parser]->error, ""))
  26      $xml_parser_handlers[$parser]->StartElement($xml_parser_handlers[$parser], $name, $attrs);
  27  }
  28  
  29  Function xml_parser_end_element_handler($parser, $name) {
  30      global $xml_parser_handlers;
  31  
  32      if (!strcmp($xml_parser_handlers[$parser]->error, ""))
  33      $xml_parser_handlers[$parser]->EndElement($xml_parser_handlers[$parser], $name);
  34  }
  35  
  36  Function xml_parser_character_data_handler($parser, $data) {
  37      global $xml_parser_handlers;
  38  
  39      if (!strcmp($xml_parser_handlers[$parser]->error, ""))
  40      $xml_parser_handlers[$parser]->CharacterData($xml_parser_handlers[$parser], $data);
  41  }
  42  
  43  class xml_parser_handler_class {
  44  
  45      var $xml_parser;
  46      var $error_number = 0;
  47      var $error = "";
  48      var $error_code = 0;
  49      var $error_line, $error_column, $error_byte_index;
  50      var $structure = array();
  51      var $positions = array();
  52      var $path = "";
  53      var $store_positions = 0;
  54      var $simplified_xml = 0;
  55      var $fail_on_non_simplified_xml = 0;
  56  
  57      Function SetError(&$object, $error_number, $error) {
  58      $object->error_number = $error_number;
  59      $object->error = $error;
  60      $object->error_line = xml_get_current_line_number($object->xml_parser);
  61      $object->error_column = xml_get_current_column_number($object->xml_parser);
  62      $object->error_byte_index = xml_get_current_byte_index($object->xml_parser);
  63      }
  64  
  65      Function SetElementData(&$object, $path, &$data) {
  66      $object->structure[$path] = $data;
  67      if ($object->store_positions) {
  68          $object->positions[$path] = array(
  69          "Line" => xml_get_current_line_number($object->xml_parser),
  70          "Column" => xml_get_current_column_number($object->xml_parser),
  71          "Byte" => xml_get_current_byte_index($object->xml_parser)
  72          );
  73      }
  74      }
  75  
  76      Function StartElement(&$object, $name, &$attrs) {
  77      if (strcmp($this->path, "")) {
  78          $element = $object->structure[$this->path]["Elements"];
  79          $object->structure[$this->path]["Elements"]++;
  80          $this->path.=",$element";
  81      } else {
  82          $element = 0;
  83          $this->path = "0";
  84      }
  85      $data = array(
  86          "Tag" => $name,
  87          "Elements" => 0
  88      );
  89      if ($object->simplified_xml) {
  90          if ($object->fail_on_non_simplified_xml
  91              && count($attrs) > 0) {
  92          $this->SetError($object, 2, "Simplified XML can not have attributes in tags");
  93          return;
  94          }
  95      }
  96      else
  97          $data["Attributes"] = $attrs;
  98      $this->SetElementData($object, $this->path, $data);
  99      }
 100  
 101      Function EndElement(&$object, $name) {
 102      $this->path = (($position = strrpos($this->path, ",")) ? substr($this->path, 0, $position) : "");
 103      }
 104  
 105      Function CharacterData(&$object, $data) {
 106      $element = $object->structure[$this->path]["Elements"];
 107      $previous = $this->path . "," . strval($element - 1);
 108      if ($element > 0
 109          && GetType($object->structure[$previous]) == "string")
 110          $object->structure[$previous].=$data;
 111      else {
 112          $this->SetElementData($object, $this->path . ",$element", $data);
 113          $object->structure[$this->path]["Elements"]++;
 114      }
 115      }
 116  
 117  }
 118  
 119  ;
 120  
 121  class xml_parser_class {
 122  
 123      var $xml_parser = 0;
 124      var $parser_handler;
 125      var $error = "";
 126      var $error_number = 0;
 127      var $error_line = 0;
 128      var $error_column = 0;
 129      var $error_byte_index = 0;
 130      var $error_code = 0;
 131      var $stream_buffer_size = 4096;
 132      var $structure = array();
 133      var $positions = array();
 134      var $store_positions = 0;
 135      var $case_folding = 0;
 136      var $target_encoding = "ISO-8859-1";
 137      var $simplified_xml = 0;
 138      var $fail_on_non_simplified_xml = 0;
 139  
 140      Function xml_parser_start_element_handler($parser, $name, $attrs) {
 141      if (!strcmp($this->error, ""))
 142          $this->parser_handler->StartElement($this, $name, $attrs);
 143      }
 144  
 145      Function xml_parser_end_element_handler($parser, $name) {
 146      if (!strcmp($this->error, ""))
 147          $this->parser_handler->EndElement($this, $name);
 148      }
 149  
 150      Function xml_parser_character_data_handler($parser, $data) {
 151      if (!strcmp($this->error, ""))
 152          $this->parser_handler->CharacterData($this, $data);
 153      }
 154  
 155      Function SetErrorPosition($error_number, $error, $line, $column, $byte_index) {
 156      $this->error_number = $error_number;
 157      $this->error = $error;
 158      $this->error_line = $line;
 159      $this->error_column = $column;
 160      $this->error_byte_index = $byte_index;
 161      }
 162  
 163      Function SetError($error_number, $error) {
 164      $this->error_number = $error_number;
 165      $this->error = $error;
 166      if ($this->xml_parser) {
 167          $line = xml_get_current_line_number($this->xml_parser);
 168          $column = xml_get_current_column_number($this->xml_parser);
 169          $byte_index = xml_get_current_byte_index($this->xml_parser);
 170      } else {
 171          $line = $column = 1;
 172          $byte_index = 0;
 173      }
 174      $this->SetErrorPosition($error_number, $error, $line, $column, $byte_index);
 175      }
 176  
 177      Function Parse($data, $end_of_data) {
 178      global $xml_parser_handlers;
 179  
 180      if (strcmp($this->error, ""))
 181          return($this->error);
 182      if (!$this->xml_parser) {
 183          if (!function_exists("xml_parser_create")) {
 184          $this->SetError(1, "XML support is not available in this PHP configuration");
 185          return($this->error);
 186          }
 187          if (!($this->xml_parser = xml_parser_create())) {
 188          $this->SetError(1, "Could not create the XML parser");
 189          return($this->error);
 190          }
 191          xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, $this->case_folding);
 192          xml_parser_set_option($this->xml_parser, XML_OPTION_TARGET_ENCODING, $this->target_encoding);
 193          if (function_exists("xml_set_object")) {
 194          xml_set_object($this->xml_parser, $this);
 195          $this->parser_handler = new xml_parser_handler_class;
 196          $this->structure = array();
 197          $this->positions = array();
 198          } else {
 199          $xml_parser_handlers[$this->xml_parser] = new xml_parser_handler_class;
 200          $xml_parser_handlers[$this->xml_parser]->xml_parser = $this->xml_parser;
 201          $xml_parser_handlers[$this->xml_parser]->store_positions = $this->store_positions;
 202          $xml_parser_handlers[$this->xml_parser]->simplified_xml = $this->simplified_xml;
 203          $xml_parser_handlers[$this->xml_parser]->fail_on_non_simplified_xml = $this->fail_on_non_simplified_xml;
 204          }
 205          xml_set_element_handler($this->xml_parser, "xml_parser_start_element_handler", "xml_parser_end_element_handler");
 206          xml_set_character_data_handler($this->xml_parser, "xml_parser_character_data_handler");
 207      }
 208      $parser_ok = xml_parse($this->xml_parser, $data, $end_of_data);
 209      if (!function_exists("xml_set_object"))
 210          $this->error = $xml_parser_handlers[$this->xml_parser]->error;
 211      if (!strcmp($this->error, "")) {
 212          if ($parser_ok) {
 213          if ($end_of_data) {
 214              if (function_exists("xml_set_object"))
 215              Unset($this->parser_handler);
 216              else {
 217              $this->structure = $xml_parser_handlers[$this->xml_parser]->structure;
 218              $this->positions = $xml_parser_handlers[$this->xml_parser]->positions;
 219              Unset($xml_parser_handlers[$this->xml_parser]);
 220              }
 221              xml_parser_free($this->xml_parser);
 222              $this->xml_parser = 0;
 223          }
 224          }
 225          else
 226          $this->SetError(2, "Could not parse data: " . xml_error_string($this->error_code = xml_get_error_code($this->xml_parser)));
 227      }
 228      else {
 229          if (!function_exists("xml_set_object")) {
 230          $this->error_number = $xml_parser_handlers[$this->xml_parser]->error_number;
 231          $this->error_code = $xml_parser_handlers[$this->xml_parser]->error_code;
 232          $this->error_line = $xml_parser_handlers[$this->xml_parser]->error_line;
 233          $this->error_column = $xml_parser_handlers[$this->xml_parser]->error_column;
 234          $this->error_byte_index = $xml_parser_handlers[$this->xml_parser]->error_byte_index;
 235          }
 236      }
 237      return($this->error);
 238      }
 239  
 240      Function VerifyWhiteSpace($path) {
 241      if ($this->store_positions) {
 242          $line = $parser->positions[$path]["Line"];
 243          $column = $parser->positions[$path]["Column"];
 244          $byte_index = $parser->positions[$path]["Byte"];
 245      } else {
 246          $line = $column = 1;
 247          $byte_index = 0;
 248      }
 249      if (!IsSet($this->structure[$path])) {
 250          $this->SetErrorPosition(2, "element path does not exist", $line, $column, $byte_index);
 251          return($this->error);
 252      }
 253      if (GetType($this->structure[$path]) != "string") {
 254          $this->SetErrorPosition(2, "element is not data", $line, $column, $byte_index);
 255          return($this->error);
 256      }
 257      $data = $this->structure[$path];
 258      for ($previous_return = 0, $position = 0; $position < strlen($data); $position++) {
 259          switch ($data[$position]) {
 260          case " ":
 261          case "\t":
 262              $column++;
 263              $byte_index++;
 264              $previous_return = 0;
 265              break;
 266          case "\n":
 267              if (!$previous_return)
 268              $line++;
 269              $column = 1;
 270              $byte_index++;
 271              $previous_return = 0;
 272              break;
 273          case "\r":
 274              $line++;
 275              $column = 1;
 276              $byte_index++;
 277              $previous_return = 1;
 278              break;
 279          default:
 280              $this->SetErrorPosition(2, "data is not white space", $line, $column, $byte_index);
 281              return($this->error);
 282          }
 283      }
 284      return("");
 285      }
 286  
 287      Function ParseStream($stream) {
 288      if (strcmp($this->error, ""))
 289          return($this->error);
 290      do {
 291          if (!($data = @fread($stream, $this->stream_buffer_size))) {
 292          if (!feof($stream)) {
 293              $this->SetError(3, "Could not read from input stream" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : ''));
 294              break;
 295          }
 296          }
 297          if (strcmp($error = $this->Parse($data, feof($stream)), ""))
 298          break;
 299      }
 300      while (!feof($stream));
 301      return($this->error);
 302      }
 303  
 304      Function ParseFile($file) {
 305      if (!file_exists($file))
 306          return("the XML file to parse ($file) does not exist");
 307      if (!($definition = @fopen($file, "r")))
 308          return("could not open the XML file ($file)" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : ''));
 309      $error = $this->ParseStream($definition);
 310      fclose($definition);
 311      return($error);
 312      }
 313  
 314  }
 315  
 316  ;
 317  
 318  Function XMLParseFile(&$parser, $file, $store_positions, $cache="", $case_folding=0, $target_encoding="ISO-8859-1", $simplified_xml=0, $fail_on_non_simplified_xml=0) {
 319      if (!file_exists($file))
 320      return("the XML file to parse ($file) does not exist");
 321      if (strcmp($cache, "")) {
 322      if (file_exists($cache)
 323          && filemtime($file) <= filemtime($cache)) {
 324          if (($cache_file = @fopen($cache, "r"))) {
 325          if (function_exists("set_file_buffer"))
 326              set_file_buffer($cache_file, 0);
 327          if (!($cache_contents = @fread($cache_file, filesize($cache))))
 328              $error = "could not read from the XML cache file $cache" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : '');
 329          else
 330              $error="";
 331          fclose($cache_file);
 332          if (!strcmp($error, "")) {
 333              if (GetType($parser = unserialize($cache_contents)) == "object"
 334                  && IsSet($parser->structure)) {
 335              if (!IsSet($parser->simplified_xml))
 336                  $parser->simplified_xml = 0;
 337              if (($simplified_xml
 338                  || !$parser->simplified_xml)
 339                  && (!$store_positions
 340                  || $parser->store_positions)) {
 341                  return("");
 342              }
 343              }
 344              else
 345              $error="it was not specified a valid cache object in XML file ($cache)";
 346          }
 347          }
 348          else
 349          $error="could not open cache XML file ($cache)" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : '');
 350          if (strcmp($error, ""))
 351          return($error);
 352      }
 353      }
 354      $parser = new xml_parser_class;
 355      $parser->store_positions = $store_positions;
 356      $parser->case_folding = $case_folding;
 357      $parser->target_encoding = $target_encoding;
 358      $parser->simplified_xml = $simplified_xml;
 359      $parser->fail_on_non_simplified_xml = $fail_on_non_simplified_xml;
 360      if (!strcmp($error = $parser->ParseFile($file), "")
 361          && strcmp($cache, "")) {
 362      if (($cache_file = @fopen($cache, "w"))) {
 363          if (function_exists("set_file_buffer"))
 364          set_file_buffer($cache_file, 0);
 365          if (!@fwrite($cache_file, serialize($parser))
 366              || !@fclose($cache_file))
 367          $error = "could to write to the XML cache file ($cache)" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : '');
 368          if (strcmp($error, ""))
 369          unlink($cache);
 370      }
 371      else
 372          $error="could not open for writing to the cache file ($cache)" . (IsSet($php_errormsg) ? ': ' . $php_errormsg : '');
 373      }
 374      return($error);
 375  }
 376  
 377  ?>

title

Description

title

Description

title

Description

title

title

Body