[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/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  
  46          if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE) {
  47              if (trim($currentChar != '')) {
  48                  $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY;
  49              }
  50          }
  51  
  52          switch ($currentChar) {
  53              case "\t":
  54                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  55                      $valueDump .= $currentChar;
  56                  }
  57                  else {
  58                      $currentChar = '';
  59                  }
  60                  break;
  61  
  62              case "\x0B": //vertical tab
  63              case "\n":
  64              case "\r":
  65                  $currentChar = '';
  66                  break;
  67  
  68              case '=':
  69                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  70                      $valueDump .= $currentChar;
  71                  }
  72                  else {
  73                      $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE;
  74                      $quoteType = '';
  75                      $maybeEntity = false;
  76                  }
  77                  break;
  78  
  79              case '"':
  80                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  81                      if ($quoteType == '') {
  82                          $quoteType = '"';
  83                      }
  84                      else {
  85                          if ($quoteType == $currentChar) {
  86                              if ($convertEntities && $maybeEntity) {
  87                                  $valueDump = strtr($valueDump, DOMIT_PREDEFINED_ENTITIES);
  88                                  $valueDump = strtr($valueDump, $definedEntities);
  89                              }
  90  
  91                              $attrArray[trim($keyDump)] = $valueDump;
  92                              $keyDump = $valueDump = $quoteType = '';
  93                              $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
  94                          }
  95                          else {
  96                              $valueDump .= $currentChar;
  97                          }
  98                      }
  99                  }
 100                  break;
 101  
 102              case "'":
 103                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
 104                      if ($quoteType == '') {
 105                          $quoteType = "'";
 106                      }
 107                      else {
 108                          if ($quoteType == $currentChar) {
 109                              if ($convertEntities && $maybeEntity) {
 110                                  $valueDump = strtr($valueDump, $predefinedEntities);
 111                                  $valueDump = strtr($valueDump, $definedEntities);
 112                              }
 113  
 114                              $attrArray[trim($keyDump)] = $valueDump;
 115                              $keyDump = $valueDump = $quoteType = '';
 116                              $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
 117                          }
 118                          else {
 119                              $valueDump .= $currentChar;
 120                          }
 121                      }
 122                  }
 123                  break;
 124  
 125              case '&':
 126                  //might be an entity
 127                  $maybeEntity = true;
 128                  $valueDump .= $currentChar;
 129                  break;
 130  
 131              default:
 132                  if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY) {
 133                      $keyDump .= $currentChar;
 134                  }
 135                  else {
 136                      $valueDump .= $currentChar;
 137                  }
 138          }
 139      }
 140  
 141      return $attrArray;
 142  } //parseAttributes
 143  ?>


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