org.jaxen.dom

Class DocumentNavigator

Implemented Interfaces:
Navigator, Serializable

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 DefaultNavigator interface 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 NamespaceNode class, and users will have to check result sets to locate and isolate these.

Authors:
David Megginson
James Strachan
See Also:
XPath, NamespaceNode

Nested Class Summary

private static class
DocumentNavigator.AttributeIterator
An iterator over an attribute list.
(package private) abstract class
DocumentNavigator.NodeIterator
A generic iterator over DOM nodes.

Field Summary

private static DocumentNavigator
SINGLETON
private static long
serialVersionUID

Constructor Summary

DocumentNavigator()
Default constructor.

Method Summary

Iterator
getAttributeAxisIterator(Object contextNode)
Get an iterator over all attributes.
String
getAttributeName(Object attribute)
Get the local name of an attribute.
String
getAttributeNamespaceUri(Object attribute)
Get the namespace URI of an attribute.
String
getAttributeQName(Object attribute)
Get the qualified name of an attribute.
String
getAttributeStringValue(Object object)
Get the string value of an attribute node.
Iterator
getChildAxisIterator(Object contextNode)
Get an iterator over all of this node's children.
String
getCommentStringValue(Object object)
Get the string value of a comment node.
Object
getDocument(String uri)
Use JAXP to load a namespace aware document from a given URI.
Object
getDocumentNode(Object contextNode)
Get the top-level document node.
Object
getElementById(Object object, String elementId)
Returns the element whose ID is given by elementId.
String
getElementName(Object element)
Get the local name of an element.
String
getElementNamespaceUri(Object element)
Get the namespace URI of an element.
String
getElementQName(Object element)
Get the qualified name of an element.
String
getElementStringValue(Object object)
Get the string value of an element node.
Iterator
getFollowingAxisIterator(Object contextNode)
Get an iterator over all following nodes, depth-first.
Iterator
getFollowingSiblingAxisIterator(Object contextNode)
Get an iterator over all following siblings.
static Navigator
getInstance()
Get a constant DocumentNavigator for efficiency.
Iterator
getNamespaceAxisIterator(Object contextNode)
Get an iterator over all declared namespaces.
String
getNamespacePrefix(Object object)
Get the prefix value of a namespace node.
String
getNamespaceStringValue(Object object)
Get the string value of a namespace node.
Iterator
getParentAxisIterator(Object contextNode)
Get a (single-member) iterator over this node's parent.
Object
getParentNode(Object o)
Return the XPath parent of this DOM node.
Iterator
getPrecedingSiblingAxisIterator(Object contextNode)
Get an iterator over all preceding siblings.
String
getProcessingInstructionData(Object obj)
Get the data of a processing instruction node.
String
getProcessingInstructionTarget(Object obj)
Get the target of a processing instruction node.
private StringBuffer
getStringValue(Node node, StringBuffer buffer)
Construct a node's string value recursively.
String
getTextStringValue(Object object)
Get the string value of text.
boolean
isAttribute(Object object)
Test if a node is an attribute.
boolean
isComment(Object object)
Test if a node is a comment.
boolean
isDocument(Object object)
Test if a node is a top-level document.
boolean
isElement(Object object)
Test if a node is an element.
boolean
isNamespace(Object object)
Test if a node is a namespace.
boolean
isProcessingInstruction(Object object)
Test if a node is a processing instruction.
boolean
isText(Object object)
Test if a node is plain text.
XPath
parseXPath(String xpath)
Returns a parsed form of the given XPath string, which will be suitable for queries on DOM documents.
String
translateNamespacePrefixToUri(String prefix, Object element)
Translate a namespace prefix to a URI.

Methods inherited from class org.jaxen.DefaultNavigator

getAncestorAxisIterator, getAncestorOrSelfAxisIterator, getAttributeAxisIterator, getChildAxisIterator, getDescendantAxisIterator, getDescendantOrSelfAxisIterator, getDocument, getDocumentNode, getElementById, getFollowingAxisIterator, getFollowingSiblingAxisIterator, getNamespaceAxisIterator, getNodeType, getParentAxisIterator, getParentNode, getPrecedingAxisIterator, getPrecedingSiblingAxisIterator, getProcessingInstructionData, getProcessingInstructionTarget, getSelfAxisIterator, translateNamespacePrefixToUri

Field Details

SINGLETON

private static final DocumentNavigator SINGLETON

serialVersionUID

private static final long serialVersionUID
Field Value:
8460943068889528115L

Constructor Details

DocumentNavigator

public DocumentNavigator()
Default constructor.

Method Details

getAttributeAxisIterator

