[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/domit/ -> xml_domit_nodetools.php (source)

   1  <?php
   2  /**
   3  * nodetools is a class of miscellaneous XML helper methods
   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  * A class of miscellaneous XML helper methods
  27  *
  28  * @package domit-xmlparser
  29  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  30  */
  31  class nodetools {
  32      /**
  33      * Parses the attributes string into an array of key / value pairs
  34      * @param string The attribute text
  35      * @return Array An array of key / value pairs
  36      */
  37  	function parseAttributes($attrText, $convertEntities = true, $definedEntities = null) {
  38          $attrText = trim($attrText);
  39          $attrArray = array();
  40          $maybeEntity = false;
  41  
  42          $total = strlen($attrText);
  43          $keyDump = '';
  44          $valueDump = '';
  45          $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
  46          $quoteType = '';
  47  
  48          if ($definedEntities == null) $defineEntities = array();
  49  
  50          for ($i = 0; $i < $total; $i++) {
  51              $currentChar = $attrText{$i};
  52  
  53              if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE) {
  54                  if (trim($currentChar != '')) {
  55                      $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY;
  56                  }
  57              }
  58  
  59              switch ($currentChar) {
  60                  case "\t":
  61                      if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  62                          $valueDump .= $currentChar;
  63                      }
  64                      else {
  65                          $currentChar = '';
  66                      }
  67                      break;
  68  
  69                  case "\x0B": //vertical tab
  70                  case "\n":
  71                  case "\r":
  72                      $currentChar = '';
  73                      break;
  74  
  75                  case '=':
  76                      if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  77                          $valueDump .= $currentChar;
  78                      }
  79                      else {
  80                          $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE;
  81                          $quoteType = '';
  82                          $maybeEntity = false;
  83                      }
  84                      break;
  85  
  86                  case '"':
  87                      if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
  88                          if ($quoteType == '') {
  89                              $quoteType = '"';
  90                          }
  91                          else {
  92                              if ($quoteType == $currentChar) {
  93                                  if ($convertEntities && $maybeEntity) {
  94                                      $valueDump = strtr($valueDump, DOMIT_PREDEFINED_ENTITIES);
  95                                      $valueDump = strtr($valueDump, $definedEntities);
  96                                  }
  97  
  98                                  $attrArray[trim($keyDump)] = $valueDump;
  99                                  $keyDump = $valueDump = $quoteType = '';
 100                                  $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
 101                              }
 102                              else {
 103                                  $valueDump .= $currentChar;
 104                              }
 105                          }
 106                      }
 107                      break;
 108  
 109                  case "'":
 110                      if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
 111                          if ($quoteType == '') {
 112                              $quoteType = "'";
 113                          }
 114                          else {
 115                              if ($quoteType == $currentChar) {
 116                                  if ($convertEntities && $maybeEntity) {
 117                                      $valueDump = strtr($valueDump, $predefinedEntities);
 118                                      $valueDump = strtr($valueDump, $definedEntities);
 119                                  }
 120  
 121                                  $attrArray[trim($keyDump)] = $valueDump;
 122                                  $keyDump = $valueDump = $quoteType = '';
 123                                  $currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
 124                              }
 125                              else {
 126                                  $valueDump .= $currentChar;
 127                              }
 128                          }
 129                      }
 130                      break;
 131  
 132                  case '&':
 133                      //might be an entity
 134                      $maybeEntity = true;
 135                      $valueDump .= $currentChar;
 136                      break;
 137  
 138                  default:
 139                      if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY) {
 140                          $keyDump .= $currentChar;
 141                      }
 142                      else {
 143                          $valueDump .= $currentChar;
 144                      }
 145              }
 146          }
 147  
 148          return $attrArray;
 149      } //parseAttributes
 150  
 151      /**
 152      * Move a node to the previous index in the childNodes array
 153      * @param Object The node to be moved
 154      */
 155  	function moveUp(&$node) {
 156          if (($node->previousSibling != null) && ($node->parentNode != null)) {
 157              $parent =& $node->parentNode;
 158              $previous =& $node->previousSibling;
 159  
 160              $node =& $parent->removeChild($node);
 161              $parent->insertBefore($node, $previous);
 162          }
 163      } //moveUp
 164  
 165      /**
 166      * Move a node to the next index in the childNodes array
 167      * @param Object The node to be moved
 168      */
 169  	function moveDown(&$node) {
 170          if (($node->nextSibling != null) && ($node->parentNode != null)) {
 171              $parent =& $node->parentNode;
 172  
 173              if ($node->nextSibling->nextSibling == null) {
 174                  $node =& $parent->removeChild($node);
 175                  $parent->appendChild($node);
 176              }
 177              else {
 178                  $insertionPoint =& $node->nextSibling->nextSibling;
 179                  $node =& $parent->removeChild($node);
 180                  $parent->insertBefore($node, $insertionPoint);
 181              }
 182          }
 183      } //moveDown
 184  
 185      /**
 186      * Checks if a node exists on the given path; if so, returns the node, otherwise false
 187      * @param Object The calling node
 188      * @param string The path
 189      * @return mixed The found node, or false
 190      */
 191      function &nodeExists(&$callingNode, $path) {
 192          $foundNode =& $callingNode->getElementsByPath($path, 1);
 193  
 194          if ($foundNode == null) return false;
 195          return $foundNode;
 196      } //nodeExists
 197  
 198      /**
 199      * Generates a heirarchy of nodes based on a path expression
 200      * @param string The path expression
 201      * @param string The value of a text node to be appended to the last element
 202      * @return object The generated nodes
 203      */
 204      function &fromPath(&$xmldoc, $path, $text = null) {
 205          $pathSegments = explode('/', $path);
 206          $parent = null;
 207          $lastNode = null;
 208          $total = count($pathSegments);
 209  
 210          for ($i = 0; $i < $total; $i++) {
 211              if ($pathSegments[$i] != '') {
 212                  $currNode =& $xmldoc->createElement($pathSegments[$i]);
 213  
 214                  if ($parent == null) {
 215                      $parent =& $currNode;
 216                  }
 217                  else {
 218                      $lastNode->appendChild($currNode);
 219                  }
 220  
 221                  $lastNode =& $currNode;
 222              }
 223          }
 224  
 225          if ($text != null) {
 226              $currNode =& $xmldoc->createTextNode($text);
 227              $lastNode->appendChild($currNode);
 228          }
 229  
 230          return $parent;
 231      } //nodeExists
 232  } //nodetools
 233  
 234  ?>


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