[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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


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