cvs: pear /XML_XPath XPath.php /XML_XPath/XPath common.php result.php
| From: | Daniel Allen | Date: | Fri, 07 Jun 2002 23:17:22 +0000 |
| Subject: | cvs: pear /XML_XPath XPath.php /XML_XPath/XPath common.php result.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4489@lists.php.net to get a copy of this message | ||
dallen Fri Jun 7 19:17:22 2002 EDT
Modified files:
/pear/XML_XPath XPath.php
/pear/XML_XPath/XPath common.php result.php
Log:
Major changes:
getAttribute('foo', true) now puts you on the attribute, not the element node
cleaned up the XML_XPath_result initialization (no longer create an StdClass when calling class)
fixed sorting for childNodes() and getElementsByTagName() generated result objects
cleaned up sorting in result object, and added comments (so I don't forget again how I did
it)
major speed enhancements (calling user defined functions methods is slow, so tried to trim that
down)
don't create a result object in the _quick_evaluate function since we don't need it
anyway
enabled relative xpath queries differently, in_xpathQuery = array(query, node) for relative...
added relative xpath queries for the _quick_evaluate feature
anything else I just can't remember I will mention in my next commit
Index: pear/XML_XPath/XPath.php diff -u pear/XML_XPath/XPath.php:1.12 pear/XML_XPath/XPath.php:1.13 --- pear/XML_XPath/XPath.php:1.12 Thu Jun 6 20:13:14 2002 +++ pear/XML_XPath/XPath.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: XPath.php,v 1.12 2002/06/07 00:13:14 dallen Exp $ +// $Id: XPath.php,v 1.13 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -41,13 +41,14 @@ define('XML_XPATH_NO_DOM', -5); define('XML_XPATH_INVALID_INDEX', -6); define('XML_XPATH_INVALID_NODESET', -7); -define('XML_XPATH_NOT_LOADED', -8); -define('XML_XPATH_INVALID_NODETYPE', -9); -define('XML_XPATH_FILE_NOT_WRITABLE', -10); -define('XML_XPATH_NODE_REQUIRED', -11); -define('XML_XPATH_INDEX_SIZE', -12); -define('XML_PARSE_ERROR', -13); -define('XML_DUPLICATE_ROOT', -14); +define('XML_XPATH_NULL_POINTER', -8); +define('XML_XPATH_NOT_LOADED', -9); +define('XML_XPATH_INVALID_NODETYPE', -10); +define('XML_XPATH_FILE_NOT_WRITABLE', -11); +define('XML_XPATH_NODE_REQUIRED', -12); +define('XML_XPATH_INDEX_SIZE', -13); +define('XML_PARSE_ERROR', -14); +define('XML_DUPLICATE_ROOT', -15); // }}} // {{{ includes @@ -191,31 +192,43 @@ * @access public * @return object result object {or XML_XPath_Error exception} */ - function evaluate($in_xpathQuery, $in_movePointer = false, $in_relative = false) + function &evaluate($in_xpathQuery, $in_movePointer = false) { // Make sure we have loaded an xml document and were able to create an xpath context - if (!is_a_php_class($this->ctx, 'xpathcontext')) { + if (get_class($this->ctx) != 'XPathContext') { return PEAR::raiseError(null, XML_XPATH_NOT_LOADED, null, E_USER_ERROR, null, 'XML_XPath_Error', true); } - if ($in_relative) { + // enable relative xpath queries (I don't check a valid dom object yet) + settype($in_xpathQuery, 'array'); + if (isset($in_xpathQuery[1])) { $sep = '/'; // those double slashes cause an anomally - if (substr($in_xpathQuery, 0, 2) == '//') { + if (substr($in_xpathQuery[0], 0, 2) == '//') { $sep = ''; } + + if ($in_xpathQuery[1] == 'current()') { + $in_xpathQuery[1] = $this->getNodePath($this->pointer); + } + else { + $in_xpathQuery[1] = $this->getNodePath($in_xpathQuery[1]); + } - $in_xpathQuery = $this->getNodePath($this->pointer) . $sep . $in_xpathQuery; + $xpathQuery = $in_xpathQuery[1] . $sep . $in_xpathQuery[0]; + } + else { + $xpathQuery = reset($in_xpathQuery); } - if (!$result = @xpath_eval($this->ctx, $in_xpathQuery)) { - return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); + if (!$result = @xpath_eval($this->ctx, $xpathQuery)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); } - $resultObj = new XML_XPath_result($result, $in_xpathQuery, $this->xml, $this->ctx); + $resultObj =& new XML_XPath_result($result->type == XPATH_NODESET ? $result->nodeset : $result->value, $result->type, $xpathQuery, $this->ctx); - if ($in_movePointer && $resultObj->resultType() == XPATH_NODESET && $resultObj->numResults()) { - $this->setPointer($resultObj->getPointer()); + if ($in_movePointer && $result->type == XPATH_NODESET && !empty($result->nodeset)) { + $this->pointer = reset($result->nodeset); } return $resultObj; @@ -265,6 +278,7 @@ XML_XPATH_INVALID_INDEX => 'invalid index', XML_XPATH_INVALID_NODESET => 'requires nodeset and one of appropriate type', XML_XPATH_NOT_LOADED => 'DomDocument has not been loaded', + XML_XPATH_NULL_POINTER => 'Null pointer, probably due to empty result object', XML_XPATH_INVALID_NODETYPE => 'invalid nodetype for requested feature', XML_XPATH_FILE_NOT_WRITABLE => 'file could not be written', XML_XPATH_NODE_REQUIRED => 'DomNode required for operation', Index: pear/XML_XPath/XPath/common.php diff -u pear/XML_XPath/XPath/common.php:1.15 pear/XML_XPath/XPath/common.php:1.16 --- pear/XML_XPath/XPath/common.php:1.15 Thu Jun 6 20:13:15 2002 +++ pear/XML_XPath/XPath/common.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: common.php,v 1.15 2002/06/07 00:13:15 dallen Exp $ +// $Id: common.php,v 1.16 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -53,16 +53,7 @@ } // }}} -/** - * changes to domxml that I have to look at - * - * implement the striping of formating on the way in possibly - * append_sibling() added - * insert_before() now copies node before inserting - * domxml_parser() might be good for bringing in stuff - * when using getAttribute with the move = true, we should sit on the attribute node, not the parent element - * let's eliminate _quick_evaluate_shutdown since calling a function is slow and it just does a check and calls another function - */ + // {{{ class XML_XPath_common /** @@ -108,17 +99,30 @@ class XML_XPath_common { // {{{ properties - /** @var object current location in xml document */ + /** + * domxml node of the current location in the xml document + * @var object $pointer + */ var $pointer; - /** @var object bookmark used for holding a place in the document */ + /** + * domxml node bookmark used for holding a place in the xml document + * @var object $bookmark + */ var $bookmark; - /** @var boolean when stepping through nodes, should we skip empty text nodes */ + /** + * when working with the xml document, ignore the presence of blank nodes (white space) + * @var boolean $skipBlanks + */ var $skipBlanks = true; - /** @var file path to xmllint for reformating **/ - var $xmllint = "xmllint"; + /** + * path to xmllint used for reformating the xml output + * [!] should be using System_Command for this [!] + * @var string $xmllint + */ + var $xmllint = 'xmllint'; // }}} // {{{ string nodeName() @@ -134,13 +138,19 @@ */ function nodeName($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } $nodeName = $this->pointer->node_name(); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } return $nodeName; } @@ -159,40 +169,21 @@ */ function nodeType($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $nodeType = $this->pointer->node_type(); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $nodeType; - } - // }}} - // {{{ boolean parentNode() + $nodeType = $this->pointer->node_type(); - /** - * Moves the internal pointer to the parent of the current node or returns the pointer. - * - * @param boolean $in_movePointer (optional) move the internal pointer or return reference - * - * @access public - * @return boolean whether pointer was moved or object pointer to parent - */ - function parentNode($in_movePointer = true) - { - $parent = $this->pointer->parent_node(); - if ($parent) { - if ($in_movePointer) { - $this->pointer = $parent; - return true; - } - else { - return $parent; - } - } - else { - return false; + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); } + + return $nodeType; } // }}} @@ -204,47 +195,81 @@ * Similar to an xpath query, this function will grab all the first descendant child * nodes of the element node at the current position and will create an XML_XPath_result * object of type nodeset with each of the child nodes as the nodes. - * - * @param string $in_xpathQuery (optional) quick xpath query - * @param boolean $in_movePointer (optional) move the internal pointer with quick xpath query + * DOM query functions do not take an xpathQuery argument * * @access public * @return object XML_XPath_result object of type nodeset * [!] important note: since we had to hack the result object a bit, you cannot sort the * result object when generated in this manner right now [!] */ - function &childNodes($in_xpathQuery = null, $in_movePointer = false) + function &childNodes() { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { - return $result; + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); } - // since we can't do an actual xpath query, we need to create a pseudo xpath result - $xpathObj = new StdClass(); - $xpathObj->type = XPATH_NODESET; + $nodeset = array(); foreach($this->pointer->child_nodes() as $childNode) { // if this is a blank node and we are skipping blank nodes...skip to next child if ($childNode->is_blank_node() && $this->skipBlanks) { continue; } - $xpathObj->nodeset[] = $childNode; + $nodeset[] = $childNode; } - $resultObj =& new XML_XPath_result($xpathObj, null, $this->xml, $this->ctx); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $resultObj; + return new XML_XPath_result($nodeset, XPATH_NODESET, array($this->pointer, '/*'), $this->ctx); } // }}} // {{{ object getElementsByTagName() + /** + * Create an XML_XPath_result object with the elements with the specified tagname + * + * DOM query functions do not take an xpathQuery argument + * + * @param string $in_tagName + * + * @return object XML_XPath_result object of matching nodes + * @access public + */ function getElementsByTagName($in_tagName) { // since we can't do an actual xpath query, we need to create a pseudo xpath result - $xpathObj = new StdClass(); - $xpathObj->type = XPATH_NODESET; - $xpathObj->nodeset = $this->xml->get_elements_by_tagname($in_tagName); - $resultObj =& new XML_XPath_result($xpathObj, null, $this->xml, $this->ctx); - return $resultObj; + $nodeset = $this->xml->get_elements_by_tagname($in_tagName); + return new XML_XPath_result($nodeset, XPATH_NODESET, array(null, '//' . $in_tagName), $this->ctx); + } + + // }}} + // {{{ boolean parentNode() + + /** + * Moves the internal pointer to the parent of the current node or returns the pointer. + * Step functions do not take an xpathQuery argument + * + * @param boolean $in_movePointer (optional) move the internal pointer or return reference + * + * @access public + * @return boolean whether pointer was moved or object pointer to parent + */ + function parentNode($in_movePointer = true) + { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + $parent = $this->pointer->parent_node(); + if ($parent) { + if ($in_movePointer) { + $this->pointer = $parent; + return true; + } + else { + return $parent; + } + } + else { + return false; + } } // }}} @@ -253,6 +278,7 @@ /** * Moves the internal pointer to the next sibling of the current node, or returns the pointer. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -261,9 +287,14 @@ */ function nextSibling($in_movePointer = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (!$this->pointer->next_sibling()) { return false; } + $next = $this->pointer->next_sibling(); if ($this->skipBlanks) { while (true) { @@ -304,6 +335,7 @@ * Moves the internal pointer to the previous sibling * of the current node or returns the pointer. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -312,9 +344,14 @@ */ function previousSibling($in_movePointer = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (!$this->pointer->previous_sibling()) { return false; } + $previous = $this->pointer->previous_sibling(); if ($this->skipBlanks) { while (true) { @@ -349,6 +386,7 @@ /** * Moves the pointer to the first child of this node or returns the first node. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -357,9 +395,14 @@ */ function firstChild($in_movePointer = true) { - if (!$this->hasChildNodes()) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if (!$this->pointer->has_child_nodes()) { return false; } + $first = $this->pointer->first_child(); if ($this->skipBlanks) { while (true) { @@ -394,6 +437,7 @@ /** * Moves the pointer to the last child of this node or returns the last child. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -402,9 +446,14 @@ */ function lastChild($in_movePointer = true) { - if (!$this->hasChildNodes()) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if (!$this->pointer->has_child_nodes()) { return false; } + $last = $this->pointer->last_child(); if ($this->skipBlanks) { while (true) { @@ -434,29 +483,6 @@ } // }}} - // {{{ boolean isNodeType() - - /** - * Determines if the node type is equivalent to the node type requested. - * - * @param int $in_type DOM node type defined by the xml node type constants - * @param string $in_xpathQuery (optional) quick xpath query - * @param boolean $in_movePointer (optional) move internal pointer with quick xpath query - * - * @access public - * @return boolean same type of node {or XML_XPath_Error exception} - */ - function isNodeType($in_type, $in_xpathQuery = null, $in_movePointer = false) - { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { - return $result; - } - $isNodeType = $this->pointer->node_type() == $in_type ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $isNodeType; - } - - // }}} // {{{ boolean hasChildNodes() /** @@ -470,11 +496,20 @@ */ function hasChildNodes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $hasChildNodes = $this->pointer->has_child_nodes() ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $hasChildNodes = $this->pointer->has_child_nodes(); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $hasChildNodes; } @@ -492,11 +527,20 @@ */ function hasAttributes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $hasAttributes = $this->pointer->has_attributes() ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $hasAttributes = $this->pointer->has_attributes(); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $hasAttributes; } @@ -516,20 +560,20 @@ */ function hasAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $hasAttribute = false; - if (!is_array($attributeNodes = $this->pointer->attributes())) { - $attributeNodes = array(); - } - foreach($attributeNodes as $attributeNode) { - if ($attributeNode->name == $in_name) { - $hasAttribute = true; - break; - } + + $hasAttribute = $this->pointer->has_attribute($in_name) ? true : false; + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + return $hasAttribute; } @@ -548,16 +592,25 @@ */ function getAttributes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } + $return = array(); if (is_array($attributeNodes = $this->pointer->attributes())) { foreach($attributeNodes as $attributeNode) { $return[$attributeNode->name] = $attributeNode->value; } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $return; } @@ -569,7 +622,8 @@ * * Grab the attribute value if it exists and return it. If the attribute does not * exist, this function will return the boolean 'false', so be sure to check properly - * if the value is "" or if the attribute doesn't exist at all. + * if the value is "" or if the attribute doesn't exist at all. This function is the + * only attribute function which allows you to step onto the attribute node. * * @param string $in_name Name of the attribute * @param string $in_xpathQuery (optional) quick xpath query @@ -580,13 +634,23 @@ */ function getAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } $result = $this->pointer->get_attribute($in_name); + // if we found the attribute, move to it if we are moving the pointer + if ($result && $in_movePointer) { + $this->pointer = $this->pointer->get_attribute_node($in_name); + } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $result; } @@ -605,15 +669,25 @@ * @param boolean $in_movePointer (optional) move internal pointer * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function setAttribute($in_name, $in_value, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $this->pointer->set_attribute($in_name, $in_value); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $result = $this->pointer->set_attribute($in_name, $in_value); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + + return $result; } // }}} @@ -627,15 +701,25 @@ * @param boolean $in_movePointer (optional) move internal pointer * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function removeAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $this->pointer->remove_attribute($in_name); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $result = $this->pointer->remove_attribute($in_name); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + + return $result; } // }}} @@ -657,6 +741,10 @@ */ function substringData($in_offset = 0, $in_count = 0, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_TEXT_NODE, XML_ELEMENT_NODE, XML_CDATA_SECTION_NODE, XML_COMMENT_NODE)))) { return $result; } @@ -667,12 +755,15 @@ elseif (!is_int($in_offset) || $in_offset < 0 || $in_offset > strlen($content = $this->pointer->get_content())) { $return = PEAR::raiseError(null, XML_XPATH_INDEX_SIZE, null, E_USER_WARNING, "Offset: $in_offset", 'XML_XPath_Error', true); } - // if this is an element node, concat all the text children recursively else { $return = $in_count ? substr($content, $in_offset, $in_count) : substr($content, $in_offset); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $return; } @@ -766,16 +857,23 @@ * * @access public * @return object pointer to old node {or XML_XPath_Error exception} - * @todo I should make it so an xml_xpath_result is returned or something + * [!] I should make it so an xml_xpath_result is returned or something [!] */ function replaceChild($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_TEXT_NODE, XML_COMMENT_NODE, XML_CDATA_SECTION_NODE, XML_PI_NODE)))) { return $result; } if (XML_XPath::isError($importedNodes = $this->_build_fragment($in_xmlData))) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $importedNodes; } @@ -791,7 +889,9 @@ } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $oldNode; } @@ -812,12 +912,20 @@ */ function appendChild($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_DOCUMENT_NODE)))) { return $result; } + // if this is a document node, make sure no root exists - if ($this->isNodeType(XML_DOCUMENT_NODE) && $this->xml->document_element()) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($this->pointer->node_type() == XML_DOCUMENT_NODE && $this->xml->document_element()) { + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return PEAR::raiseError(null, XML_DUPLICATE_ROOT, null, E_USER_WARNING, null, 'XML_XPath_Error', true); } @@ -830,7 +938,11 @@ $newNode = $node; } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $newNode; } @@ -849,6 +961,10 @@ */ function insertBefore($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } @@ -867,7 +983,9 @@ } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $newNode; } @@ -889,6 +1007,10 @@ */ function removeChild($in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_TEXT_NODE, XML_COMMENT_NODE, XML_PI_NODE, XML_CDATA_SECTION_NODE)))) { return $result; } @@ -900,7 +1022,9 @@ // set pointer to the parent $this->pointer = $parent; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $removedNode; } @@ -923,12 +1047,19 @@ */ function replaceChildren($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } if (XML_XPath::isError($importedNodes = $this->_build_fragment($in_xmlData))) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $importedNodes; } @@ -946,7 +1077,9 @@ $this->pointer->append_child($importedNode); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } } // }}} @@ -963,6 +1096,10 @@ */ function dumpChildren($in_xpathQuery = null, $in_movePointer = false, $in_format = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } @@ -970,7 +1107,9 @@ $xmlString = trim($this->xml->dump_node($this->pointer, $in_format)); $xmlString = substr($xmlString,strpos($xmlString,'>')+1,-(strlen($this->nodeName())+3)); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $xmlString; } @@ -1024,7 +1163,11 @@ */ function toString($in_xpathQuery = null, $in_movePointer = false, $in_format = true) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } @@ -1040,7 +1183,9 @@ $xmlString = `echo $xmlString | {$this->xmllint} --format - 2>&1`; }*/ - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } return $xmlString; } @@ -1097,16 +1242,18 @@ * * @return string xpath location query * @access public - * [?] maybe add caching [?] */ - function getNodePath($in_xpathQuery = null, $in_movePointer = false) + function getNodePath($in_node) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { - return $result; + if (!$in_node) { + return null; + } + elseif (!$this->_is_dom_node($in_node)) { + return PEAR::raiseError(null, XML_XPATH_NODE_REQUIRED, null, E_USER_WARNING, $in_node, 'XML_XPath_Error', true); } $buffer = ''; - $cur = $this->pointer; + $cur = $in_node; do { $name = ''; $sep = '/'; @@ -1116,7 +1263,7 @@ break; } - $next = NULL; + $next = false; } else if ($type == XML_ATTRIBUTE_NODE) { $sep .= '@'; @@ -1129,7 +1276,7 @@ // now figure out the index $tmp = $cur->previous_sibling(); - while ($tmp != NULL) { + while ($tmp != false) { if ($name == $tmp->node_name()) { $occur++; } @@ -1167,10 +1314,8 @@ $cur = $next; - } while ($cur != NULL); + } while ($cur != false); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $buffer; } @@ -1219,6 +1364,10 @@ */ function _set_content($in_content, $in_xpathQuery, $in_movePointer, $in_replace, $in_offset = 0, $in_count = 0) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE, XML_COMMENT_NODE, XML_PI_NODE)))) { return $result; } @@ -1242,7 +1391,9 @@ $this->pointer->set_content($data); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $return; } @@ -1278,21 +1429,6 @@ } // }}} - // {{{ _set_bookmark() - - /** - * Set a temporary bookmark of the internal pointer while doing a quick xpath query - * - * @access private - * @return void - */ - function _set_bookmark($in_location) - { - $this->bookmark = $this->pointer; - $this->pointer = $in_location; - } - - // }}} // {{{ _quick_evaluate_init() /** @@ -1310,62 +1446,80 @@ */ function _quick_evaluate_init($in_xpathQuery = null, $in_movePointer = false, $in_nodeTypes = null) { - if (!is_null($in_xpathQuery)) { - if ($this->_is_dom_node($in_xpathQuery)) { + // we don't need to check for null, since false or 0 is not a valid query anyway + if ($in_xpathQuery) { + if (!is_object($in_xpathQuery)) { + // doing the following manually (without evaluate()) is critical for speed + + // Make sure we have an xpath context (mildly costly) + if (get_class($this->ctx) != 'XPathContext') { + return PEAR::raiseError(null, XML_XPATH_NOT_LOADED, null, E_USER_ERROR, null, 'XML_XPath_Error', true); + } + + // enable relative xpath queries (I don't check a valid dom object yet) + settype($in_xpathQuery, 'array'); + if (isset($in_xpathQuery[1])) { + $sep = '/'; + // those double slashes cause an anomally + if (substr($in_xpathQuery[0], 0, 2) == '//') { + $sep = ''; + } + + if ($in_xpathQuery[1] == 'current()') { + $in_xpathQuery[1] = $this->getNodePath($this->pointer); + } + else { + $in_xpathQuery[1] = $this->getNodePath($in_xpathQuery[1]); + } + + $xpathQuery = $in_xpathQuery[1] . $sep . $in_xpathQuery[0]; + } + else { + $xpathQuery = reset($in_xpathQuery); + } + + if (!$result = @xpath_eval($this->ctx, $xpathQuery)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); + } + + if (empty($result->nodeset) || $result->type != XPATH_NODESET) { + return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); + } + + // this takes the first result (too bad if you had more) + $tmpPointer = reset($result->nodeset); + } + // a bit costly, so we put it second + elseif ($this->_is_dom_node($in_xpathQuery)) { $tmpPointer = $in_xpathQuery; } else { - if (XML_XPath::isError($resultObj = $this->evaluate($in_xpathQuery))) { - return $resultObj; - } - // make sure we have at least one node - if (!($resultObj->numResults() && $resultObj->resultType() == XPATH_NODESET)) { - return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); - } - $resultObj->nextNode(); - $tmpPointer = $resultObj->getPointer(); + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); } + // if we are moving the internal pointer, then do it now if ($in_movePointer) { $this->pointer = $tmpPointer; } // set the bookmark if we only want to temporarily move the pointer + // this area is too critical to call class methods...we have to be nasty else { - $this->_set_bookmark($tmpPointer); + $this->bookmark = $this->pointer; + $this->pointer = $tmpPointer; } } - // see if we have a restricted nodeType requirement + + // see if we have a restricted nodeType requirement (negligible time) if (is_array($in_nodeTypes) && !in_array($nodeType = $this->pointer->node_type(), $in_nodeTypes)) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return PEAR::raiseError(null, XML_XPATH_INVALID_NODETYPE, null, E_USER_WARNING, "Required type: ".implode(" or ", $in_nodeTypes).", Provided type: ".$nodeType, 'XML_XPath_Error', true); } else { return true; } - } - - // }}} - // {{{ _quick_evaluate_shutdown() - - /** - * Restore the internal pointer if there was no request to permanently move it with the - * quick query. This should never return error because it is only called if the init was - * successful. - * - * @param string $in_xpathQuery options quick xpath query to execute - * @param boolean $in_movePointer allow quick query to move the internal pointer - * - * @access private - * @return boolean always true - */ - function _quick_evaluate_shutdown($in_xpathQuery = null, $in_movePointer = false) - { - // if we did quick xpath query and didn't want to move - // our internal pointer, then reset it - if (!is_null($in_xpathQuery) && !$in_movePointer) { - $this->_restore_bookmark(); - } - return true; } // }}} Index: pear/XML_XPath/XPath/result.php diff -u pear/XML_XPath/XPath/result.php:1.9 pear/XML_XPath/XPath/result.php:1.10 --- pear/XML_XPath/XPath/result.php:1.9 Thu Jun 6 20:13:15 2002 +++ pear/XML_XPath/XPath/result.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: result.php,v 1.9 2002/06/07 00:13:15 dallen Exp $ +// $Id: result.php,v 1.10 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -56,41 +56,44 @@ class XML_XPath_result extends XML_XPath_common { // {{{ properties - /** @var object reference to result object */ - var $result; - - /** @var string original xpath query, stored just for debug info */ + /** + * original xpath query, stored when we need to sort + * @var string $query + */ var $query; - /** @var int current index of the result set */ - var $index; - - /** @var boolean determines if we have counted the first node */ - var $rewound; + /** + * determines if we have counted the first node of the result nodeset + * @var boolean $isRewound + */ + var $isRewound; - /** @var int one of 4 constants that correspond to the xpath result types */ + /** + * The type of result that the query generated + * @var int $type + */ var $type; - /** @var mixed either array of nodesets, string, boolean or number from xpath result */ + /** + * either array of nodesets, string, boolean or number from xpath/DOM query + * @var mixed $data + */ var $data; - /** @var object pointer to xml data object */ - var $xml; - - /** @var object pointer to xpath context object for the xml data object */ + /** + * xpath context object for the current domxml object + * @var object $ctx + */ var $ctx; // }}} // {{{ constructor - function XML_XPath_result($in_result, $in_query, &$in_xml, &$in_ctx) + function XML_XPath_result($in_data, $in_type, $in_query, &$in_ctx) { - $this->result = &$in_result; $this->query = $in_query; - $this->type = $this->result->type; - $this->data = isset($this->result->nodeset) ? $this->result->nodeset : $this->result->value; - $this->index = 0; - $this->xml = &$in_xml; + $this->type = $in_type; + $this->data = $in_data; $this->ctx = &$in_ctx; // move the pointer to the first node if at least one node in the result exists // for convience, just so we don't have to call nextNode() if we expect only one @@ -114,7 +117,7 @@ return $this->data ? true : false; break; case XPATH_NODESET: - return $this->isNodeType(XML_ATTRIBUTE_NODE) ? $this->pointer->value() : $this->substringData(); + return $this->pointer->node_type() == XML_ATTRIBUTE_NODE ? $this->pointer->value() : $this->substringData(); break; case XPATH_STRING: case XPATH_NUMBER: @@ -166,15 +169,17 @@ } // }}} - // {{{ void sort() + // {{{ boolean sort() /** * Sort the nodeset in this result. The sort can be either ascending or descending, and * the comparisons can be text, number or natural (see the constants above). The sort * axis is provided as an xpath query and is the location path relative to the node given. * For example, so sort on an attribute, you would provide '@foo' and it will look at the - * attribute for each node. If the axis is not found, it comes first in the sort order for - * ascending order. + * attribute for each node. + * + * NOTE: If the axis is not found, the node will comes first in the sort order for ascending + * order and at the end for descending orde. * * @param string $in_sortXpath relative xpath query location to each node in nodeset * @param int $in_order either XML_XPATH_SORT_TEXT_[DE|A]SCENDING, @@ -182,43 +187,73 @@ * XML_XPATH_SORT_NATURAL_[DE|A]SCENDING * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function sort($in_sortXpath = '.', $in_order = XML_XPATH_SORT_TEXT_ASCENDING) { - if ($this->resultType() != XPATH_NODESET) { + // make sure we are dealing with a result that is a nodeset + if ($this->type != XPATH_NODESET) { return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_NOTICE, $this->data, 'XPath_Error', true); } - if ($in_sortXpath == '') { - $in_sortXpath = '.'; - } - $xpathResult = @$this->ctx->xpath_eval($this->query . '/' . $in_sortXpath . '|' . $this->query); - if (!$xpathResult || !$xpathResult->nodeset) { - return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_NOTICE, "Query {$this->query}/$in_sortXPath", 'XML_XPath_Error', true); - } + $data = array(); - $this->index = 0; - if (sizeof($xpathResult->nodeset) == sizeof($this->data)) { - foreach ($xpathResult->nodeset as $index => $node) { - array_push($data, $xpathResult->nodeset[$index]->get_content()); + + // we don't need to run it again if we are soring on the current node values + if ($in_sortXpath == '' || $in_sortXpath == '.') { + foreach ($this->data as $index => $node) { + $data[] = $node->get_content(); } } + // we need to run the query again else { - foreach ($xpathResult->nodeset as $index => $node) { - if (!isset($this->data[$this->index]) || $node != $this->data[$this->index]) { - continue; - } + // we never actually ran the query, but we can rebuild it...and we will do that now + // this is for DOM queries, such as childNodes() and getElementsByTagName() + if (is_array($this->query)) { + $this->query = $this->getNodePath(reset($this->query)) . end($this->query); + } + + // here I am reissuing the query, but with the sort path appended followed by the + // node in a logical 'OR'. The trick here is that I can keep the original nodes + // in sorted order and then just weed out the nodes I used to sort. + $xpathResult = @$this->ctx->xpath_eval($this->query . '/' . $in_sortXpath . '|' . $this->query); + if (!$xpathResult || empty($xpathResult->nodeset)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_NOTICE, "Query {$this->query}/$in_sortXPath", 'XML_XPath_Error', true); + } - if (isset($xpathResult->nodeset[$index + 1])) { - if ($xpathResult->nodeset[$index + 1] == $this->data[$this->index]) { - array_push($data, ''); + // Sorting Process: + // The reason we did a double query is so that we could line up the original nodes + // with the original data set, fill in any parts of the sorted nodeset that have + // missing sort nodes, sort the sorted nodeset and then reindex the original data array + + $origIndex = 0; + $sortIndex = 0; + while(isset($this->data[$origIndex])) { + // make sure we are lined up on original nodes, then we can proceed to check + // the next node in each nodeset to determine of the sort node was found + if ($this->data[$origIndex] == $xpathResult->nodeset[$sortIndex]) { + $origIndex++; + $sortIndex++; + // make sure we have not advanced beyond the end of the sort nodeset + if (isset($xpathResult->nodeset[$sortIndex])) { + // if the values of the next two indices of the sort nodeset and the + // original nodeset are the same, we had a missing node + if (isset($this->data[$origIndex]) && $this->data[$origIndex] == $xpathResult->nodeset[$sortIndex]) { + $data[] = ''; + } + // okay, they were different, we found a sort node, get its value + else { + $data[] = $xpathResult->nodeset[$sortIndex]->get_content(); + } } + // the last sort nodeset element is missing, which means the sort nodeset + // was missing the last sort node else { - array_push($data, $xpathResult->nodeset[$index + 1]->get_content()); + $data[] = ''; } } - - $this->index++; + else { + $sortIndex++; + } } } @@ -245,21 +280,26 @@ case XML_XPATH_SORT_NATURAL_DESCENDING: natsort($data); - $data = array_reverse($data, TRUE); + $data = array_reverse($data, true); break; default: asort($data); break; } + $dataReordered = array(); - $this->index = 0; + // this is NOT just array_values, we need to use the keys to put the values + // in the correct order foreach ($data as $reindex => $value) { - $dataReordered[$this->index++] = $this->data[$reindex]; + $dataReordered[] = $this->data[$reindex]; } + $this->data = $dataReordered; - $this->index = 0; - $this->pointer = $this->data[$this->index]; + // rewind to the beginning of the data set + $this->rewind(); + + return true; } // }}} @@ -275,7 +315,7 @@ { if (is_array($this->data)) { $this->pointer = reset($this->data); - $this->rewound = true; + $this->isRewound = true; return true; } @@ -299,8 +339,8 @@ function next() { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; $seekFunction = 'reset'; } else { @@ -332,8 +372,8 @@ function nextByNodeName($in_name) { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; if (($node = reset($this->data)) && $node->node_name() == $in_name) { $this->pointer = $node; return true; @@ -367,8 +407,8 @@ function nextByNodeType($in_type) { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; if (($node = reset($this->data)) && $node->node_type() == $in_type) { $this->pointer = $node; return true;
Index: pear/XML_XPath/XPath.php diff -u pear/XML_XPath/XPath.php:1.12 pear/XML_XPath/XPath.php:1.13 --- pear/XML_XPath/XPath.php:1.12 Thu Jun 6 20:13:14 2002 +++ pear/XML_XPath/XPath.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: XPath.php,v 1.12 2002/06/07 00:13:14 dallen Exp $ +// $Id: XPath.php,v 1.13 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -41,13 +41,14 @@ define('XML_XPATH_NO_DOM', -5); define('XML_XPATH_INVALID_INDEX', -6); define('XML_XPATH_INVALID_NODESET', -7); -define('XML_XPATH_NOT_LOADED', -8); -define('XML_XPATH_INVALID_NODETYPE', -9); -define('XML_XPATH_FILE_NOT_WRITABLE', -10); -define('XML_XPATH_NODE_REQUIRED', -11); -define('XML_XPATH_INDEX_SIZE', -12); -define('XML_PARSE_ERROR', -13); -define('XML_DUPLICATE_ROOT', -14); +define('XML_XPATH_NULL_POINTER', -8); +define('XML_XPATH_NOT_LOADED', -9); +define('XML_XPATH_INVALID_NODETYPE', -10); +define('XML_XPATH_FILE_NOT_WRITABLE', -11); +define('XML_XPATH_NODE_REQUIRED', -12); +define('XML_XPATH_INDEX_SIZE', -13); +define('XML_PARSE_ERROR', -14); +define('XML_DUPLICATE_ROOT', -15); // }}} // {{{ includes @@ -191,31 +192,43 @@ * @access public * @return object result object {or XML_XPath_Error exception} */ - function evaluate($in_xpathQuery, $in_movePointer = false, $in_relative = false) + function &evaluate($in_xpathQuery, $in_movePointer = false) { // Make sure we have loaded an xml document and were able to create an xpath context - if (!is_a_php_class($this->ctx, 'xpathcontext')) { + if (get_class($this->ctx) != 'XPathContext') { return PEAR::raiseError(null, XML_XPATH_NOT_LOADED, null, E_USER_ERROR, null, 'XML_XPath_Error', true); } - if ($in_relative) { + // enable relative xpath queries (I don't check a valid dom object yet) + settype($in_xpathQuery, 'array'); + if (isset($in_xpathQuery[1])) { $sep = '/'; // those double slashes cause an anomally - if (substr($in_xpathQuery, 0, 2) == '//') { + if (substr($in_xpathQuery[0], 0, 2) == '//') { $sep = ''; } + + if ($in_xpathQuery[1] == 'current()') { + $in_xpathQuery[1] = $this->getNodePath($this->pointer); + } + else { + $in_xpathQuery[1] = $this->getNodePath($in_xpathQuery[1]); + } - $in_xpathQuery = $this->getNodePath($this->pointer) . $sep . $in_xpathQuery; + $xpathQuery = $in_xpathQuery[1] . $sep . $in_xpathQuery[0]; + } + else { + $xpathQuery = reset($in_xpathQuery); } - if (!$result = @xpath_eval($this->ctx, $in_xpathQuery)) { - return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); + if (!$result = @xpath_eval($this->ctx, $xpathQuery)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); } - $resultObj = new XML_XPath_result($result, $in_xpathQuery, $this->xml, $this->ctx); + $resultObj =& new XML_XPath_result($result->type == XPATH_NODESET ? $result->nodeset : $result->value, $result->type, $xpathQuery, $this->ctx); - if ($in_movePointer && $resultObj->resultType() == XPATH_NODESET && $resultObj->numResults()) { - $this->setPointer($resultObj->getPointer()); + if ($in_movePointer && $result->type == XPATH_NODESET && !empty($result->nodeset)) { + $this->pointer = reset($result->nodeset); } return $resultObj; @@ -265,6 +278,7 @@ XML_XPATH_INVALID_INDEX => 'invalid index', XML_XPATH_INVALID_NODESET => 'requires nodeset and one of appropriate type', XML_XPATH_NOT_LOADED => 'DomDocument has not been loaded', + XML_XPATH_NULL_POINTER => 'Null pointer, probably due to empty result object', XML_XPATH_INVALID_NODETYPE => 'invalid nodetype for requested feature', XML_XPATH_FILE_NOT_WRITABLE => 'file could not be written', XML_XPATH_NODE_REQUIRED => 'DomNode required for operation', Index: pear/XML_XPath/XPath/common.php diff -u pear/XML_XPath/XPath/common.php:1.15 pear/XML_XPath/XPath/common.php:1.16 --- pear/XML_XPath/XPath/common.php:1.15 Thu Jun 6 20:13:15 2002 +++ pear/XML_XPath/XPath/common.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: common.php,v 1.15 2002/06/07 00:13:15 dallen Exp $ +// $Id: common.php,v 1.16 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -53,16 +53,7 @@ } // }}} -/** - * changes to domxml that I have to look at - * - * implement the striping of formating on the way in possibly - * append_sibling() added - * insert_before() now copies node before inserting - * domxml_parser() might be good for bringing in stuff - * when using getAttribute with the move = true, we should sit on the attribute node, not the parent element - * let's eliminate _quick_evaluate_shutdown since calling a function is slow and it just does a check and calls another function - */ + // {{{ class XML_XPath_common /** @@ -108,17 +99,30 @@ class XML_XPath_common { // {{{ properties - /** @var object current location in xml document */ + /** + * domxml node of the current location in the xml document + * @var object $pointer + */ var $pointer; - /** @var object bookmark used for holding a place in the document */ + /** + * domxml node bookmark used for holding a place in the xml document + * @var object $bookmark + */ var $bookmark; - /** @var boolean when stepping through nodes, should we skip empty text nodes */ + /** + * when working with the xml document, ignore the presence of blank nodes (white space) + * @var boolean $skipBlanks + */ var $skipBlanks = true; - /** @var file path to xmllint for reformating **/ - var $xmllint = "xmllint"; + /** + * path to xmllint used for reformating the xml output + * [!] should be using System_Command for this [!] + * @var string $xmllint + */ + var $xmllint = 'xmllint'; // }}} // {{{ string nodeName() @@ -134,13 +138,19 @@ */ function nodeName($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } $nodeName = $this->pointer->node_name(); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } return $nodeName; } @@ -159,40 +169,21 @@ */ function nodeType($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $nodeType = $this->pointer->node_type(); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $nodeType; - } - // }}} - // {{{ boolean parentNode() + $nodeType = $this->pointer->node_type(); - /** - * Moves the internal pointer to the parent of the current node or returns the pointer. - * - * @param boolean $in_movePointer (optional) move the internal pointer or return reference - * - * @access public - * @return boolean whether pointer was moved or object pointer to parent - */ - function parentNode($in_movePointer = true) - { - $parent = $this->pointer->parent_node(); - if ($parent) { - if ($in_movePointer) { - $this->pointer = $parent; - return true; - } - else { - return $parent; - } - } - else { - return false; + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); } + + return $nodeType; } // }}} @@ -204,47 +195,81 @@ * Similar to an xpath query, this function will grab all the first descendant child * nodes of the element node at the current position and will create an XML_XPath_result * object of type nodeset with each of the child nodes as the nodes. - * - * @param string $in_xpathQuery (optional) quick xpath query - * @param boolean $in_movePointer (optional) move the internal pointer with quick xpath query + * DOM query functions do not take an xpathQuery argument * * @access public * @return object XML_XPath_result object of type nodeset * [!] important note: since we had to hack the result object a bit, you cannot sort the * result object when generated in this manner right now [!] */ - function &childNodes($in_xpathQuery = null, $in_movePointer = false) + function &childNodes() { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { - return $result; + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); } - // since we can't do an actual xpath query, we need to create a pseudo xpath result - $xpathObj = new StdClass(); - $xpathObj->type = XPATH_NODESET; + $nodeset = array(); foreach($this->pointer->child_nodes() as $childNode) { // if this is a blank node and we are skipping blank nodes...skip to next child if ($childNode->is_blank_node() && $this->skipBlanks) { continue; } - $xpathObj->nodeset[] = $childNode; + $nodeset[] = $childNode; } - $resultObj =& new XML_XPath_result($xpathObj, null, $this->xml, $this->ctx); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $resultObj; + return new XML_XPath_result($nodeset, XPATH_NODESET, array($this->pointer, '/*'), $this->ctx); } // }}} // {{{ object getElementsByTagName() + /** + * Create an XML_XPath_result object with the elements with the specified tagname + * + * DOM query functions do not take an xpathQuery argument + * + * @param string $in_tagName + * + * @return object XML_XPath_result object of matching nodes + * @access public + */ function getElementsByTagName($in_tagName) { // since we can't do an actual xpath query, we need to create a pseudo xpath result - $xpathObj = new StdClass(); - $xpathObj->type = XPATH_NODESET; - $xpathObj->nodeset = $this->xml->get_elements_by_tagname($in_tagName); - $resultObj =& new XML_XPath_result($xpathObj, null, $this->xml, $this->ctx); - return $resultObj; + $nodeset = $this->xml->get_elements_by_tagname($in_tagName); + return new XML_XPath_result($nodeset, XPATH_NODESET, array(null, '//' . $in_tagName), $this->ctx); + } + + // }}} + // {{{ boolean parentNode() + + /** + * Moves the internal pointer to the parent of the current node or returns the pointer. + * Step functions do not take an xpathQuery argument + * + * @param boolean $in_movePointer (optional) move the internal pointer or return reference + * + * @access public + * @return boolean whether pointer was moved or object pointer to parent + */ + function parentNode($in_movePointer = true) + { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + $parent = $this->pointer->parent_node(); + if ($parent) { + if ($in_movePointer) { + $this->pointer = $parent; + return true; + } + else { + return $parent; + } + } + else { + return false; + } } // }}} @@ -253,6 +278,7 @@ /** * Moves the internal pointer to the next sibling of the current node, or returns the pointer. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -261,9 +287,14 @@ */ function nextSibling($in_movePointer = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (!$this->pointer->next_sibling()) { return false; } + $next = $this->pointer->next_sibling(); if ($this->skipBlanks) { while (true) { @@ -304,6 +335,7 @@ * Moves the internal pointer to the previous sibling * of the current node or returns the pointer. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -312,9 +344,14 @@ */ function previousSibling($in_movePointer = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (!$this->pointer->previous_sibling()) { return false; } + $previous = $this->pointer->previous_sibling(); if ($this->skipBlanks) { while (true) { @@ -349,6 +386,7 @@ /** * Moves the pointer to the first child of this node or returns the first node. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -357,9 +395,14 @@ */ function firstChild($in_movePointer = true) { - if (!$this->hasChildNodes()) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if (!$this->pointer->has_child_nodes()) { return false; } + $first = $this->pointer->first_child(); if ($this->skipBlanks) { while (true) { @@ -394,6 +437,7 @@ /** * Moves the pointer to the last child of this node or returns the last child. * If the flag is on to skip blank nodes then the first non-blank node is used. + * Step functions do not take an xpathQuery argument * * @param boolean $in_movePointer (optional) move the internal pointer or return reference * @@ -402,9 +446,14 @@ */ function lastChild($in_movePointer = true) { - if (!$this->hasChildNodes()) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if (!$this->pointer->has_child_nodes()) { return false; } + $last = $this->pointer->last_child(); if ($this->skipBlanks) { while (true) { @@ -434,29 +483,6 @@ } // }}} - // {{{ boolean isNodeType() - - /** - * Determines if the node type is equivalent to the node type requested. - * - * @param int $in_type DOM node type defined by the xml node type constants - * @param string $in_xpathQuery (optional) quick xpath query - * @param boolean $in_movePointer (optional) move internal pointer with quick xpath query - * - * @access public - * @return boolean same type of node {or XML_XPath_Error exception} - */ - function isNodeType($in_type, $in_xpathQuery = null, $in_movePointer = false) - { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { - return $result; - } - $isNodeType = $this->pointer->node_type() == $in_type ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $isNodeType; - } - - // }}} // {{{ boolean hasChildNodes() /** @@ -470,11 +496,20 @@ */ function hasChildNodes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $hasChildNodes = $this->pointer->has_child_nodes() ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $hasChildNodes = $this->pointer->has_child_nodes(); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $hasChildNodes; } @@ -492,11 +527,20 @@ */ function hasAttributes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } - $hasAttributes = $this->pointer->has_attributes() ? true : false; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $hasAttributes = $this->pointer->has_attributes(); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $hasAttributes; } @@ -516,20 +560,20 @@ */ function hasAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $hasAttribute = false; - if (!is_array($attributeNodes = $this->pointer->attributes())) { - $attributeNodes = array(); - } - foreach($attributeNodes as $attributeNode) { - if ($attributeNode->name == $in_name) { - $hasAttribute = true; - break; - } + + $hasAttribute = $this->pointer->has_attribute($in_name) ? true : false; + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + return $hasAttribute; } @@ -548,16 +592,25 @@ */ function getAttributes($in_xpathQuery = null, $in_movePointer = false) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } + $return = array(); if (is_array($attributeNodes = $this->pointer->attributes())) { foreach($attributeNodes as $attributeNode) { $return[$attributeNode->name] = $attributeNode->value; } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $return; } @@ -569,7 +622,8 @@ * * Grab the attribute value if it exists and return it. If the attribute does not * exist, this function will return the boolean 'false', so be sure to check properly - * if the value is "" or if the attribute doesn't exist at all. + * if the value is "" or if the attribute doesn't exist at all. This function is the + * only attribute function which allows you to step onto the attribute node. * * @param string $in_name Name of the attribute * @param string $in_xpathQuery (optional) quick xpath query @@ -580,13 +634,23 @@ */ function getAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } $result = $this->pointer->get_attribute($in_name); + // if we found the attribute, move to it if we are moving the pointer + if ($result && $in_movePointer) { + $this->pointer = $this->pointer->get_attribute_node($in_name); + } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $result; } @@ -605,15 +669,25 @@ * @param boolean $in_movePointer (optional) move internal pointer * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function setAttribute($in_name, $in_value, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $this->pointer->set_attribute($in_name, $in_value); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $result = $this->pointer->set_attribute($in_name, $in_value); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + + return $result; } // }}} @@ -627,15 +701,25 @@ * @param boolean $in_movePointer (optional) move internal pointer * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function removeAttribute($in_name, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } - $this->pointer->remove_attribute($in_name); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + $result = $this->pointer->remove_attribute($in_name); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + + return $result; } // }}} @@ -657,6 +741,10 @@ */ function substringData($in_offset = 0, $in_count = 0, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_TEXT_NODE, XML_ELEMENT_NODE, XML_CDATA_SECTION_NODE, XML_COMMENT_NODE)))) { return $result; } @@ -667,12 +755,15 @@ elseif (!is_int($in_offset) || $in_offset < 0 || $in_offset > strlen($content = $this->pointer->get_content())) { $return = PEAR::raiseError(null, XML_XPATH_INDEX_SIZE, null, E_USER_WARNING, "Offset: $in_offset", 'XML_XPath_Error', true); } - // if this is an element node, concat all the text children recursively else { $return = $in_count ? substr($content, $in_offset, $in_count) : substr($content, $in_offset); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $return; } @@ -766,16 +857,23 @@ * * @access public * @return object pointer to old node {or XML_XPath_Error exception} - * @todo I should make it so an xml_xpath_result is returned or something + * [!] I should make it so an xml_xpath_result is returned or something [!] */ function replaceChild($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_TEXT_NODE, XML_COMMENT_NODE, XML_CDATA_SECTION_NODE, XML_PI_NODE)))) { return $result; } if (XML_XPath::isError($importedNodes = $this->_build_fragment($in_xmlData))) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $importedNodes; } @@ -791,7 +889,9 @@ } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $oldNode; } @@ -812,12 +912,20 @@ */ function appendChild($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_DOCUMENT_NODE)))) { return $result; } + // if this is a document node, make sure no root exists - if ($this->isNodeType(XML_DOCUMENT_NODE) && $this->xml->document_element()) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($this->pointer->node_type() == XML_DOCUMENT_NODE && $this->xml->document_element()) { + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return PEAR::raiseError(null, XML_DUPLICATE_ROOT, null, E_USER_WARNING, null, 'XML_XPath_Error', true); } @@ -830,7 +938,11 @@ $newNode = $node; } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $newNode; } @@ -849,6 +961,10 @@ */ function insertBefore($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } @@ -867,7 +983,9 @@ } } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $newNode; } @@ -889,6 +1007,10 @@ */ function removeChild($in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE, XML_TEXT_NODE, XML_COMMENT_NODE, XML_PI_NODE, XML_CDATA_SECTION_NODE)))) { return $result; } @@ -900,7 +1022,9 @@ // set pointer to the parent $this->pointer = $parent; - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $removedNode; } @@ -923,12 +1047,19 @@ */ function replaceChildren($in_xmlData, $in_xpathQuery = null, $in_movePointer = false) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } if (XML_XPath::isError($importedNodes = $this->_build_fragment($in_xmlData))) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return $importedNodes; } @@ -946,7 +1077,9 @@ $this->pointer->append_child($importedNode); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } } // }}} @@ -963,6 +1096,10 @@ */ function dumpChildren($in_xpathQuery = null, $in_movePointer = false, $in_format = true) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_ELEMENT_NODE)))) { return $result; } @@ -970,7 +1107,9 @@ $xmlString = trim($this->xml->dump_node($this->pointer, $in_format)); $xmlString = substr($xmlString,strpos($xmlString,'>')+1,-(strlen($this->nodeName())+3)); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $xmlString; } @@ -1024,7 +1163,11 @@ */ function toString($in_xpathQuery = null, $in_movePointer = false, $in_format = true) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + + if ($hasQuery = !is_null($in_xpathQuery) && XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { return $result; } @@ -1040,7 +1183,9 @@ $xmlString = `echo $xmlString | {$this->xmllint} --format - 2>&1`; }*/ - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if ($hasQuery && !$in_movePointer) { + $this->_restore_bookmark(); + } return $xmlString; } @@ -1097,16 +1242,18 @@ * * @return string xpath location query * @access public - * [?] maybe add caching [?] */ - function getNodePath($in_xpathQuery = null, $in_movePointer = false) + function getNodePath($in_node) { - if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer))) { - return $result; + if (!$in_node) { + return null; + } + elseif (!$this->_is_dom_node($in_node)) { + return PEAR::raiseError(null, XML_XPATH_NODE_REQUIRED, null, E_USER_WARNING, $in_node, 'XML_XPath_Error', true); } $buffer = ''; - $cur = $this->pointer; + $cur = $in_node; do { $name = ''; $sep = '/'; @@ -1116,7 +1263,7 @@ break; } - $next = NULL; + $next = false; } else if ($type == XML_ATTRIBUTE_NODE) { $sep .= '@'; @@ -1129,7 +1276,7 @@ // now figure out the index $tmp = $cur->previous_sibling(); - while ($tmp != NULL) { + while ($tmp != false) { if ($name == $tmp->node_name()) { $occur++; } @@ -1167,10 +1314,8 @@ $cur = $next; - } while ($cur != NULL); + } while ($cur != false); - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); - return $buffer; } @@ -1219,6 +1364,10 @@ */ function _set_content($in_content, $in_xpathQuery, $in_movePointer, $in_replace, $in_offset = 0, $in_count = 0) { + if (!$this->pointer) { + return PEAR::raiseError(null, XML_XPATH_NULL_POINTER, null, E_USER_WARNING, '', 'XML_XPath_Error', true); + } + if (XML_XPath::isError($result = $this->_quick_evaluate_init($in_xpathQuery, $in_movePointer, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE, XML_COMMENT_NODE, XML_PI_NODE)))) { return $result; } @@ -1242,7 +1391,9 @@ $this->pointer->set_content($data); } - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } return $return; } @@ -1278,21 +1429,6 @@ } // }}} - // {{{ _set_bookmark() - - /** - * Set a temporary bookmark of the internal pointer while doing a quick xpath query - * - * @access private - * @return void - */ - function _set_bookmark($in_location) - { - $this->bookmark = $this->pointer; - $this->pointer = $in_location; - } - - // }}} // {{{ _quick_evaluate_init() /** @@ -1310,62 +1446,80 @@ */ function _quick_evaluate_init($in_xpathQuery = null, $in_movePointer = false, $in_nodeTypes = null) { - if (!is_null($in_xpathQuery)) { - if ($this->_is_dom_node($in_xpathQuery)) { + // we don't need to check for null, since false or 0 is not a valid query anyway + if ($in_xpathQuery) { + if (!is_object($in_xpathQuery)) { + // doing the following manually (without evaluate()) is critical for speed + + // Make sure we have an xpath context (mildly costly) + if (get_class($this->ctx) != 'XPathContext') { + return PEAR::raiseError(null, XML_XPATH_NOT_LOADED, null, E_USER_ERROR, null, 'XML_XPath_Error', true); + } + + // enable relative xpath queries (I don't check a valid dom object yet) + settype($in_xpathQuery, 'array'); + if (isset($in_xpathQuery[1])) { + $sep = '/'; + // those double slashes cause an anomally + if (substr($in_xpathQuery[0], 0, 2) == '//') { + $sep = ''; + } + + if ($in_xpathQuery[1] == 'current()') { + $in_xpathQuery[1] = $this->getNodePath($this->pointer); + } + else { + $in_xpathQuery[1] = $this->getNodePath($in_xpathQuery[1]); + } + + $xpathQuery = $in_xpathQuery[1] . $sep . $in_xpathQuery[0]; + } + else { + $xpathQuery = reset($in_xpathQuery); + } + + if (!$result = @xpath_eval($this->ctx, $xpathQuery)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); + } + + if (empty($result->nodeset) || $result->type != XPATH_NODESET) { + return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_WARNING, "XML_XPath query: $xpathQuery", 'XML_XPath_Error', true); + } + + // this takes the first result (too bad if you had more) + $tmpPointer = reset($result->nodeset); + } + // a bit costly, so we put it second + elseif ($this->_is_dom_node($in_xpathQuery)) { $tmpPointer = $in_xpathQuery; } else { - if (XML_XPath::isError($resultObj = $this->evaluate($in_xpathQuery))) { - return $resultObj; - } - // make sure we have at least one node - if (!($resultObj->numResults() && $resultObj->resultType() == XPATH_NODESET)) { - return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); - } - $resultObj->nextNode(); - $tmpPointer = $resultObj->getPointer(); + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_WARNING, "XML_XPath query: $in_xpathQuery", 'XML_XPath_Error', true); } + // if we are moving the internal pointer, then do it now if ($in_movePointer) { $this->pointer = $tmpPointer; } // set the bookmark if we only want to temporarily move the pointer + // this area is too critical to call class methods...we have to be nasty else { - $this->_set_bookmark($tmpPointer); + $this->bookmark = $this->pointer; + $this->pointer = $tmpPointer; } } - // see if we have a restricted nodeType requirement + + // see if we have a restricted nodeType requirement (negligible time) if (is_array($in_nodeTypes) && !in_array($nodeType = $this->pointer->node_type(), $in_nodeTypes)) { - $this->_quick_evaluate_shutdown($in_xpathQuery, $in_movePointer); + if (!is_null($in_xpathQuery) && !$in_movePointer) { + $this->_restore_bookmark(); + } + return PEAR::raiseError(null, XML_XPATH_INVALID_NODETYPE, null, E_USER_WARNING, "Required type: ".implode(" or ", $in_nodeTypes).", Provided type: ".$nodeType, 'XML_XPath_Error', true); } else { return true; } - } - - // }}} - // {{{ _quick_evaluate_shutdown() - - /** - * Restore the internal pointer if there was no request to permanently move it with the - * quick query. This should never return error because it is only called if the init was - * successful. - * - * @param string $in_xpathQuery options quick xpath query to execute - * @param boolean $in_movePointer allow quick query to move the internal pointer - * - * @access private - * @return boolean always true - */ - function _quick_evaluate_shutdown($in_xpathQuery = null, $in_movePointer = false) - { - // if we did quick xpath query and didn't want to move - // our internal pointer, then reset it - if (!is_null($in_xpathQuery) && !$in_movePointer) { - $this->_restore_bookmark(); - } - return true; } // }}} Index: pear/XML_XPath/XPath/result.php diff -u pear/XML_XPath/XPath/result.php:1.9 pear/XML_XPath/XPath/result.php:1.10 --- pear/XML_XPath/XPath/result.php:1.9 Thu Jun 6 20:13:15 2002 +++ pear/XML_XPath/XPath/result.php Fri Jun 7 19:17:21 2002 @@ -17,7 +17,7 @@ // | Authors: Dan Allen <dan@mojavelinux.com> | // +----------------------------------------------------------------------+ -// $Id: result.php,v 1.9 2002/06/07 00:13:15 dallen Exp $ +// $Id: result.php,v 1.10 2002/06/07 23:17:21 dallen Exp $ // }}} // {{{ description @@ -56,41 +56,44 @@ class XML_XPath_result extends XML_XPath_common { // {{{ properties - /** @var object reference to result object */ - var $result; - - /** @var string original xpath query, stored just for debug info */ + /** + * original xpath query, stored when we need to sort + * @var string $query + */ var $query; - /** @var int current index of the result set */ - var $index; - - /** @var boolean determines if we have counted the first node */ - var $rewound; + /** + * determines if we have counted the first node of the result nodeset + * @var boolean $isRewound + */ + var $isRewound; - /** @var int one of 4 constants that correspond to the xpath result types */ + /** + * The type of result that the query generated + * @var int $type + */ var $type; - /** @var mixed either array of nodesets, string, boolean or number from xpath result */ + /** + * either array of nodesets, string, boolean or number from xpath/DOM query + * @var mixed $data + */ var $data; - /** @var object pointer to xml data object */ - var $xml; - - /** @var object pointer to xpath context object for the xml data object */ + /** + * xpath context object for the current domxml object + * @var object $ctx + */ var $ctx; // }}} // {{{ constructor - function XML_XPath_result($in_result, $in_query, &$in_xml, &$in_ctx) + function XML_XPath_result($in_data, $in_type, $in_query, &$in_ctx) { - $this->result = &$in_result; $this->query = $in_query; - $this->type = $this->result->type; - $this->data = isset($this->result->nodeset) ? $this->result->nodeset : $this->result->value; - $this->index = 0; - $this->xml = &$in_xml; + $this->type = $in_type; + $this->data = $in_data; $this->ctx = &$in_ctx; // move the pointer to the first node if at least one node in the result exists // for convience, just so we don't have to call nextNode() if we expect only one @@ -114,7 +117,7 @@ return $this->data ? true : false; break; case XPATH_NODESET: - return $this->isNodeType(XML_ATTRIBUTE_NODE) ? $this->pointer->value() : $this->substringData(); + return $this->pointer->node_type() == XML_ATTRIBUTE_NODE ? $this->pointer->value() : $this->substringData(); break; case XPATH_STRING: case XPATH_NUMBER: @@ -166,15 +169,17 @@ } // }}} - // {{{ void sort() + // {{{ boolean sort() /** * Sort the nodeset in this result. The sort can be either ascending or descending, and * the comparisons can be text, number or natural (see the constants above). The sort * axis is provided as an xpath query and is the location path relative to the node given. * For example, so sort on an attribute, you would provide '@foo' and it will look at the - * attribute for each node. If the axis is not found, it comes first in the sort order for - * ascending order. + * attribute for each node. + * + * NOTE: If the axis is not found, the node will comes first in the sort order for ascending + * order and at the end for descending orde. * * @param string $in_sortXpath relative xpath query location to each node in nodeset * @param int $in_order either XML_XPATH_SORT_TEXT_[DE|A]SCENDING, @@ -182,43 +187,73 @@ * XML_XPATH_SORT_NATURAL_[DE|A]SCENDING * * @access public - * @return void {or XML_XPath_Error exception} + * @return boolean success {or XML_XPath_Error exception} */ function sort($in_sortXpath = '.', $in_order = XML_XPATH_SORT_TEXT_ASCENDING) { - if ($this->resultType() != XPATH_NODESET) { + // make sure we are dealing with a result that is a nodeset + if ($this->type != XPATH_NODESET) { return PEAR::raiseError(null, XML_XPATH_INVALID_NODESET, null, E_USER_NOTICE, $this->data, 'XPath_Error', true); } - if ($in_sortXpath == '') { - $in_sortXpath = '.'; - } - $xpathResult = @$this->ctx->xpath_eval($this->query . '/' . $in_sortXpath . '|' . $this->query); - if (!$xpathResult || !$xpathResult->nodeset) { - return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_NOTICE, "Query {$this->query}/$in_sortXPath", 'XML_XPath_Error', true); - } + $data = array(); - $this->index = 0; - if (sizeof($xpathResult->nodeset) == sizeof($this->data)) { - foreach ($xpathResult->nodeset as $index => $node) { - array_push($data, $xpathResult->nodeset[$index]->get_content()); + + // we don't need to run it again if we are soring on the current node values + if ($in_sortXpath == '' || $in_sortXpath == '.') { + foreach ($this->data as $index => $node) { + $data[] = $node->get_content(); } } + // we need to run the query again else { - foreach ($xpathResult->nodeset as $index => $node) { - if (!isset($this->data[$this->index]) || $node != $this->data[$this->index]) { - continue; - } + // we never actually ran the query, but we can rebuild it...and we will do that now + // this is for DOM queries, such as childNodes() and getElementsByTagName() + if (is_array($this->query)) { + $this->query = $this->getNodePath(reset($this->query)) . end($this->query); + } + + // here I am reissuing the query, but with the sort path appended followed by the + // node in a logical 'OR'. The trick here is that I can keep the original nodes + // in sorted order and then just weed out the nodes I used to sort. + $xpathResult = @$this->ctx->xpath_eval($this->query . '/' . $in_sortXpath . '|' . $this->query); + if (!$xpathResult || empty($xpathResult->nodeset)) { + return PEAR::raiseError(null, XML_XPATH_INVALID_QUERY, null, E_USER_NOTICE, "Query {$this->query}/$in_sortXPath", 'XML_XPath_Error', true); + } - if (isset($xpathResult->nodeset[$index + 1])) { - if ($xpathResult->nodeset[$index + 1] == $this->data[$this->index]) { - array_push($data, ''); + // Sorting Process: + // The reason we did a double query is so that we could line up the original nodes + // with the original data set, fill in any parts of the sorted nodeset that have + // missing sort nodes, sort the sorted nodeset and then reindex the original data array + + $origIndex = 0; + $sortIndex = 0; + while(isset($this->data[$origIndex])) { + // make sure we are lined up on original nodes, then we can proceed to check + // the next node in each nodeset to determine of the sort node was found + if ($this->data[$origIndex] == $xpathResult->nodeset[$sortIndex]) { + $origIndex++; + $sortIndex++; + // make sure we have not advanced beyond the end of the sort nodeset + if (isset($xpathResult->nodeset[$sortIndex])) { + // if the values of the next two indices of the sort nodeset and the + // original nodeset are the same, we had a missing node + if (isset($this->data[$origIndex]) && $this->data[$origIndex] == $xpathResult->nodeset[$sortIndex]) { + $data[] = ''; + } + // okay, they were different, we found a sort node, get its value + else { + $data[] = $xpathResult->nodeset[$sortIndex]->get_content(); + } } + // the last sort nodeset element is missing, which means the sort nodeset + // was missing the last sort node else { - array_push($data, $xpathResult->nodeset[$index + 1]->get_content()); + $data[] = ''; } } - - $this->index++; + else { + $sortIndex++; + } } } @@ -245,21 +280,26 @@ case XML_XPATH_SORT_NATURAL_DESCENDING: natsort($data); - $data = array_reverse($data, TRUE); + $data = array_reverse($data, true); break; default: asort($data); break; } + $dataReordered = array(); - $this->index = 0; + // this is NOT just array_values, we need to use the keys to put the values + // in the correct order foreach ($data as $reindex => $value) { - $dataReordered[$this->index++] = $this->data[$reindex]; + $dataReordered[] = $this->data[$reindex]; } + $this->data = $dataReordered; - $this->index = 0; - $this->pointer = $this->data[$this->index]; + // rewind to the beginning of the data set + $this->rewind(); + + return true; } // }}} @@ -275,7 +315,7 @@ { if (is_array($this->data)) { $this->pointer = reset($this->data); - $this->rewound = true; + $this->isRewound = true; return true; } @@ -299,8 +339,8 @@ function next() { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; $seekFunction = 'reset'; } else { @@ -332,8 +372,8 @@ function nextByNodeName($in_name) { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; if (($node = reset($this->data)) && $node->node_name() == $in_name) { $this->pointer = $node; return true; @@ -367,8 +407,8 @@ function nextByNodeType($in_type) { if (is_array($this->data)) { - if ($this->rewound) { - $this->rewound = false; + if ($this->isRewound) { + $this->isRewound = false; if (($node = reset($this->data)) && $node->node_type() == $in_type) { $this->pointer = $node; return true;