- md5
- c1bf110277d467512db756931806633c
- sha1
- ca0b1e8e19de2821bbc40731556a54715d821a46
- sha256
- 8394a32be3ac485105a7cf590a5c259b74a2cbe5e978a67e4458c5cb48aaf3a3
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
<?php
/**
* @package domit-xmlparser
* @subpackage domit-xmlparser-main
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <[email protected]>
* @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 absolute path */
define('DOMIT_XPATH_SEPARATOR_ABSOLUTE', '/');
/** Separator for relative path */
define('DOMIT_XPATH_SEPARATOR_RELATIVE', '//');
/** OR separator for multiple patterns */
define('DOMIT_XPATH_SEPARATOR_OR', '|');
/** Constant for an absolute path search (starting at the document root) */
define('DOMIT_XPATH_SEARCH_ABSOLUTE', 0);
/** Constant for a relative path search (starting at the level of the calling node) */
define('DOMIT_XPATH_SEARCH_RELATIVE', 1);
/** Constant for a variable path search (finds all matches, regardless of place in the hierarchy) */
define('DOMIT_XPATH_SEARCH_VARIABLE', 2);
/**
* DOMIT! XPath is an XPath parser.
*/
class DOMIT_XPath {
/** @var Object The node from which the search is called */
var $callingNode;
/** @var Object The node that is the current parent of the search */
var $searchType;
/** @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 string A temporary string container */
var $charContainer;
/** @var string The current character of the current pattern segment being parsed */
var $currChar;
/** @var string The current pattern segment being parsed */
var $currentSegment;
/** @var array A temporary node container for caching node references at the pattern level*/
var $globalNodeContainer;
/** @var array A temporary node container for caching node references at the pattern segment level */
var $localNodeContainer;
/** @var array Normalization table for XPath syntax */
var $normalizationTable = array('child::' => '', 'self::' => '.',
'attribute::' => '@', 'descendant::' => '*//',
"\t" => ' ', "\x0B" => ' ');
/** @var array A second-pass normalization table for XPath syntax */
var $normalizationTable2 = array(' =' => '=', '= ' => '=', ' <' => '<',
' >' => '>', '< ' => '<', '> ' => '>',
' !' => '!', '( ' => '(',
' )' => ')', ' ]' => ']', '] ' => ']',
' [' => '[', '[ ' => '[', ' /' => '/',
'/ ' => '/', '"' => "'");
/** @var array A third-pass normalization table for XPath syntax */
var $normalizationTable3 = array('position()=' => '',
'/descendant-or-self::node()/' => "//", 'self::node()' => '.',
'parent::node()' => '..');
/**
* Constructor - creates an empty DOMIT_NodeList to store matching nodes
*/
function DOMIT_XPath() {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
$this->nodeList = new DOMIT_NodeList();
} //DOMIT_XPath
/**
* Parses the supplied "path"-based pattern
* @param Object The node from which the search is called
* @param string The pattern
* @return Object The NodeList containing matching nodes
*/
function &parsePattern(&$node, $pattern, $nodeIndex = 0) {
$this->callingNode =& $node;
$pattern = $this->normalize(trim($pattern));
$this->splitPattern($pattern);
$total = count($this->arPathSegments);
//whole pattern level
for ($i = 0; $i < $total; $i++) {
$outerArray =& $this->arPathSegments[$i];
$this->initSearch($outerArray);
$outerTotal = count($outerArray);
$isInitialMatchAttempt = true;
//variable path segment level
for ($j = 0; $j < $outerTotal; $j++) {
$innerArray =& $outerArray[$j];
$innerTotal = count($innerArray);
if (!$isInitialMatchAttempt) {
$this->searchType = DOMIT_XPATH_SEARCH_VARIABLE;
}
//pattern segment level
for ($k = 0; $k < $innerTotal; $k++) {
$currentPattern = $innerArray[$k];
if (($k == 0) && ($currentPattern == null)) {
if ($innerTotal == 1) {
$isInitialMatchAttempt = false;
}
//else just skip current step and don't alter searchType
}
else {
if (!$isInitialMatchAttempt && ($k > 0)) {
$this->searchType = DOMIT_XPATH_SEARCH_RELATIVE;
}
$this->currentSegment = $currentPattern;
$this->processPatternSegment();
$isInitialMatchAttempt = false;
}
}
}
}
if ($nodeIndex > 0) {
if ($nodeIndex <= count($this->globalNodeContainer)) {
return $this->globalNodeContainer[($nodeIndex - 1)];
}
else {
$null = null;
return $null;
}
}
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$this->nodeList->appendNode($currNode);
}
}
return $this->nodeList;
} //parsePattern
/**
* Generates a new globalNodeContainer of matches
*/
function processPatternSegment() {
$total = strlen($this->currentSegment);
$this->charContainer = '';
$this->localNodeContainer = array();
for ($i = 0; $i < $total; $i++) {
// $this->currChar = $this->currentSegment{$i};
$this->currChar = substr($this->currentSegment, $i, 1);
switch ($this->currChar) {
case '@':
$this->selectAttribute(substr($this->currentSegment, ($this->currChar + 1)));
$this->updateNodeContainers();
return;
//break;
case '*':
if ($i == ($total - 1)) {
$this->selectNamedChild('*');
}
else {
$this->charContainer .= $this->currChar;
}
break;
case '.':
$this->charContainer .= $this->currChar;
if ($i == ($total - 1)) {
if ($this->charContainer == '..') {
$this->selectParent();
}
else {
return;
}
}
break;
case ')':
$this->charContainer .= $this->currChar;
$this->selectNodesByFunction();
break;
case '[':
$this->parsePredicate($this->charContainer,
substr($this->currentSegment, ($i + 1)));
return;
//break;
default:
$this->charContainer .= $this->currChar;
}
}
if ($this->charContainer != '') {
$this->selectNamedChild($this->charContainer);
}
$this->updateNodeContainers();
} //processPatternSegment
/**
* Replaces the global node container with the local node container
*/
function updateNodeContainers() {
$this->globalNodeContainer =& $this->localNodeContainer;
unset($this->localNodeContainer);
} //updateNodeContainers
/**
* Parses a predicate expression [...]
* @param string The pattern segment containing the node expression
* @param string The pattern segment containing the predicate expression
*/
function parsePredicate($nodeName, $patternSegment) {
$arPredicates =& explode('][', $patternSegment);
$total = count($arPredicates);
$lastIndex = $total - 1;
$arPredicates[$lastIndex] = substr($arPredicates[$lastIndex],
0, (strlen($arPredicates[$lastIndex]) - 1));
for ($i = 0; $i < $total; $i++) {
$isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false;
$currPredicate = $arPredicates[$i];
if (is_numeric($currPredicate)) {
if ($i == 0) {
$this->filterByIndex($nodeName, intval($currPredicate), $isRecursive);
}
else {
$this->refilterByIndex(intval($currPredicate));
}
}
else {
if ($i == 0) {
$this->selectNamedChild($nodeName);
$this->updateNodeContainers();
}
$phpExpression = $this->predicateToPHP($currPredicate);
$this->filterByPHPExpression($phpExpression);
}
$this->updateNodeContainers();
}
$this->charContainer = '';
} //parsePredicate
/**
* Converts the predicate into PHP evaluable code
* @param string The predicate
* @return string The converted PHP expression
*/
function predicateToPHP($predicate) {
$phpExpression = $predicate;
$currChar = '';
$charContainer = '';
$totalChars = strlen($predicate);
for ($i = 0; $i < $totalChars; $i++) {
$currChar = substr($predicate, $i, 1);
switch ($currChar) {
case '(':
case ')':
case ' ':
if ($charContainer != '') {
$convertedPredicate = $this->expressionToPHP($charContainer);
$phpExpression = str_replace($charContainer, $convertedPredicate, $phpExpression);
$charContainer = '';
}
break;
default:
$charContainer .= $currChar;
}
}
if ($charContainer != '') {
$convertedPredicate = $this->expressionToPHP($charContainer);
$phpExpression = str_replace($charContainer, $convertedPredicate, $phpExpression);
}
return $phpExpression;
} //predicateToPHP
/**
* Converts the predicate expression into a PHP expression
* @param string The predicate expression
* @return string The converted PHP expression
*/
function expressionToPHP($expression) {
if ($expression == 'and') {
$expression = '&&';
}
else if ($expression == 'or') {
$expression = '||';
}
else if ($expression == 'not') {
$expression = '!';
}
else {
$expression = trim($expression);
if (strpos($expression, '@') !== false) {
if (strpos($expression, '>=') !== false) {
$expression = str_replace('@', ('floatval($' . "contextNode->getAttribute('"), $expression);
$expression = str_replace('>=', "')) >= floatval(", $expression);
if (!is_numeric($expression)) $expression = str_replace('floatval', '', $expression);
$expression .= ')';
}
else if (strpos($expression, '<=') !== false) {
$expression = str_replace('@', ('floatval($' . "contextNode->getAttribute('"), $expression);
$expression = str_replace('<=', "')) <= floatval(", $expression);
if (!is_numeric($expression)) $expression = str_replace('floatval', '', $expression);
$expression .= ')';
}
else if (strpos($expression, '!=') !== false) {
$expression = str_replace('@', ('$' . "contextNode->getAttribute('"), $expression);
$expression = str_replace('!=', "') != ", $expression);
}
else if (strpos($expression, '=') !== false) {
$expression = str_replace('@', ('$' . "contextNode->getAttribute('"), $expression);
$expression = str_replace('=', "') == ", $expression);
}
else if (strpos($expression, '>') !== false) {
$expression = str_replace('>', "')) > floatval(", $expression); //reverse so > doesn't get replaced
$expression = str_replace('@', ('floatval($' . "contextNode->getAttribute('"), $expression);
if (!is_numeric($expression)) $expression = str_replace('floatval', '', $expression);
$expression .= ')';
}
else if (strpos($expression, '<') !== false) {
$expression = str_replace('@', ('floatval($' . "contextNode->getAttribute('"), $expression);
$expression = str_replace('<', "')) < floatval(", $expression);
if (!is_numeric($expression)) $expression = str_replace('floatval', '', $expression);
$expression .= ')';
}
else {
$expression = str_replace('@', ('$' . "contextNode->hasAttribute('"), $expression);
$expression.= "')";
}
}
else {
if (strpos($expression, '>=') !== false) {
$signPos = strpos($expression, '>=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 2)));
$expression = '$' . "this->hasNamedChildElementGreaterThanOrEqualToValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else if (strpos($expression, '<=') !== false) {
$signPos = strpos($expression, '>=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 2)));
$expression = '$' . "this->hasNamedChildElementLessThanOrEqualToValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else if (strpos($expression, '!=') !== false) {
$signPos = strpos($expression, '>=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 2)));
$expression = '$' . "this->hasNamedChildElementNotEqualToValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else if (strpos($expression, '=') !== false) {
$signPos = strpos($expression, '=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 1)));
$expression = '$' . "this->hasNamedChildElementEqualToValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else if (strpos($expression, '>') !== false) {
$signPos = strpos($expression, '=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 1)));
$expression = '$' . "this->hasNamedChildElementGreaterThanValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else if (strpos($expression, '<') !== false) {
$signPos = strpos($expression, '=');
$elementName = trim(substr($expression, 0, $signPos));
$elementValue = trim(substr($expression, ($signPos + 1)));
$expression = '$' . "this->hasNamedChildElementLessThanValue(" .
'$' . "contextNode, '" . $elementName . "', " .
$elementValue . ')';
}
else {
$expression = '$' . "this->hasNamedChildElement(" .
'$' . "contextNode, '" . $expression . "')";
}
}
}
return $expression;
} //expressionToPHP
/**
* Selects nodes that match the predicate expression
* @param string The predicate expression, formatted as a PHP expression
*/
function filterByPHPExpression($expression) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$contextNode =& $this->globalNodeContainer[$key];
if ($contextNode->nodeType == DOMIT_ELEMENT_NODE) {
$evaluatedExpression = 'if (' . $expression . ") $" .
'this->localNodeContainer[] =& $' . 'contextNode;';
eval($evaluatedExpression);
}
}
}
} //filterByPHPExpression
/**
* Selects nodes with child elements that match the specified name
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElement(&$parentNode, $nodeName) {
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
return true;
}
}
return false;
} //hasNamedChildElement
/**
* Selects nodes with child elements that match the specified name and text value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementEqualToValue(&$parentNode, $nodeName, $nodeValue) {
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) &&
($currNode->nodeName == $nodeName) && ($currNode->getText() == $nodeValue)) {
return true;
}
}
return false;
} //hasNamedChildElementEqualToValue
/**
* Selects nodes with child elements that are greater than or equal to the specified name and value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementGreaterThanOrEqualToValue(&$parentNode, $nodeName, $nodeValue) {
$isNumeric = false;
if (is_numeric($nodeValue)) {
$isNumeric = true;
$nodeValue = floatval($nodeValue);
}
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
if ($isNumeric) {$compareVal = floatval($currNode->getText());}
else {$compareVal = $currNode->getText();}
if ($compareVal >= $nodeValue) return true;
}
}
return false;
} //hasNamedChildElementGreaterThanOrEqualToValue
/**
* Selects nodes with child elements that are less than or equal to the specified name and value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementLessThanOrEqualToValue(&$parentNode, $nodeName, $nodeValue) {
$isNumeric = false;
if (is_numeric($nodeValue)) {
$isNumeric = true;
$nodeValue = floatval($nodeValue);
}
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
if ($isNumeric) {$compareVal = floatval($currNode->getText());}
else {$compareVal = $currNode->getText();}
if ($compareVal <= $nodeValue) return true;
}
}
return false;
} //hasNamedChildElementLessThanOrEqualToValue
/**
* Selects nodes with child elements that are not equal to the specified name and value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementNotEqualToValue(&$parentNode, $nodeName, $nodeValue) {
$isNumeric = false;
if (is_numeric($nodeValue)) {
$isNumeric = true;
$nodeValue = floatval($nodeValue);
}
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
if ($isNumeric) {$compareVal = floatval($currNode->getText());}
else {$compareVal = $currNode->getText();}
if ($compareVal != $nodeValue) return true;
}
}
return false;
} //hasNamedChildElementNotEqualToValue
/**
* Selects nodes with child elements that are greater than the specified name and value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementGreaterThanValue(&$parentNode, $nodeName, $nodeValue) {
$isNumeric = false;
if (is_numeric($nodeValue)) {
$isNumeric = true;
$nodeValue = floatval($nodeValue);
}
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
if ($isNumeric) {$compareVal = floatval($currNode->getText());}
else {$compareVal = $currNode->getText();}
if ($compareVal > $nodeValue) return true;
}
}
return false;
} //hasNamedChildElementGreaterThanValue
/**
* Selects nodes with child elements that are less than the specified name and value
* @param object The parent node of the child elements to match
* @param string The tag name to match on
* @param string The text string to match on
* @return boolean True if a matching child element exists
*/
function hasNamedChildElementLessThanValue(&$parentNode, $nodeName, $nodeValue) {
$isNumeric = false;
if (is_numeric($nodeValue)) {
$isNumeric = true;
$nodeValue = floatval($nodeValue);
}
$total = $parentNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $parentNode->childNodes[$i];
if (($currNode->nodeType == DOMIT_ELEMENT_NODE) && ($currNode->nodeName == $nodeName)) {
if ($isNumeric) {$compareVal = floatval($currNode->getText());}
else {$compareVal = $currNode->getText();}
if ($compareVal < $nodeValue) return true;
}
}
return false;
} //hasNamedChildElementLessThanValue
/**
* Selects named elements of the specified index
* @param string The pattern segment containing the node expression
* @param int The index (base 1) of the matching node
* @param boolean True if the selection is to be performed recursively
*/
function refilterByIndex($index) {
if ($index > 1) {
if (count($this->globalNodeContainer) != 0) {
$counter = 0;
$lastParentID = null;
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
if (($lastParentID != null) && ($currNode->parentNode->uid != $lastParentID)) {
$counter = 0;
}
$counter++;
if (($counter == $index) && ($currNode->parentNode->uid == $lastParentID)) {
$this->localNodeContainer[] =& $currNode;
}
$lastParentID = $currNode->parentNode->uid;
}
}
}
else {
$this->localNodeContainer =& $this->globalNodeContainer;
}
} //refilterByIndex
/**
* Selects named elements of the specified index
* @param string The pattern segment containing the node expression
* @param int The index (base 1) of the matching node
* @param boolean True if the selection is to be performed recursively
*/
function filterByIndex($nodeName, $index, $deep) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$this->_filterByIndex($currNode, $nodeName, $index, $deep);
}
}
} //filterByIndex
/**
* Selects named elements of the specified index
* @param object The context node
* @param string The pattern segment containing the node expression
* @param int The index (base 1) of the matching node
* @param boolean True if the selection is to be performed recursively
*/
function _filterByIndex(&$contextNode, $nodeName, $index, $deep) {
if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) ||
($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) {
$total = $contextNode->childCount;
$nodeCounter = 0;
for ($i = 0; $i < $total; $i++) {
$currChildNode =& $contextNode->childNodes[$i];
if ($currChildNode->nodeName == $nodeName) {
$nodeCounter++;
if ($nodeCounter == $index) {
$this->localNodeContainer[] =& $currChildNode;
}
}
if ($deep) {
$this->_filterByIndex($currChildNode, $nodeName, $index, $deep);
}
}
}
} //_filterByIndex
/**
* Selects named elements with the specified named child
* @param string The pattern segment containing the node expression
* @param string The tag name of the matching child
* @param boolean True if the selection is to be performed recursively
*/
function filterByChildName($nodeName, $childName, $deep) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$this->_filterByChildName($currNode, $nodeName, $childName, $deep);
}
}
} //filterByChildName
/**
* Selects named elements with the specified named child
* @param object The context node
* @param string The pattern segment containing the node expression
* @param string The tag name of the matching child
* @param boolean True if the selection is to be performed recursively
*/
function _filterByChildName(&$contextNode, $nodeName, $childName, $deep) {
if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) ||
($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) {
$total = $contextNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currChildNode =& $contextNode->childNodes[$i];
if (($currChildNode->nodeName == $nodeName) &&
($currChildNode->nodeType == DOMIT_ELEMENT_NODE)) {
$total2 = $currChildNode->childCount;
for ($j = 0; $j < $total2; $j++) {
$currChildChildNode =& $currChildNode->childNodes[$j];
if ($currChildChildNode->nodeName == $childName) {
$this->localNodeContainer[] =& $currChildNode;
}
}
}
if ($deep) {
$this->_filterByChildName($currChildNode, $nodeName, $childName, $deep);
}
}
}
} //_filterByChildName
/**
* Selects named attributes of the current context nodes
* @param string The attribute name, or * to match all attributes
*/
function selectAttribute($attrName) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false;
$this->_selectAttribute($currNode, $attrName, $isRecursive);
}
}
$this->charContainer = '';
} //selectAttribute
/**
* Selects all attributes of the context nodes
* @param object The context node
* @param string The attribute name, or * to match all attributes
* @param boolean True if the selection is to be performed recursively
*/
function _selectAttribute(&$contextNode, $attrName, $deep) {
if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) ||
($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) {
$total = $contextNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $contextNode->childNodes[$i];
if ($currNode->nodeType == DOMIT_ELEMENT_NODE) {
if ($attrName == '*') {
$total2 = $currNode->attributes->getLength();
for ($j = 0; $j < $total2; $j++) {
$this->localNodeContainer[] =& $currNode->attributes->item($j);
}
}
else {
if ($currNode->hasAttribute($attrName)) {
$this->localNodeContainer[] =& $currNode->getAttributeNode($attrName);
}
}
}
if ($deep) {
$this->_selectAttribute($currNode, $attrName, $deep);
}
}
}
} //_selectAttribute
/**
* Selects all child nodes of the current context nodes
* @param string The element name
*/
function selectNamedChild($tagName) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false;
$this->_selectNamedChild($currNode, $tagName, $isRecursive);
}
}
$this->charContainer = '';
} //selectNamedChild
/**
* Selects all child nodes of the context node
* @param object The context node
* @param string The element name
* @param boolean True if the selection is to be performed recursively
*/
function _selectNamedChild(&$contextNode, $tagName, $deep = false) {
if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) ||
($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) {
$total = $contextNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currChildNode =& $contextNode->childNodes[$i];
if (($currChildNode->nodeType == DOMIT_ELEMENT_NODE) ||
($currChildNode->nodeType == DOMIT_DOCUMENT_NODE)) {
if (($tagName == '*') || ($tagName == $currChildNode->nodeName)) {
$this->localNodeContainer[] =& $currChildNode;
}
if ($deep) {
$this->_selectNamedChild($currChildNode, $tagName, $deep);
}
}
}
}
} //_selectNamedChild
/**
* Selects parent node of the current context nodes
*/
function selectParent() {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
$isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false;
$this->_selectParent($currNode, $isRecursive);
}
}
$this->charContainer = '';
} //selectParent
/**
* Selects parent node of the current context nodes
* @param object The context node
* @param boolean True if the selection is to be performed recursively
*/
function _selectParent(&$contextNode, $deep = false) {
if ($contextNode->nodeType == DOMIT_ELEMENT_NODE) {
if ($contextNode->parentNode != null) {
$this->localNodeContainer[] =& $contextNode->parentNode;
}
}
if ($deep) {
if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) ||
($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) {
$total = $contextNode->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $contextNode->childNodes[$i];
if ($currNode->nodeType == DOMIT_ELEMENT_NODE) {
$this->_selectParent($contextNode, $deep);
}
}
}
}
} //_selectParent
/**
* Selects any nodes of the current context nodes which match the given function
*/
function selectNodesByFunction() {
$doProcess = false;
$targetNodeType = -1;
switch (strtolower(trim($this->charContainer))) {
case 'last()':
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
if ($currNode->nodeType == DOMIT_ELEMENT_NODE) {
if ($currNode->lastChild != null) {
$this->localNodeContainer[] =& $currNode->lastChild;
}
}
}
}
break;
case 'text()':
$doProcess = true;
$targetNodeType = DOMIT_TEXT_NODE;
break;
case 'comment()':
$doProcess = true;
$targetNodeType = DOMIT_COMMENT_NODE;
break;
case 'processing-instruction()':
$doProcess = true;
$targetNodeType = DOMIT_PROCESSING_INSTRUCTION_NODE;
break;
}
if ($doProcess) {
if (count($this->globalNodeContainer) != 0) {
foreach ($this->globalNodeContainer as $key =>$value) {
$currNode =& $this->globalNodeContainer[$key];
if ($currNode->nodeType == DOMIT_ELEMENT_NODE) {
$total = $currNode->childCount;
for ($j = 0; $j < $total; $j++) {
if ($currNode->childNodes[$j]->nodeType == $targetNodeType) {
$this->localNodeContainer[] =& $currNode->childNodes[$j];
}
}
}
}
}
}
$this->charContainer = '';
} //selectNodesByFunction
/**
* Splits the supplied pattern into searchable segments
* @param string The pattern
*/
function splitPattern($pattern) {
//split multiple patterns if they exist (e.g. pattern1 | pattern2 | pattern3)
$this->arPathSegments =& explode(DOMIT_XPATH_SEPARATOR_OR, $pattern);
//split each pattern by relative path dividers (i.e., '//')
$total = count($this->arPathSegments);
for ($i = 0; $i < $total; $i++) {
$this->arPathSegments[$i] =& explode(DOMIT_XPATH_SEPARATOR_RELATIVE, trim($this->arPathSegments[$i]));
$currArray =& $this->arPathSegments[$i];
$total2 = count($currArray);
for ($j = 0; $j < $total2; $j++) {
$currArray[$j] =& explode(DOMIT_XPATH_SEPARATOR_ABSOLUTE, $currArray[$j]);
}
}
} //splitPattern
/**
* Converts long XPath syntax into abbreviated XPath syntax
* @param string The pattern
* @return string The normalized pattern
*/
function normalize($pattern) {
$pattern = strtr($pattern, $this->normalizationTable);
while (strpos($pattern, ' ') !== false) {
$pattern = str_replace(' ', ' ', $pattern);
}
$pattern = strtr($pattern, $this->normalizationTable2);
$pattern = strtr($pattern, $this->normalizationTable3);
return $pattern;
} //normalize
/**
* Initializes the contextNode and searchType
* @param array The current array of path segments
* @return int The index of the first array item to begin the search at
*/
function initSearch(&$currArPathSegments) {
$this->globalNodeContainer = array();
if (is_null($currArPathSegments[0])) {
if (count($currArPathSegments) == 1) {
//variable path
$this->searchType = DOMIT_XPATH_SEARCH_VARIABLE;
$this->globalNodeContainer[] =& $this->callingNode->ownerDocument;
}
else {
//absolute path
$this->searchType = DOMIT_XPATH_SEARCH_ABSOLUTE;
$this->globalNodeContainer[] =& $this->callingNode->ownerDocument;
}
}
else {
//relative path
$this->searchType = DOMIT_XPATH_SEARCH_RELATIVE;
if ($this->callingNode->uid != $this->callingNode->ownerDocument->uid) {
$this->globalNodeContainer[] =& $this->callingNode;
}
else {
$this->globalNodeContainer[] =& $this->callingNode->ownerDocument;
}
}
} //initSearch
} //DOMIT_XPath
?>
The same content is in 23 releases, Joomla 1.5.4 to 1.5.26. See them all, or every version of this file.