[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3  * parseAttributes is a function for parsing attribute and attribute-like strings
   4  * @package domit-xmlparser
   5  * @copyright (C) 2004 John Heinstein. All rights reserved
   6  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   7  * @author John Heinstein <johnkarl@nbnet.nb.ca>
   8  * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
   9  * DOMIT! is Free Software
  10  **/
  11  
  12  /** attribute parse state, just before parsing an attribute */
  13  define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE', 0);
  14  /** attribute parse state, parsing an attribute key */
  15  define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY', 1);
  16  /** attribute parse state, parsing an attribute value */
  17  define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE', 2);
  18  
  19  /**
  20  *@global Array Translation table for predefined XML entities
  21  */
  22  $GLOBALS['DOMIT_PREDEFINED_ENTITIES'] = array('&' => '&amp;', '<' => '&lt;', '>' => '&gt;',
  23                                              '"' => '&quot;', "'" => '&apos;');
  24  
  25  /**
  26  * Parses the attributes string into an array of key / value pairs
  27  * @param string The attribute text
  28  * @return Array An array of key / value pairs
  29  */
  30  function parseAttributes($attrText, $convertEntities = true, $definedEntities = null) {
  31      $attrText = trim($attrText);
  32      $attrArray = array();
  33      $maybeEntity = false;
  34  
  35      $total = strlen($attrText);
  36      $keyDump = '';
  37      $valueDump = '';
  38      $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
  39      $quoteType = '';
  40  
  41      if ($definedEntities == null) $defineEntities = array();
  42  
  43      for ($i = 0; $i < $total; $i++) {
  44  //        $currentChar = $attrText{$i};
  45          $currentChar = substr($attrText, $i, 1);
  46  
  47          if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE) {
  48              if (trim($currentChar != '')) {
  49                  $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY;
  50              }
  51          }
  52  
  53          switch ($currentChar) {
  54              case "\t":
  55                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  56                      $valueDump .= $currentChar;
  57                  }
  58                  else {
  59                      $currentChar = '';
  60                  }
  61                  break;
  62  
  63              case "\x0B": //vertical tab
  64              case "\n":
  65              case "\r":
  66                  $currentChar = '';
  67                  break;
  68  
  69              case '=':
  70                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  71                      $valueDump .= $currentChar;
  72                  }
  73                  else {
  74                      $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE;
  75                      $quoteType = '';
  76                      $maybeEntity = false;
  77                  }
  78                  break;
  79  
  80              case '"':
  81                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  82                      if ($quoteType == '') {
  83                          $quoteType = '"';
  84                      }
  85                      else {
  86                          if ($quoteType == $currentChar) {
  87                              if ($convertEntities && $maybeEntity) {
  88                                  $valueDump = strtr($valueDump, DOMIT_PREDEFINED_ENTITIES);
  89                                  $valueDump = strtr($valueDump, $definedEntities);
  90                              }
  91  
  92                              $attrArray[trim($keyDump)] = $valueDump;
  93                              $keyDump = $valueDump = $quoteType = '';
  94                              $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
  95                          }
  96                          else {
  97                              $valueDump .= $currentChar;
  98                          }
  99                      }
 100                  }
 101                  break;
 102  
 103              case "'":
 104                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
 105                      if ($quoteType == '') {
 106                          $quoteType = "'";
 107                      }
 108                      else {
 109                          if ($quoteType == $currentChar) {
 110                              if ($convertEntities && $maybeEntity) {
 111                                  $valueDump = strtr($valueDump, $predefinedEntities);
 112                                  $valueDump = strtr($valueDump, $definedEntities);
 113                              }
 114  
 115                              $attrArray[trim($keyDump)] = $valueDump;
 116                              $keyDump = $valueDump = $quoteType = '';
 117                              $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
 118                          }
 119                          else {
 120                              $valueDump .= $currentChar;
 121                          }
 122                      }
 123                  }
 124                  break;
 125  
 126              case '&':
 127                  //might be an entity
 128                  $maybeEntity = true;
 129                  $valueDump .= $currentChar;
 130                  break;
 131  
 132              default:
 133                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY) {
 134                      $keyDump .= $currentChar;
 135                  }
 136                  else {
 137                      $valueDump .= $currentChar;
 138                  }
 139          }
 140      }
 141  
 142      return $attrArray;
 143  } //parseAttributes
 144  ?>


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