public Iterator getAttributeAxisIterator(Object contextNode)
Get an iterator over all attributes.
Specified by:
getAttributeAxisIterator in interface Navigator
Overrides:
getAttributeAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the attribute axis
Returns:
a possibly-empty iterator (not null)

getAttributeName

public String getAttributeName(Object attribute)
Get the local name of an attribute.
Specified by:
getAttributeName in interface Navigator
Parameters:
attribute - the target node
Returns:
a string representing the unqualified local name if the node is an attribute, or null otherwise

getAttributeNamespaceUri

public String getAttributeNamespaceUri(Object attribute)
Get the namespace URI of an attribute.
Specified by:
getAttributeNamespaceUri in interface Navigator
Parameters:
attribute - the target node
Returns:
the namespace name of the specified node

getAttributeQName

public String getAttributeQName(Object attribute)
Get the qualified name of an attribute.
Specified by:
getAttributeQName in interface Navigator
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

getAttributeStringValue

public String getAttributeStringValue(Object object)
Get the string value of an attribute node.
Specified by:
getAttributeStringValue in interface Navigator
Parameters:
object - the target node
Returns:
the text of the attribute value if the node is an attribute, null otherwise

getChildAxisIterator

public Iterator getChildAxisIterator(Object contextNode)
Get an iterator over all of this node's children.
Specified by:
getChildAxisIterator in interface Navigator
Overrides:
getChildAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the child axis.
Returns:
a possibly-empty iterator (not null)

getCommentStringValue

public String getCommentStringValue(Object object)
Get the string value of a comment node.
Specified by:
getCommentStringValue in interface Navigator
Parameters:
object - the target node
Returns:
the text of the comment if the node is a comment, null otherwise

getDocument

public Object getDocument(String uri)
            throws FunctionCallException
Use JAXP to load a namespace aware document from a given URI.
Specified by:
getDocument in interface Navigator
Overrides:
getDocument in interface DefaultNavigator
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

getDocumentNode

public Object getDocumentNode(Object contextNode)
Get the top-level document node.
Specified by:
getDocumentNode in interface Navigator
Overrides:
getDocumentNode in interface DefaultNavigator
Parameters:
contextNode - any node in the document
Returns:
the root node

getElementById

