[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

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

   1  <?php
   2  /**

   3  * @package domit-xmlparser

   4  * @copyright (C) 2004 John Heinstein. All rights reserved

   5  * @license http://www.gnu.org/copyleft/lesser.html LGPL License

   6  * @author John Heinstein <johnkarl@nbnet.nb.ca>

   7  * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page

   8  * DOMIT! is Free Software

   9  **/
  10  
  11  if (!defined('DOMIT_INCLUDE_PATH')) {
  12      define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  13  }
  14  
  15  /** Separator for elements path */

  16  define('GET_ELEMENTS_BY_PATH_SEPARATOR', '/');
  17  /** Constant for an absolute path search (starting at the document root) */

  18  define('GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE', 0);
  19  /** Constant for a relative path search (starting at the level of the calling node) */

  20  define('GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE', 1);
  21  /** Constant for a variable path search (finds all matches, regardless of place in the hierarchy) */

  22  define('GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE', 2);
  23  
  24  /**

  25  * getElementsByPath is a simple utility for path-based access to nodes in a DOMIT! document.

  26  */
  27  class DOMIT_GetElementsByPath {
  28      /** @var Object The node from which the search is called */

  29      var $callingNode;
  30      /** @var int The type of search to be performed, i.e., relative, absolute, or variable */

  31      var $searchType;
  32      /** @var Object The node that is the current parent of the search */

  33      var $contextNode;
  34      /** @var array An array containing a series of path segments for which to search */

  35      var $arPathSegments = array();
  36      /** @var Object A DOMIT_NodeList of matching nodes */

  37      var $nodeList;
  38      /** @var Object The index of the current node of the search */

  39      var $targetIndex;
  40      /** @var Object if true, the search will be aborted once the first match is found */

  41      var $abortSearch = false;
  42  
  43      /**

  44      * Constructor - creates an empty DOMIT_NodeList to store matching nodes

  45      */
  46  	function DOMIT_GetElementsByPath() {
  47          require_once (DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
  48          $this->nodeList = new DOMIT_NodeList();
  49      } //DOMIT_GetElementsByPath

  50  
  51      /**

  52      * Parses the supplied "path"-based pattern

  53      * @param Object The node from which the search is called

  54      * @param string The pattern

  55      * @param int The node level of the current search

  56      * @return Object The NodeList containing matching nodes

  57      */
  58      function &parsePattern(&$node, $pattern, $nodeIndex = 0) {
  59          $this->callingNode =& $node;
  60          $pattern = trim($pattern);
  61  
  62          $this->determineSearchType($pattern);
  63          $this->setContextNode();
  64          $this->splitPattern($pattern);
  65  
  66          $this->targetIndex = $nodeIndex;
  67          $totalSegments = count($this->arPathSegments);
  68  
  69          if ($totalSegments > 0) {
  70              if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE) {
  71                  $arContextNodes =& $this->contextNode->ownerDocument->getElementsByTagName($this->arPathSegments[0]);
  72                  $totalContextNodes = $arContextNodes->getLength();
  73  
  74                  for ($i = 0; $i < $totalContextNodes; $i++) {
  75                      $this->selectNamedChild($arContextNodes->item($i), 1);
  76                  }
  77              }
  78              else {
  79                  if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE) {
  80                      if ($this->contextNode->nodeName == $this->arPathSegments[0]) {
  81                          if (count($this->arPathSegments) == 1) {
  82                              $this->nodeList->appendNode($this->contextNode);
  83                          }
  84                          else {
  85                              $this->selectNamedChild($this->contextNode, 1);
  86                          }
  87                      }
  88                  }
  89                  else if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE) {
  90                      $this->selectNamedChild($this->contextNode, 0);
  91                  }
  92              }
  93          }
  94  
  95          if ($nodeIndex > 0) {
  96              if ($nodeIndex <= $this->nodeList->getLength()) {
  97                  return $this->nodeList->item(($nodeIndex - 1));
  98              }
  99              else {
 100                  $null = null;
 101                  return $null;
 102              }
 103          }
 104  
 105          return $this->nodeList;
 106      } //parsePattern

 107  
 108      /**

 109      * Determines the type of search to be performed: absolute, relative, or variable

 110      * @param string The pattern

 111      */
 112  	function determineSearchType($pattern) {
 113          $firstChar = $pattern{0};
 114  
 115          if ($firstChar != GET_ELEMENTS_BY_PATH_SEPARATOR) {
 116              //relative path

 117              $this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE;
 118          }
 119          else {
 120              $secondChar = $pattern{1};
 121  
 122              if ($secondChar != GET_ELEMENTS_BY_PATH_SEPARATOR) {
 123                  //absolute path

 124                  $this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE;
 125              }
 126              else {
 127                  //variable path

 128                  $this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE;
 129              }
 130          }
 131      } //determineSearchType

 132  
 133  
 134      /**

 135      * Sets the context node, i.e., the node from which the search begins

 136      */
 137  	function setContextNode() {
 138          switch($this->searchType) {
 139              case GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE:
 140                  $this->contextNode =& $this->callingNode->ownerDocument->documentElement;
 141                  break;
 142  
 143              case GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE:
 144                  if ($this->callingNode->uid != $this->callingNode->ownerDocument->uid) {
 145                      $this->contextNode =& $this->callingNode;
 146                  }
 147                  else {
 148                      $this->contextNode =& $this->callingNode->ownerDocument->documentElement;
 149                  }
 150                  break;
 151  
 152              case GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE:
 153                  $this->contextNode =& $this->callingNode->ownerDocument->documentElement;
 154                  break;
 155          }
 156      } //setContextNode

 157  
 158      /**

 159      * Splits the supplied pattern into searchable segments

 160      * @param string The pattern

 161      */
 162  	function splitPattern($pattern) {
 163          switch($this->searchType) {
 164              case GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE:
 165                  $lastIndex = 1;
 166                  break;
 167  
 168              case GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE:
 169                  $lastIndex = 0;
 170                  break;
 171  
 172              case GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE:
 173                  $lastIndex = 2;
 174                  break;
 175          }
 176  
 177          $this->arPathSegments = explode(GET_ELEMENTS_BY_PATH_SEPARATOR, substr($pattern, $lastIndex));
 178      } //splitPattern

 179  
 180      /**

 181      * Matches the current path segment against the child nodes of the current context node

 182      * @param Object The context node

 183      * @param int The index in the arPathSegments array of the current path segment

 184      */
 185  	function selectNamedChild(&$node, $pIndex) {
 186          if (!$this->abortSearch) {
 187              if ($pIndex < count($this->arPathSegments)) { //not at last path segment
 188                  $name = $this->arPathSegments[$pIndex];
 189                  $numChildren = $node->childCount;
 190  
 191                  for ($i = 0; $i < $numChildren; $i++) {
 192                      $currentChild =& $node->childNodes[$i];
 193  
 194                      if ($currentChild->nodeName == $name) {
 195                          $this->selectNamedChild($currentChild, ($pIndex + 1));
 196                      }
 197                  }
 198              }
 199              else {
 200                  $this->nodeList->appendNode($node);
 201  
 202                  if ($this->targetIndex == $this->nodeList->getLength()) {
 203                      $this->abortSearch = true;
 204                  }
 205              }
 206          }
 207      } //selectNamedChild

 208  } //DOMIT_GetElementsByPath

 209  
 210  
 211  
 212  /**

 213  * getElementsByAttributePath is a temporary utility requested by a DOMIT! user for path-based access to attributes in a DOMIT! document.

 214  * This class may be absent from future versions of DOMIT!

 215  */
 216  class DOMIT_GetElementsByAttributePath {
 217      /** @var Object A DOMIT_NodeList of matching attribute nodes */

 218      var $nodeList;
 219  
 220      /**

 221      * Constructor - creates an empty DOMIT_NodeList to store matching nodes

 222      */
 223  	function DOMIT_GetElementsByAttributePath() {
 224          require_once (DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
 225          $this->nodeList = new DOMIT_NodeList();
 226      } //DOMIT_GetElementsByAttributePath

 227  
 228      /**

 229      * Matches the current path segment against the child nodes of the current context node

 230      * @param Object The context node

 231      * @param string The pattern

 232      * @param int The index of the current path segment

 233      */
 234      function &parsePattern(&$node, $pattern, $nodeIndex = 0) {
 235          $beginSquareBrackets = strpos($pattern, '[');
 236  
 237          if ($beginSquareBrackets != 0) {
 238              $path = substr($pattern, 0, $beginSquareBrackets);
 239  
 240              $attrPattern = substr($pattern, (strpos($pattern, '@') + 1));
 241              $attrPattern = substr($attrPattern, 0, strpos($attrPattern, ')'));
 242  
 243              $commaIndex = strpos($attrPattern, ',');
 244              $key = trim(substr($attrPattern, 0, $commaIndex));
 245              $value = trim(substr($attrPattern, ($commaIndex + 1)));
 246              $value = substr($value, 1, (strlen($value) - 2));
 247  
 248              $gebp = new DOMIT_GetElementsByPath();
 249              $myResponse =& $gebp->parsePattern($node, $path);
 250  
 251              $total = $myResponse->getLength();
 252  
 253              for ($i = 0; $i < $total; $i++) {
 254                  $currNode =& $myResponse->item($i);
 255  
 256                  if ($currNode->hasAttribute($key)) {
 257                      if ($currNode->getAttribute($key) == $value) {
 258                          $this->nodeList->appendNode($currNode);
 259                      }
 260                  }
 261              }
 262          }
 263  
 264          if ($nodeIndex == 0) {
 265              return $this->nodeList;
 266          }
 267          else {
 268              if ($nodeIndex <= $this->nodeList->getLength()) {
 269                  return $this->nodeList->item(($nodeIndex - 1));
 270              }
 271              else {
 272                  $this->nodeList = new DOMIT_NodeList();
 273                  return $this->nodeList;
 274              }
 275          }
 276      } //parsePattern

 277  } //DOMIT_GetElementsByAttributePath

 278  ?>


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