[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3  * SAXY_Parser_Base is a base class for SAXY and SAXY Lite
   4  * @package saxy-xmlparser
   5  * @version 1.0
   6  * @copyright (C) 2004 John Heinstein. All rights reserved
   7  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   8  * @author John Heinstein <johnkarl@nbnet.nb.ca>
   9  * @link http://www.engageinteractive.com/saxy/ SAXY Home Page
  10  * SAXY is Free Software
  11  **/
  12  
  13  /** the initial characters of a cdata section */
  14  define('SAXY_SEARCH_CDATA', '![CDATA[');
  15  /** the length of the initial characters of a cdata section */
  16  define('SAXY_CDATA_LEN', 8);
  17  /** the initial characters of a notation */
  18  define('SAXY_SEARCH_NOTATION', '!NOTATION');
  19  /** the initial characters of a doctype */
  20  define('SAXY_SEARCH_DOCTYPE', '!DOCTYPE');
  21  /** saxy parse state, just before parsing an attribute */
  22  define('SAXY_STATE_ATTR_NONE', 0);
  23  /** saxy parse state, parsing an attribute key */
  24  define('SAXY_STATE_ATTR_KEY', 1);
  25  /** saxy parse state, parsing an attribute value */
  26  define('SAXY_STATE_ATTR_VALUE', 2);
  27  
  28  /**
  29  * The base SAX Parser class
  30  *
  31  * @package saxy-xmlparser
  32  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  33  */
  34  class SAXY_Parser_Base {
  35      /** @var int The current state of the parser */
  36      var $state;
  37      /** @var int A temporary container for parsed characters */
  38      var $charContainer;
  39      /** @var Object A reference to the start event handler */
  40      var $startElementHandler;
  41      /** @var Object A reference to the end event handler */
  42      var $endElementHandler;
  43      /** @var Object A reference to the data event handler */
  44      var $characterDataHandler;
  45      /** @var Object A reference to the CDATA Section event handler */
  46      var $cDataSectionHandler = null;
  47      /** @var boolean True if predefined entities are to be converted into characters */
  48      var $convertEntities = true;
  49      /** @var Array Translation table for predefined entities */
  50      var $predefinedEntities = array('&amp;' => '&', '&lt;' => '<', '&gt;' => '>',
  51                              '&quot;' => '"', '&apos;' => "'");
  52      /** @var Array User defined translation table for entities */
  53      var $definedEntities = array();
  54      /** @var boolean True if whitespace is to be preserved during parsing. NOT YET IMPLEMENTED! */
  55      var $preserveWhitespace = false;
  56  
  57  
  58      /**
  59      * Constructor for SAX parser
  60      */
  61  	function SAXY_Parser_Base() {
  62          $this->charContainer = '';
  63      } //SAXY_Parser_Base
  64  
  65      /**
  66      * Sets a reference to the handler for the start element event
  67      * @param mixed A reference to the start element handler
  68      */
  69  	function xml_set_element_handler($startHandler, $endHandler) {
  70          $this->startElementHandler = $startHandler;
  71          $this->endElementHandler = $endHandler;
  72      } //xml_set_element_handler
  73  
  74      /**
  75      * Sets a reference to the handler for the data event
  76      * @param mixed A reference to the data handler
  77      */
  78  	function xml_set_character_data_handler($handler) {
  79          $this->characterDataHandler =& $handler;
  80      } //xml_set_character_data_handler
  81  
  82      /**
  83      * Sets a reference to the handler for the CDATA Section event
  84      * @param mixed A reference to the CDATA Section handler
  85      */
  86  	function xml_set_cdata_section_handler($handler) {
  87          $this->cDataSectionHandler =& $handler;
  88      } //xml_set_cdata_section_handler
  89  
  90      /**
  91      * Sets whether predefined entites should be replaced with their equivalent characters during parsing
  92      * @param boolean True if entity replacement is to occur
  93      */
  94  	function convertEntities($truthVal) {
  95          $this->convertEntities = $truthVal;
  96      } //convertEntities
  97  
  98      /**
  99      * Appends an array of entity mappings to the existing translation table
 100      *
 101      * Intended mainly to facilitate the conversion of non-ASCII entities into equivalent characters
 102      *
 103      * @param array A list of entity mappings in the format: array('&amp;' => '&');
 104      */
 105  	function appendEntityTranslationTable($table) {
 106          $this->definedEntities = $table;
 107      } //appendEntityTranslationTable
 108  
 109  
 110      /**
 111      * Gets the nth character from the end of the string
 112      * @param string The text to be queried
 113      * @param int The index from the end of the string
 114      * @return string The found character
 115      */
 116  	function getCharFromEnd($text, $index) {
 117          $len = strlen($text);
 118          $char = $text{($len - 1 - $index)};
 119  
 120          return $char;
 121      } //getCharFromEnd
 122  
 123      /**
 124      * Parses the attributes string into an array of key / value pairs
 125      * @param string The attribute text
 126      * @return Array An array of key / value pairs
 127      */
 128  	function parseAttributes($attrText) {
 129          $attrText = trim($attrText);
 130          $attrArray = array();
 131          $maybeEntity = false;
 132  
 133          $total = strlen($attrText);
 134          $keyDump = '';
 135          $valueDump = '';
 136          $currentState = SAXY_STATE_ATTR_NONE;
 137          $quoteType = '';
 138  
 139          for ($i = 0; $i < $total; $i++) {
 140  //            $currentChar = $attrText{$i};
 141              $currentChar = substr($attrText, $i, 1);
 142  
 143              if ($currentState == SAXY_STATE_ATTR_NONE) {
 144                  if (trim($currentChar != '')) {
 145                      $currentState = SAXY_STATE_ATTR_KEY;
 146                  }
 147              }
 148  
 149              switch ($currentChar) {
 150                  case "\t":
 151                      if ($currentState == SAXY_STATE_ATTR_VALUE) {
 152                          $valueDump .= $currentChar;
 153                      }
 154                      else {
 155                          $currentChar = '';
 156                      }
 157                      break;
 158  
 159                  case "\x0B": //vertical tab
 160                  case "\n":
 161                  case "\r":
 162                      $currentChar = '';
 163                      break;
 164  
 165                  case '=':
 166                      if ($currentState == SAXY_STATE_ATTR_VALUE) {
 167                          $valueDump .= $currentChar;
 168                      }
 169                      else {
 170                          $currentState = SAXY_STATE_ATTR_VALUE;
 171                          $quoteType = '';
 172                          $maybeEntity = false;
 173                      }
 174                      break;
 175  
 176                  case '"':
 177                      if ($currentState == SAXY_STATE_ATTR_VALUE) {
 178                          if ($quoteType == '') {
 179                              $quoteType = '"';
 180                          }
 181                          else {
 182                              if ($quoteType == $currentChar) {
 183                                  // Joomla! hack
 184                                  if (isset( $this ) && $this->convertEntities && $maybeEntity) {
 185                                      $valueDump = strtr($valueDump, $this->predefinedEntities);
 186                                      $valueDump = strtr($valueDump, $this->definedEntities);
 187                                  }
 188  
 189                                  $keyDump = trim($keyDump);
 190                                  $attrArray[$keyDump] = $valueDump;
 191                                  $keyDump = $valueDump = $quoteType = '';
 192                                  $currentState = SAXY_STATE_ATTR_NONE;
 193                              }
 194                              else {
 195                                  $valueDump .= $currentChar;
 196                              }
 197                          }
 198                      }
 199                      break;
 200  
 201                  case "'":
 202                      if ($currentState == SAXY_STATE_ATTR_VALUE) {
 203                          if ($quoteType == '') {
 204                              $quoteType = "'";
 205                          }
 206                          else {
 207                              if ($quoteType == $currentChar) {
 208                                  // Joomla! hack
 209                                  if (isset( $this ) && $this->convertEntities && $maybeEntity) {
 210                                      $valueDump = strtr($valueDump, $this->predefinedEntities);
 211                                      $valueDump = strtr($valueDump, $this->definedEntities);
 212                                  }
 213  
 214                                  $keyDump = trim($keyDump);
 215                                  $attrArray[$keyDump] = $valueDump;
 216                                  $keyDump = $valueDump = $quoteType = '';
 217                                  $currentState = SAXY_STATE_ATTR_NONE;
 218                              }
 219                              else {
 220                                  $valueDump .= $currentChar;
 221                              }
 222                          }
 223                      }
 224                      break;
 225  
 226                  case '&':
 227                      //might be an entity
 228                      $maybeEntity = true;
 229                      $valueDump .= $currentChar;
 230                      break;
 231  
 232                  default:
 233                      if ($currentState == SAXY_STATE_ATTR_KEY) {
 234                          $keyDump .= $currentChar;
 235                      }
 236                      else {
 237                          $valueDump .= $currentChar;
 238                      }
 239              }
 240          }
 241  
 242          return $attrArray;
 243      } //parseAttributes
 244  
 245      /**
 246      * Parses character data
 247      * @param string The character data
 248      */
 249  	function parseBetweenTags($betweenTagText) {
 250          if (trim($betweenTagText) != ''){
 251              $this->fireCharacterDataEvent($betweenTagText);
 252          }
 253      } //parseBetweenTags
 254  
 255      /**
 256      * Fires a start element event
 257      * @param string The start element tag name
 258      * @param Array The start element attributes
 259      */
 260  	function fireStartElementEvent($tagName, $attributes) {
 261          call_user_func($this->startElementHandler, $this, $tagName, $attributes);
 262      } //fireStartElementEvent
 263  
 264      /**
 265      * Fires an end element event
 266      * @param string The end element tag name
 267      */
 268  	function fireEndElementEvent($tagName) {
 269          call_user_func($this->endElementHandler, $this, $tagName);
 270      } //fireEndElementEvent
 271  
 272      /**
 273      * Fires a character data event
 274      * @param string The character data
 275      */
 276  	function fireCharacterDataEvent($data) {
 277          if ($this->convertEntities && ((strpos($data, "&") != -1))) {
 278              $data = strtr($data, $this->predefinedEntities);
 279              $data = strtr($data, $this->definedEntities);
 280          }
 281  
 282          call_user_func($this->characterDataHandler, $this, $data);
 283      } //fireCharacterDataEvent
 284  
 285      /**
 286      * Fires a CDATA Section event
 287      * @param string The CDATA Section data
 288      */
 289  	function fireCDataSectionEvent($data) {
 290          call_user_func($this->cDataSectionHandler, $this, $data);
 291      } //fireCDataSectionEvent
 292  } //SAXY_Parser_Base
 293  
 294  ?>


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