public Object getElementById(Object object,
                             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 call setValidating(true) on the DocumentBuilderFactory.
Specified by:
getElementById in interface Navigator
Overrides:
getElementById in interface DefaultNavigator
Parameters:
object - a node from the document in which to look for the id
elementId - 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
See Also:
javax.xml.parsers.DocumentBuilderFactory

getElementName

public String getElementName(Object element)
Get the local name of an element.
Specified by:
getElementName in interface Navigator
Parameters:
element - the target node
Returns:
a string representing the unqualified local name if the node is an element, or null otherwise

getElementNamespaceUri

public String getElementNamespaceUri(Object element)
Get the namespace URI of an element.
Specified by:
getElementNamespaceUri in interface Navigator
Parameters:
element - the target node
Returns:
a string (possibly empty) if the node is an element, and null otherwise

getElementQName

public String getElementQName(Object element)
Get the qualified name of an element.
Specified by:
getElementQName in interface Navigator
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

getElementStringValue

public String getElementStringValue(Object object)
Get the string value of an element node.
Specified by:
getElementStringValue in interface Navigator
Parameters:
object - the target node
Returns:
the text inside the node and its descendants if the node is an element, null otherwise

getFollowingAxisIterator

public Iterator getFollowingAxisIterator(Object contextNode)
Get an iterator over all following nodes, depth-first.
Specified by:
getFollowingAxisIterator in interface Navigator
Overrides:
getFollowingAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the following axis
Returns:
a possibly-empty iterator (not null)

getFollowingSiblingAxisIterator

public Iterator getFollowingSiblingAxisIterator(Object contextNode)
Get an iterator over all following siblings.
Specified by:
getFollowingSiblingAxisIterator in interface Navigator
Overrides:
getFollowingSiblingAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the sibling iterator
Returns:
a possibly-empty iterator (not null)

getInstance

public static Navigator getInstance()
Get a constant DocumentNavigator for efficiency.
Returns:
a constant instance of a DocumentNavigator.

getNamespaceAxisIterator

public Iterator getNamespaceAxisIterator(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:foo element is declared by DOM to be in the http://www.a.com/ namespace but also has an xmlns:pre="http://www.b.com" attribute; then only one of the namespaces will be counted. This will be the intrinsic namespace of the Element or Attr object 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:foo in the http://www.a.com/ namespace and pre:bar in the http://www.b.com/ namespace--it is undefined which namespace will be returned.

Specified by:
getNamespaceAxisIterator in interface Navigator
Overrides:
getNamespaceAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the namespace axis
Returns:
a possibly-empty iterator (not null)

getNamespacePrefix

public String getNamespacePrefix(Object object)
Get the prefix value of a namespace node.
Specified by:
getNamespacePrefix in interface Navigator
Parameters:
object - the target node
Returns:
the namespace prefix a (possibly empty) string if the node is a namespace node, null otherwise

getNamespaceStringValue

public String getNamespaceStringValue(Object object)
Get the string value of a namespace node.
Specified by:
getNamespaceStringValue in interface Navigator
Parameters:
object - the target node
Returns:
the namespace URI as a (possibly empty) string if the node is a namespace node, null otherwise

getParentAxisIterator

public Iterator getParentAxisIterator(Object contextNode)
Get a (single-member) iterator over this node's parent.
Specified by:
getParentAxisIterator in interface Navigator
Overrides:
getParentAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the parent axis
Returns:
a possibly-empty iterator (not null)

getParentNode

public Object getParentNode(Object o)
Return the XPath parent of this DOM node. XPath has slightly different definition of parent than DOM does. In particular, the parent of an attribute is not null.
Specified by:
getParentNode in interface Navigator
Overrides:
getParentNode in interface DefaultNavigator
Parameters:
o -
Returns:
the parent of the specified node; or null if the node does not have a parent

getPrecedingSiblingAxisIterator

public Iterator getPrecedingSiblingAxisIterator(Object contextNode)
Get an iterator over all preceding siblings.
Specified by:
getPrecedingSiblingAxisIterator in interface Navigator
Overrides:
getPrecedingSiblingAxisIterator in interface DefaultNavigator
Parameters:
contextNode - the context node for the preceding sibling axis
Returns:
a possibly-empty iterator (not null)

getProcessingInstructionData

public String getProcessingInstructionData(Object obj)
Get the data of a processing instruction node.
Specified by:
getProcessingInstructionData in interface Navigator
Overrides:
getProcessingInstructionData in interface DefaultNavigator
Parameters:
obj - the processing instruction
Returns:
the target of the processing instruction

getProcessingInstructionTarget

public String getProcessingInstructionTarget(Object obj)
Get the target of a processing instruction node.
Specified by:
getProcessingInstructionTarget in interface Navigator
Overrides:
getProcessingInstructionTarget in interface DefaultNavigator
Parameters:
obj - the processing instruction
Returns:
the target of the processing instruction

getStringValue

private StringBuffer getStringValue(Node node,
                                    StringBuffer buffer)
Construct a node's string value recursively.
Parameters:
node - the current node
buffer - the buffer for building the text
Returns:
the buffer passed as a parameter (for convenience)

getTextStringValue

public String getTextStringValue(Object object)
Get the string value of text.
Specified by:
getTextStringValue in interface Navigator
Parameters:
object - the target node
Returns:
the string of text if the node is text, null otherwise

isAttribute

public boolean isAttribute(Object object)
Test if a node is an attribute. xmlns and xmlns:pre attributes do not count as attributes for the purposes of XPath.
Specified by:
isAttribute in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is an attribute, false otherwise

isComment

public boolean isComment(Object object)
Test if a node is a comment.
Specified by:
isComment in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is a comment, false otherwise

isDocument

public boolean isDocument(Object object)
Test if a node is a top-level document.
Specified by:
isDocument in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is the document root, false otherwise

isElement

public boolean isElement(Object object)
Test if a node is an element.
Specified by:
isElement in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is an element, false otherwise

isNamespace

public boolean isNamespace(Object object)
Test if a node is a namespace.
Specified by:
isNamespace in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is a namespace, false otherwise

isProcessingInstruction

public boolean isProcessingInstruction(Object object)
Test if a node is a processing instruction.
Specified by:
isProcessingInstruction in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is a processing instruction, false otherwise

isText

public boolean isText(Object object)
Test if a node is plain text.
Specified by:
isText in interface Navigator
Parameters:
object - the target node
Returns:
true if the node is a text node, false otherwise

parseXPath

public XPath parseXPath(String xpath)
            throws SAXPathException
Returns a parsed form of the given XPath string, which will be suitable for queries on DOM documents.
Specified by:
parseXPath in interface Navigator
Parameters:
xpath - the XPath expression
Returns:
a parsed form of the given XPath string
Throws:
SAXPathException - if the string is syntactically incorrect

translateNamespacePrefixToUri

public String translateNamespacePrefixToUri(String prefix,
                                            Object element)
Translate a namespace prefix to a URI.
Specified by:
translateNamespacePrefixToUri in interface Navigator
Overrides:
translateNamespacePrefixToUri in interface DefaultNavigator
Parameters:
prefix - the namespace prefix
element - the namespace context
Returns:
the namespace URI bound to the prefix in the scope of element; null if the prefix is not bound