- md5
- 17226e93e2b740844ce1ccb71da5f728
- sha1
- f8dfca7f1488df885bc753ee57a4829e4f595126
- sha256
- 5dc3a3e06036d5efdc9d31a03376f4639851d0bcbb0aa6d1e984ec6c421e81ee
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
<?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};
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 11 releases, Joomla 1.0.3 to 1.0.15. See them all, or every version of this file.