Class DocumentNavigator
- java.lang.Object
-
- org.jaxen.DefaultNavigator
-
- org.jaxen.dom.DocumentNavigator
-
- All Implemented Interfaces:
java.io.Serializable,Navigator
public class DocumentNavigator extends DefaultNavigator
Interface for navigating around the W3C DOM Level 2 object model.This class is not intended for direct usage, but is used by the Jaxen engine during evaluation.
This class implements the
DefaultNavigatorinterface for the Jaxen XPath library. This adapter allows the Jaxen library to be used to execute XPath queries against any object tree that implements the DOM level 2 interfaces.Note: DOM level 2 does not include a node representing an XPath namespace node. This navigator will return namespace nodes as instances of the custom
NamespaceNodeclass, and users will have to check result sets to locate and isolate these.- Author:
- David Megginson, James Strachan
- See Also:
XPath,NamespaceNode, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DocumentNavigator()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.IteratorgetAttributeAxisIterator(java.lang.Object contextNode)Get an iterator over all attributes.java.lang.StringgetAttributeName(java.lang.Object attribute)Get the local name of an attribute.java.lang.StringgetAttributeNamespaceUri(java.lang.Object attribute)Get the namespace URI of an attribute.java.lang.StringgetAttributeQName(java.lang.Object attribute)Get the qualified name of an attribute.java.lang.StringgetAttributeStringValue(java.lang.Object object)Get the string value of an attribute node.java.util.IteratorgetChildAxisIterator(java.lang.Object contextNode)Get an iterator over all of this node's children.java.lang.StringgetCommentStringValue(java.lang.Object object)Get the string value of a comment node.java.lang.ObjectgetDocument(java.lang.String uri)Use JAXP to load a namespace aware document from a given URI.java.lang.ObjectgetDocumentNode(java.lang.Object contextNode)Get the top-level document node.java.lang.ObjectgetElementById(java.lang.Object object, java.lang.String elementId)Returns the element whose ID is given by elementId.java.lang.StringgetElementName(java.lang.Object element)Get the local name of an element.java.lang.StringgetElementNamespaceUri(java.lang.Object element)Get the namespace URI of an element.java.lang.StringgetElementQName(java.lang.Object element)Get the qualified name of an element.java.lang.StringgetElementStringValue(java.lang.Object object)Get the string value of an element node.java.util.IteratorgetFollowingAxisIterator(java.lang.Object contextNode)Get an iterator over all following nodes, depth-first.java.util.IteratorgetFollowingSiblingAxisIterator(java.lang.Object contextNode)Get an iterator over all following siblings.static NavigatorgetInstance()Get a constant DocumentNavigator for efficiency.java.util.IteratorgetNamespaceAxisIterator(java.lang.Object contextNode)Get an iterator over all declared namespaces.java.lang.StringgetNamespacePrefix(java.lang.Object object)Get the prefix value of a namespace node.java.lang.StringgetNamespaceStringValue(java.lang.Object object)Get the string value of a namespace node.java.util.IteratorgetParentAxisIterator(java.lang.Object contextNode)Get a (single-member) iterator over this node's parent.java.lang.ObjectgetParentNode(java.lang.Object child)Return the XPath parent of the supplied DOM node.java.util.IteratorgetPrecedingSiblingAxisIterator(java.lang.Object contextNode)Get an iterator over all preceding siblings.java.lang.StringgetProcessingInstructionData(java.lang.Object obj)Get the data of a processing instruction node.java.lang.StringgetProcessingInstructionTarget(java.lang.Object obj)Get the target of a processing instruction node.java.lang.StringgetTextStringValue(java.lang.Object object)Get the string value of text.booleanisAttribute(java.lang.Object object)Test if a node is an attribute.booleanisComment(java.lang.Object object)Test if a node is a comment.booleanisDocument(java.lang.Object object)Test if a node is a top-level document.booleanisElement(java.lang.Object object)Test if a node is an element.booleanisNamespace(java.lang.Object object)Test if a node is a namespace.booleanisProcessingInstruction(java.lang.Object object)Test if a node is a processing instruction.booleanisText(java.lang.Object object)Test if a node is plain text.XPathparseXPath(java.lang.String xpath)Returns a parsed form of the given XPath string, which will be suitable for queries on DOM documents.java.lang.StringtranslateNamespacePrefixToUri(java.lang.String prefix, java.lang.Object element)Translate a namespace prefix to a URI.-
Methods inherited from class org.jaxen.DefaultNavigator
getAncestorAxisIterator, getAncestorOrSelfAxisIterator, getDescendantAxisIterator, getDescendantOrSelfAxisIterator, getNodeType, getPrecedingAxisIterator, getSelfAxisIterator
-
-
-
-
Method Detail
-
getInstance
public static Navigator getInstance()
Get a constant DocumentNavigator for efficiency.- Returns:
- a constant instance of a DocumentNavigator.
-
getChildAxisIterator
public java.util.Iterator getChildAxisIterator(java.lang.Object contextNode)
Get an iterator over all of this node's children.- Specified by:
getChildAxisIteratorin interfaceNavigator- Overrides:
getChildAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the child axis.- Returns:
- a possibly-empty iterator (not null)
-
getParentAxisIterator
public java.util.Iterator getParentAxisIterator(java.lang.Object contextNode)
Get a (single-member) iterator over this node's parent.- Specified by:
getParentAxisIteratorin interfaceNavigator- Overrides:
getParentAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the parent axis- Returns:
- a possibly-empty iterator (not null)
-
getParentNode
public java.lang.Object getParentNode(java.lang.Object child)
Return the XPath parent of the supplied DOM node. XPath has slightly different definition of parent than DOM does. In particular, the parent of an attribute is not null.- Specified by:
getParentNodein interfaceNavigator- Overrides:
getParentNodein classDefaultNavigator- Parameters:
child- the child node- Returns:
- the parent of the specified node; or null if the node does not have a parent
- See Also:
Navigator.isDocument(java.lang.Object),Navigator.isElement(java.lang.Object)
-
getFollowingSiblingAxisIterator
public java.util.Iterator getFollowingSiblingAxisIterator(java.lang.Object contextNode)
Get an iterator over all following siblings.- Specified by:
getFollowingSiblingAxisIteratorin interfaceNavigator- Overrides:
getFollowingSiblingAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the sibling iterator- Returns:
- a possibly-empty iterator (not null)
-
getPrecedingSiblingAxisIterator
public java.util.Iterator getPrecedingSiblingAxisIterator(java.lang.Object contextNode)
Get an iterator over all preceding siblings.- Specified by:
getPrecedingSiblingAxisIteratorin interfaceNavigator- Overrides:
getPrecedingSiblingAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the preceding sibling axis- Returns:
- a possibly-empty iterator (not null)
-
getFollowingAxisIterator
public java.util.Iterator getFollowingAxisIterator(java.lang.Object contextNode)
Get an iterator over all following nodes, depth-first.- Specified by:
getFollowingAxisIteratorin interfaceNavigator- Overrides:
getFollowingAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the following axis- Returns:
- a possibly-empty iterator (not null)
-
getAttributeAxisIterator
public java.util.Iterator getAttributeAxisIterator(java.lang.Object contextNode)
Get an iterator over all attributes.- Specified by:
getAttributeAxisIteratorin interfaceNavigator- Overrides:
getAttributeAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the attribute axis- Returns:
- a possibly-empty iterator (not null)
-
getNamespaceAxisIterator
public java.util.Iterator getNamespaceAxisIterator(java.lang.Object contextNode)
Get an iterator over all declared namespaces.Note: this iterator is not live: it takes a snapshot and that snapshot remains static during the life of the iterator (i.e. it won't reflect subsequent changes to the DOM).
In the event that the DOM is inconsistent; for instance a
pre:fooelement is declared by DOM to be in the http://www.a.com/ namespace but also has anxmlns:pre="http://www.b.com"attribute; then only one of the namespaces will be counted. This will be the intrinsic namespace of theElementorAttrobject rather than the one provide by the contradictory namespace declaration attribute. In the event of a contradiction between two attributes on the same element--e.g.pre:fooin the http://www.a.com/ namespace andpre:barin the http://www.b.com/ namespace--it is undefined which namespace will be returned.- Specified by:
getNamespaceAxisIteratorin interfaceNavigator- Overrides:
getNamespaceAxisIteratorin classDefaultNavigator- Parameters:
contextNode- the context node for the namespace axis- Returns:
- a possibly-empty iterator (not null)
-
parseXPath
public XPath parseXPath(java.lang.String xpath) throws SAXPathException
Returns a parsed form of the given XPath string, which will be suitable for queries on DOM documents.- Parameters:
xpath- the XPath expression- Returns:
- a parsed form of the given XPath string
- Throws:
SAXPathException- if the string is syntactically incorrect- See Also:
XPath
-
getDocumentNode
public java.lang.Object getDocumentNode(java.lang.Object contextNode)
Get the top-level document node.- Specified by:
getDocumentNodein interfaceNavigator- Overrides:
getDocumentNodein classDefaultNavigator- Parameters:
contextNode- any node in the document- Returns:
- the root node
- See Also:
Navigator.isDocument(Object)
-
getElementNamespaceUri
public java.lang.String getElementNamespaceUri(java.lang.Object element)
Get the namespace URI of an element.- Parameters:
element- the target node- Returns:
- a string (possibly empty) if the node is an element, and null otherwise
-
getElementName
public java.lang.String getElementName(java.lang.Object element)
Get the local name of an element.- Parameters:
element- the target node- Returns:
- a string representing the unqualified local name if the node is an element, or null otherwise
-
getElementQName
public java.lang.String getElementQName(java.lang.Object element)
Get the qualified name of an element.- Parameters:
element- the target node- Returns:
- a string representing the qualified (i.e. possibly prefixed) name if the argument is an element, or null otherwise
-
getAttributeNamespaceUri
public java.lang.String getAttributeNamespaceUri(java.lang.Object attribute)
Get the namespace URI of an attribute.- Parameters:
attribute- the target node- Returns:
- the namespace name of the specified node
-
getAttributeName
public java.lang.String getAttributeName(java.lang.Object attribute)
Get the local name of an attribute.- Parameters:
attribute- the target node- Returns:
- a string representing the unqualified local name if the node is an attribute, or null otherwise
-
getAttributeQName
public java.lang.String getAttributeQName(java.lang.Object attribute)
Get the qualified name of an attribute.- Parameters:
attribute- the target node- Returns:
- a string representing the qualified (i.e. possibly prefixed) name if the argument is an attribute, or null otherwise
-
isDocument
public boolean isDocument(java.lang.Object object)
Test if a node is a top-level document.- Parameters:
object- the target node- Returns:
- true if the node is the document root, false otherwise
-
isNamespace
public boolean isNamespace(java.lang.Object object)
Test if a node is a namespace.- Parameters:
object- the target node- Returns:
- true if the node is a namespace, false otherwise
-
isElement
public boolean isElement(java.lang.Object object)
Test if a node is an element.- Parameters:
object- the target node- Returns:
- true if the node is an element, false otherwise
-
isAttribute
public boolean isAttribute(java.lang.Object object)
Test if a node is an attribute.xmlnsandxmlns:preattributes do not count as attributes for the purposes of XPath.- Parameters:
object- the target node- Returns:
- true if the node is an attribute, false otherwise
-
isComment
public boolean isComment(java.lang.Object object)
Test if a node is a comment.- Parameters:
object- the target node- Returns:
- true if the node is a comment, false otherwise
-
isText
public boolean isText(java.lang.Object object)
Test if a node is plain text.- Parameters:
object- the target node- Returns:
- true if the node is a text node, false otherwise
-
isProcessingInstruction
public boolean isProcessingInstruction(java.lang.Object object)
Test if a node is a processing instruction.- Parameters:
object- the target node- Returns:
- true if the node is a processing instruction, false otherwise
-
getElementStringValue
public java.lang.String getElementStringValue(java.lang.Object object)
Get the string value of an element node.- Parameters:
object- the target node- Returns:
- the text inside the node and its descendants if the node is an element, null otherwise
-
getAttributeStringValue
public java.lang.String getAttributeStringValue(java.lang.Object object)
Get the string value of an attribute node.- Parameters:
object- the target node- Returns:
- the text of the attribute value if the node is an attribute, null otherwise
-
getTextStringValue
public java.lang.String getTextStringValue(java.lang.Object object)
Get the string value of text.- Parameters:
object- the target node- Returns:
- the string of text if the node is text, null otherwise
-
getCommentStringValue
public java.lang.String getCommentStringValue(java.lang.Object object)
Get the string value of a comment node.- Parameters:
object- the target node- Returns:
- the text of the comment if the node is a comment, null otherwise
-
getNamespaceStringValue
public java.lang.String getNamespaceStringValue(java.lang.Object object)
Get the string value of a namespace node.- Parameters:
object- the target node- Returns:
- the namespace URI as a (possibly empty) string if the node is a namespace node, null otherwise
-
getNamespacePrefix
public java.lang.String getNamespacePrefix(java.lang.Object object)
Get the prefix value of a namespace node.- Parameters:
object- the target node- Returns:
- the namespace prefix a (possibly empty) string if the node is a namespace node, null otherwise
-
translateNamespacePrefixToUri
public java.lang.String translateNamespacePrefixToUri(java.lang.String prefix, java.lang.Object element)Translate a namespace prefix to a URI.- Specified by:
translateNamespacePrefixToUriin interfaceNavigator- Overrides:
translateNamespacePrefixToUriin classDefaultNavigator- Parameters:
prefix- the namespace prefixelement- the namespace context- Returns:
- the namespace URI bound to the prefix in the scope of
element; null if the prefix is not bound - See Also:
NamespaceContext
-
getDocument
public java.lang.Object getDocument(java.lang.String uri) throws FunctionCallExceptionUse JAXP to load a namespace aware document from a given URI.- Specified by:
getDocumentin interfaceNavigator- Overrides:
getDocumentin classDefaultNavigator- Parameters:
uri- the URI of the document to load- Returns:
- the new W3C DOM Level 2 Document instance
- Throws:
FunctionCallException- containing a nested exception if a problem occurs trying to parse the given document
-
getProcessingInstructionTarget
public java.lang.String getProcessingInstructionTarget(java.lang.Object obj)
Get the target of a processing instruction node.- Specified by:
getProcessingInstructionTargetin interfaceNavigator- Overrides:
getProcessingInstructionTargetin classDefaultNavigator- Parameters:
obj- the processing instruction- Returns:
- the target of the processing instruction
- Throws:
java.lang.ClassCastException- if obj is not a processing instruction
-
getProcessingInstructionData
public java.lang.String getProcessingInstructionData(java.lang.Object obj)
Get the data of a processing instruction node.- Specified by:
getProcessingInstructionDatain interfaceNavigator- Overrides:
getProcessingInstructionDatain classDefaultNavigator- Parameters:
obj- the processing instruction- Returns:
- the target of the processing instruction
- Throws:
java.lang.ClassCastException- if obj is not a processing instruction
-
getElementById
public java.lang.Object getElementById(java.lang.Object object, java.lang.String elementId)Returns the element whose ID is given by elementId. If no such element exists, returns null. Attributes with the name "ID" are not of type ID unless so defined. Attribute types are only known if when the parser understands DTD's or schemas that declare attributes of type ID. When JAXP is used, you must callsetValidating(true)on the DocumentBuilderFactory.- Specified by:
getElementByIdin interfaceNavigator- Overrides:
getElementByIdin classDefaultNavigator- Parameters:
object- a node from the document in which to look for the idelementId- id to look for- Returns:
- element whose ID is given by elementId, or null if no such element exists in the document or if the implementation does not know about attribute types
- Throws:
java.lang.ClassCastException- if object is not anorg.w3c.dom.Nodeobject- See Also:
DocumentBuilderFactory
-
-