[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/domit/ -> xml_saxy_lite_parser.php (source)

   1  <?php
   2  /**
   3  * SAXY Lite is a non-validating, but lightweight and fast SAX parser for PHP, modelled on the Expat parser
   4  * @package saxy-xmlparser
   5  * @subpackage saxy-xmlparser-lite
   6  * @version 1.0
   7  * @copyright (C) 2004 John Heinstein. All rights reserved
   8  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   9  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  10  * @link http://www.engageinteractive.com/saxy/ SAXY Home Page
  11  * SAXY is Free Software
  12  **/
  13  
  14  if (!defined('SAXY_INCLUDE_PATH')) {
  15      define('SAXY_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  16  }
  17  
  18  /** current version of SAXY Lite */
  19  define ('SAXY_LITE_VERSION', '1.0');
  20  
  21  /** initial saxy lite parse state, before anything is encountered */
  22  define('SAXY_STATE_NONE', 0);
  23  /** saxy lite parse state, processing main document */
  24  define('SAXY_STATE_PARSING', 1);
  25  
  26  require_once (SAXY_INCLUDE_PATH . 'xml_saxy_shared.php');
  27  
  28  /**
  29  * The SAX Parser class
  30  *
  31  * @package saxy-xmlparser
  32  * @subpackage saxy-xmlparser-lite
  33  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  34  */
  35  class SAXY_Lite_Parser extends SAXY_Parser_Base {
  36      /**
  37      * Constructor for SAX parser
  38      */
  39  	function SAXY_Lite_Parser() {
  40          $this->SAXY_Parser_Base();
  41          $this->state = SAXY_STATE_NONE;
  42      } //SAXY_Lite_Parser
  43  
  44      /**
  45      * Returns the current version of SAXY Lite
  46      * @return Object The current version of SAXY Lite
  47      */
  48  	function getVersion() {
  49          return SAXY_LITE_VERSION;
  50      } //getVersion
  51  
  52      /**
  53      * Processes the xml prolog, doctype, and any other nodes that exist outside of the main xml document
  54      * @param string The xml text to be processed
  55      * @return string The preprocessed xml text
  56      */
  57  	function preprocessXML($xmlText) {
  58          //strip prolog
  59          $xmlText = trim($xmlText);
  60          $total = strlen($xmlText);
  61  
  62          for ($i = 0; $i < $total; $i++) {
  63  //            if ($xmlText{$i} == '<') {
  64              if (substr($xmlText, $i, 1) == '<') {
  65                  switch ($xmlText{($i + 1)}) {
  66                      case '?':
  67                      case '!':
  68                          break;
  69                      default:
  70                          $this->state = SAXY_STATE_PARSING;
  71                          return (substr($xmlText, $i));
  72                  }
  73              }
  74          }
  75      } //preprocessXML
  76  
  77      /**
  78      * The controlling method for the parsing process
  79      * @param string The xml text to be processed
  80      * @return boolean True if parsing is successful
  81      */
  82  	function parse ($xmlText) {
  83          $xmlText = $this->preprocessXML($xmlText);
  84          $total = strlen($xmlText);
  85  
  86          for ($i = 0; $i < $total; $i++) {
  87  //            $currentChar = $xmlText{$i};
  88              $currentChar = substr($xmlText, $i, 1);
  89  
  90              switch ($this->state) {
  91                  case SAXY_STATE_PARSING:
  92  
  93                      switch ($currentChar) {
  94                          case '<':
  95                              if (substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) {
  96                                  $this->charContainer .= $currentChar;
  97                              }
  98                              else {
  99                                  $this->parseBetweenTags($this->charContainer);
 100                                  $this->charContainer = '';
 101                              }
 102                              break;
 103  
 104                          case '>':
 105                              if ((substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) &&
 106                                  !(($this->getCharFromEnd($this->charContainer, 0) == ']') &&
 107                                  ($this->getCharFromEnd($this->charContainer, 1) == ']'))) {
 108                                  $this->charContainer .= $currentChar;
 109                              }
 110                              else {
 111                                  $this->parseTag($this->charContainer);
 112                                  $this->charContainer = '';
 113                              }
 114                              break;
 115  
 116                          default:
 117                              $this->charContainer .= $currentChar;
 118                      }
 119  
 120                      break;
 121              }
 122          }
 123  
 124          return true;
 125      } //parse
 126  
 127      /**
 128      * Parses an element tag
 129      * @param string The interior text of the element tag
 130      */
 131  	function parseTag($tagText) {
 132          $tagText = trim($tagText);
 133          $firstChar = $tagText{0};
 134          $myAttributes = array();
 135  
 136          switch ($firstChar) {
 137              case '/':
 138                  $tagName = substr($tagText, 1);
 139                  $this->fireEndElementEvent($tagName);
 140                  break;
 141  
 142              case '!':
 143                  $upperCaseTagText = strtoupper($tagText);
 144  
 145                  if (strpos($upperCaseTagText, SAXY_SEARCH_CDATA) !== false) { //CDATA Section
 146                      $total = strlen($tagText);
 147                      $openBraceCount = 0;
 148                      $textNodeText = '';
 149  
 150                      for ($i = 0; $i < $total; $i++) {
 151  //                        $currentChar = $tagText{$i};
 152                          $currentChar = substr($tagText, $i, 1);
 153  
 154                          if (($currentChar == ']') && ($tagText{($i + 1)} == ']')) {
 155                              break;
 156                          }
 157                          else if ($openBraceCount > 1) {
 158                              $textNodeText .= $currentChar;
 159                          }
 160                          else if ($currentChar == '[') { //this won't be reached after the first open brace is found
 161                              $openBraceCount ++;
 162                          }
 163                      }
 164  
 165                      if ($this->cDataSectionHandler == null) {
 166                          $this->fireCharacterDataEvent($textNodeText);
 167                      }
 168                      else {
 169                          $this->fireCDataSectionEvent($textNodeText);
 170                      }
 171                  }
 172                  else if (strpos($upperCaseTagText, SAXY_SEARCH_NOTATION) !== false) { //NOTATION node, discard
 173                      return;
 174                  }
 175                  else if (substr($tagText, 0, 2) == '!-') { //comment node, discard
 176                      return;
 177                  }
 178  
 179                  break;
 180  
 181              case '?':
 182                  //Processing Instruction node, discard
 183                  return;
 184  
 185              default:
 186                  if ((strpos($tagText, '"') !== false) || (strpos($tagText, "'") !== false)) {
 187                      $total = strlen($tagText);
 188                      $tagName = '';
 189  
 190                      for ($i = 0; $i < $total; $i++) {
 191  //                        $currentChar = $tagText{$i};
 192                          $currentChar = substr($tagText, $i, 1);
 193  
 194                          if (($currentChar == ' ') || ($currentChar == "\t") ||
 195                              ($currentChar == "\n") || ($currentChar == "\r") ||
 196                              ($currentChar == "\x0B")) {
 197                              $myAttributes = $this->parseAttributes(substr($tagText, $i));
 198                              break;
 199                          }
 200                          else {
 201                              $tagName .= $currentChar;
 202                          }
 203                      }
 204  
 205                      if (strrpos($tagText, '/') == (strlen($tagText) - 1)) { //check $tagText, but send $tagName
 206                          $this->fireStartElementEvent($tagName, $myAttributes);
 207                          $this->fireEndElementEvent($tagName);
 208                      }
 209                      else {
 210                          $this->fireStartElementEvent($tagName, $myAttributes);
 211                      }
 212                  }
 213                  else {
 214                      if (strpos($tagText, '/') !== false) {
 215                          $tagText = trim(substr($tagText, 0, (strrchr($tagText, '/') - 1)));
 216                          $this->fireStartElementEvent($tagText, $myAttributes);
 217                          $this->fireEndElementEvent($tagText);
 218                      }
 219                      else {
 220                          $this->fireStartElementEvent($tagText, $myAttributes);
 221                      }
 222                  }
 223          }
 224      } //parseTag
 225  
 226      /**
 227      * Returns the current error code (non-functional for SAXY Lite)
 228      * @return int The current error code
 229      */
 230  	function xml_get_error_code() {
 231          return -1;
 232      } //xml_get_error_code
 233  
 234      /**
 235      * Returns a textual description of the error code (non-functional for SAXY Lite)
 236      * @param int The error code
 237      * @return string The error message
 238      */
 239  	function xml_error_string($code) {
 240          return "";
 241      } //xml_error_string
 242  } //SAXY_Lite_Parser
 243  ?>


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1