[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/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                  switch ($xmlText{($i + 1)}) {
  65                      case '?':
  66                      case '!':
  67                          break;
  68                      default:
  69                          $this->state = SAXY_STATE_PARSING;
  70                          return (substr($xmlText, $i));
  71                  }
  72              }
  73          }
  74      } //preprocessXML
  75  
  76      /**
  77      * The controlling method for the parsing process
  78      * @param string The xml text to be processed
  79      * @return boolean True if parsing is successful
  80      */
  81  	function parse ($xmlText) {
  82          $xmlText = $this->preprocessXML($xmlText);
  83          $total = strlen($xmlText);
  84  
  85          for ($i = 0; $i < $total; $i++) {
  86              $currentChar = $xmlText{$i};
  87  
  88              switch ($this->state) {
  89                  case SAXY_STATE_PARSING:
  90  
  91                      switch ($currentChar) {
  92                          case '<':
  93                              if (substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) {
  94                                  $this->charContainer .= $currentChar;
  95                              }
  96                              else {
  97                                  $this->parseBetweenTags($this->charContainer);
  98                                  $this->charContainer = '';
  99                              }
 100                              break;
 101  
 102                          case '>':
 103                              if ((substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) &&
 104                                  !(($this->getCharFromEnd($this->charContainer, 0) == ']') &&
 105                                  ($this->getCharFromEnd($this->charContainer, 1) == ']'))) {
 106                                  $this->charContainer .= $currentChar;
 107                              }
 108                              else {
 109                                  $this->parseTag($this->charContainer);
 110                                  $this->charContainer = '';
 111                              }
 112                              break;
 113  
 114                          default:
 115                              $this->charContainer .= $currentChar;
 116                      }
 117  
 118                      break;
 119              }
 120          }
 121  
 122          return true;
 123      } //parse
 124  
 125      /**
 126      * Parses an element tag
 127      * @param string The interior text of the element tag
 128      */
 129  	function parseTag($tagText) {
 130          $tagText = trim($tagText);
 131          $firstChar = $tagText{0};
 132          $myAttributes = array();
 133  
 134          switch ($firstChar) {
 135              case '/':
 136                  $tagName = substr($tagText, 1);
 137                  $this->fireEndElementEvent($tagName);
 138                  break;
 139  
 140              case '!':
 141                  $upperCaseTagText = strtoupper($tagText);
 142  
 143                  if (strpos($upperCaseTagText, SAXY_SEARCH_CDATA) !== false) { //CDATA Section
 144                      $total = strlen($tagText);
 145                      $openBraceCount = 0;
 146                      $textNodeText = '';
 147  
 148                      for ($i = 0; $i < $total; $i++) {
 149                          $currentChar = $tagText{$i};
 150  
 151                          if (($currentChar == ']') && ($tagText{($i + 1)} == ']')) {
 152                              break;
 153                          }
 154                          else if ($openBraceCount > 1) {
 155                              $textNodeText .= $currentChar;
 156                          }
 157                          else if ($currentChar == '[') { //this won't be reached after the first open brace is found
 158                              $openBraceCount ++;
 159                          }
 160                      }
 161  
 162                      if ($this->cDataSectionHandler == null) {
 163                          $this->fireCharacterDataEvent($textNodeText);
 164                      }
 165                      else {
 166                          $this->fireCDataSectionEvent($textNodeText);
 167                      }
 168                  }
 169                  else if (strpos($upperCaseTagText, SAXY_SEARCH_NOTATION) !== false) { //NOTATION node, discard
 170                      return;
 171                  }
 172                  else if (substr($tagText, 0, 2) == '!-') { //comment node, discard
 173                      return;
 174                  }
 175  
 176                  break;
 177  
 178              case '?':
 179                  //Processing Instruction node, discard
 180                  return;
 181  
 182              default:
 183                  if ((strpos($tagText, '"') !== false) || (strpos($tagText, "'") !== false)) {
 184                      $total = strlen($tagText);
 185                      $tagName = '';
 186  
 187                      for ($i = 0; $i < $total; $i++) {
 188                          $currentChar = $tagText{$i};
 189  
 190                          if (($currentChar == ' ') || ($currentChar == "\t") ||
 191                              ($currentChar == "\n") || ($currentChar == "\r") ||
 192                              ($currentChar == "\x0B")) {
 193                              $myAttributes = $this->parseAttributes(substr($tagText, $i));
 194                              break;
 195                          }
 196                          else {
 197                              $tagName .= $currentChar;
 198                          }
 199                      }
 200  
 201                      if (strrpos($tagText, '/') == (strlen($tagText) - 1)) { //check $tagText, but send $tagName
 202                          $this->fireStartElementEvent($tagName, $myAttributes);
 203                          $this->fireEndElementEvent($tagName);
 204                      }
 205                      else {
 206                          $this->fireStartElementEvent($tagName, $myAttributes);
 207                      }
 208                  }
 209                  else {
 210                      if (strpos($tagText, '/') !== false) {
 211                          $tagText = trim(substr($tagText, 0, (strrchr($tagText, '/') - 1)));
 212                          $this->fireStartElementEvent($tagText, $myAttributes);
 213                          $this->fireEndElementEvent($tagText);
 214                      }
 215                      else {
 216                          $this->fireStartElementEvent($tagText, $myAttributes);
 217                      }
 218                  }
 219          }
 220      } //parseTag
 221  
 222      /**
 223      * Returns the current error code (non-functional for SAXY Lite)
 224      * @return int The current error code
 225      */
 226  	function xml_get_error_code() {
 227          return -1;
 228      } //xml_get_error_code
 229  
 230      /**
 231      * Returns a textual description of the error code (non-functional for SAXY Lite)
 232      * @param int The error code
 233      * @return string The error message
 234      */
 235  	function xml_error_string($code) {
 236          return "";
 237      } //xml_error_string
 238  } //SAXY_Lite_Parser
 239  ?>


Généré le : Wed Nov 21 14:43:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics