Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/libraries/domit/xml_domit_getelementsbypath.php
f43d1a4680a9
8.2 KiB
text/x-php
menubar 0 to 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | <?php
/**
* @package domit-xmlparser
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
* DOMIT! is Free Software
**/
if (!defined('DOMIT_INCLUDE_PATH')) {
define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));
}
/** Separator for elements path */
define('GET_ELEMENTS_BY_PATH_SEPARATOR', '/');
/** Constant for an absolute path search (starting at the document root) */
define('GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE', 0);
/** Constant for a relative path search (starting at the level of the calling node) */
define('GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE', 1);
/** Constant for a variable path search (finds all matches, regardless of place in the hierarchy) */
define('GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE', 2);
/**
* getElementsByPath is a simple utility for path-based access to nodes in a DOMIT! document.
*/
class DOMIT_GetElementsByPath {
/** @var Object The node from which the search is called */
var $callingNode;
/** @var int The type of search to be performed, i.e., relative, absolute, or variable */
var $searchType;
/** @var Object The node that is the current parent of the search */
var $contextNode;
/** @var array An array containing a series of path segments for which to search */
var $arPathSegments = array();
/** @var Object A DOMIT_NodeList of matching nodes */
var $nodeList;
/** @var Object The index of the current node of the search */
var $targetIndex;
/** @var Object if true, the search will be aborted once the first match is found */
var $abortSearch = false;
/**
* Constructor - creates an empty DOMIT_NodeList to store matching nodes
*/
function DOMIT_GetElementsByPath() {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
$this->nodeList = new DOMIT_NodeList();
} //DOMIT_GetElementsByPath
/**
* Parses the supplied "path"-based pattern
* @param Object The node from which the search is called
* @param string The pattern
* @param int The node level of the current search
* @return Object The NodeList containing matching nodes
*/
function &parsePattern(&$node, $pattern, $nodeIndex = 0) {
$this->callingNode =& $node;
$pattern = trim($pattern);
$this->determineSearchType($pattern);
$this->setContextNode();
$this->splitPattern($pattern);
$this->targetIndex = $nodeIndex;
$totalSegments = count($this->arPathSegments);
if ($totalSegments > 0) {
if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE) {
$arContextNodes =& $this->contextNode->ownerDocument->getElementsByTagName($this->arPathSegments[0]);
$totalContextNodes = $arContextNodes->getLength();
for ($i = 0; $i < $totalContextNodes; $i++) {
$this->selectNamedChild($arContextNodes->item($i), 1);
}
}
else {
if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE) {
if ($this->contextNode->nodeName == $this->arPathSegments[0]) {
if (count($this->arPathSegments) == 1) {
$this->nodeList->appendNode($this->contextNode);
}
else {
$this->selectNamedChild($this->contextNode, 1);
}
}
}
else if ($this->searchType == GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE) {
$this->selectNamedChild($this->contextNode, 0);
}
}
}
if ($nodeIndex > 0) {
if ($nodeIndex <= $this->nodeList->getLength()) {
return $this->nodeList->item(($nodeIndex - 1));
}
else {
$null = null;
return $null;
}
}
return $this->nodeList;
} //parsePattern
/**
* Determines the type of search to be performed: absolute, relative, or variable
* @param string The pattern
*/
function determineSearchType($pattern) {
$firstChar = $pattern{0};
if ($firstChar != GET_ELEMENTS_BY_PATH_SEPARATOR) {
//relative path
$this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE;
}
else {
$secondChar = $pattern{1};
if ($secondChar != GET_ELEMENTS_BY_PATH_SEPARATOR) {
//absolute path
$this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE;
}
else {
//variable path
$this->searchType = GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE;
}
}
} //determineSearchType
/**
* Sets the context node, i.e., the node from which the search begins
*/
function setContextNode() {
switch($this->searchType) {
case GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE:
$this->contextNode =& $this->callingNode->ownerDocument->documentElement;
break;
case GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE:
if ($this->callingNode->uid != $this->callingNode->ownerDocument->uid) {
$this->contextNode =& $this->callingNode;
}
else {
$this->contextNode =& $this->callingNode->ownerDocument->documentElement;
}
break;
case GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE:
$this->contextNode =& $this->callingNode->ownerDocument->documentElement;
break;
}
} //setContextNode
/**
* Splits the supplied pattern into searchable segments
* @param string The pattern
*/
function splitPattern($pattern) {
switch($this->searchType) {
case GET_ELEMENTS_BY_PATH_SEARCH_ABSOLUTE:
$lastIndex = 1;
break;
case GET_ELEMENTS_BY_PATH_SEARCH_RELATIVE:
$lastIndex = 0;
break;
case GET_ELEMENTS_BY_PATH_SEARCH_VARIABLE:
$lastIndex = 2;
break;
}
$this->arPathSegments = explode(GET_ELEMENTS_BY_PATH_SEPARATOR, substr($pattern, $lastIndex));
} //splitPattern
/**
* Matches the current path segment against the child nodes of the current context node
* @param Object The context node
* @param int The index in the arPathSegments array of the current path segment
*/
function selectNamedChild(&$node, $pIndex) {
if (!$this->abortSearch) {
if ($pIndex < count($this->arPathSegments)) { //not at last path segment
$name = $this->arPathSegments[$pIndex];
$numChildren = $node->childCount;
for ($i = 0; $i < $numChildren; $i++) {
$currentChild =& $node->childNodes[$i];
if ($currentChild->nodeName == $name) {
$this->selectNamedChild($currentChild, ($pIndex + 1));
}
}
}
else {
$this->nodeList->appendNode($node);
if ($this->targetIndex == $this->nodeList->getLength()) {
$this->abortSearch = true;
}
}
}
} //selectNamedChild
} //DOMIT_GetElementsByPath
/**
* getElementsByAttributePath is a temporary utility requested by a DOMIT! user for path-based access to attributes in a DOMIT! document.
* This class may be absent from future versions of DOMIT!
*/
class DOMIT_GetElementsByAttributePath {
/** @var Object A DOMIT_NodeList of matching attribute nodes */
var $nodeList;
/**
* Constructor - creates an empty DOMIT_NodeList to store matching nodes
*/
function DOMIT_GetElementsByAttributePath() {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
$this->nodeList = new DOMIT_NodeList();
} //DOMIT_GetElementsByAttributePath
/**
* Matches the current path segment against the child nodes of the current context node
* @param Object The context node
* @param string The pattern
* @param int The index of the current path segment
*/
function &parsePattern(&$node, $pattern, $nodeIndex = 0) {
$beginSquareBrackets = strpos($pattern, '[');
if ($beginSquareBrackets != 0) {
$path = substr($pattern, 0, $beginSquareBrackets);
$attrPattern = substr($pattern, (strpos($pattern, '@') + 1));
$attrPattern = substr($attrPattern, 0, strpos($attrPattern, ')'));
$commaIndex = strpos($attrPattern, ',');
$key = trim(substr($attrPattern, 0, $commaIndex));
$value = trim(substr($attrPattern, ($commaIndex + 1)));
$value = substr($value, 1, (strlen($value) - 2));
$gebp = new DOMIT_GetElementsByPath();
$myResponse =& $gebp->parsePattern($node, $path);
$total = $myResponse->getLength();
for ($i = 0; $i < $total; $i++) {
$currNode =& $myResponse->item($i);
if ($currNode->hasAttribute($key)) {
if ($currNode->getAttribute($key) == $value) {
$this->nodeList->appendNode($currNode);
}
}
}
}
if ($nodeIndex == 0) {
return $this->nodeList;
}
else {
if ($nodeIndex <= $this->nodeList->getLength()) {
return $this->nodeList->item(($nodeIndex - 1));
}
else {
$this->nodeList = new DOMIT_NodeList();
return $this->nodeList;
}
}
} //parsePattern
} //DOMIT_GetElementsByAttributePath
?>
|