Class NodePointer
- java.lang.Object
-
- org.apache.commons.jxpath.ri.model.NodePointer
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Comparable
,Pointer
- Direct Known Subclasses:
CollectionPointer
,ContainerPointer
,DOMAttributePointer
,DOMNodePointer
,JDOMAttributePointer
,JDOMNamespacePointer
,JDOMNodePointer
,LangAttributePointer
,NamespacePointer
,PropertyOwnerPointer
,PropertyPointer
,VariablePointer
public abstract class NodePointer extends java.lang.Object implements Pointer
Common superclass for Pointers of all kinds. A NodePointer maps to a deterministic XPath that represents the location of a node in an object graph. This XPath uses only simple axes: child, namespace and attribute and only simple, context-independent predicates.- Version:
- $Revision: 668329 $ $Date: 2008-06-16 16:59:48 -0500 (Mon, 16 Jun 2008) $
- Author:
- Dmitri Plotnikov
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
attribute
protected int
index
Index for this NodePointerprotected java.util.Locale
locale
Localeprivate NamespaceResolver
namespaceResolver
protected NodePointer
parent
Parent pointerprivate java.lang.Object
rootNode
static java.lang.String
UNKNOWN_NAMESPACE
Constant to indicate unknown namespacestatic int
WHOLE_COLLECTION
Whole collection index.
-
Constructor Summary
Constructors Modifier Constructor Description protected
NodePointer(NodePointer parent)
Create a new NodePointer.protected
NodePointer(NodePointer parent, java.util.Locale locale)
Create a new NodePointer.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.String
asPath()
Returns an XPath that maps to this Pointer.NodeIterator
attributeIterator(QName qname)
Returns a NodeIterator that iterates over all attributes of the current node matching the supplied node name (could have a wildcard).NodeIterator
childIterator(NodeTest test, boolean reverse, NodePointer startWith)
Returns a NodeIterator that iterates over all children or all children that match the given NodeTest, starting with the specified one.java.lang.Object
clone()
Clone this NodePointer.abstract int
compareChildNodePointers(NodePointer pointer1, NodePointer pointer2)
Compares two child NodePointers and returns a positive number, zero or a positive number according to the order of the pointers.private int
compareNodePointers(NodePointer p1, int depth1, NodePointer p2, int depth2)
Compare node pointers.int
compareTo(java.lang.Object object)
NodePointer
createAttribute(JXPathContext context, QName name)
Called to create a non-existing attributeNodePointer
createChild(JXPathContext context, QName name, int index)
Called by a child pointer when it needs to create a parent object for a non-existent collection element.NodePointer
createChild(JXPathContext context, QName name, int index, java.lang.Object value)
Called by a child pointer if that child needs to assign the value supplied in the createPath(context, value) call to a non-existent node.NodePointer
createPath(JXPathContext context)
Called by a child pointer when it needs to create a parent object.NodePointer
createPath(JXPathContext context, java.lang.Object value)
Called directly by JXPathContext.private static boolean
equalStrings(java.lang.String s1, java.lang.String s2)
Compare two strings, either of which may be null, for equality.protected java.lang.String
escape(java.lang.String string)
Return a string escaping single and double quotes.protected AbstractFactory
getAbstractFactory(JXPathContext context)
Get the AbstractFactory associated with the specified JXPathContext.abstract java.lang.Object
getBaseValue()
Returns the value represented by the pointer before indexing.protected java.lang.String
getDefaultNamespaceURI()
Get the default ns uriabstract java.lang.Object
getImmediateNode()
Returns the object the pointer points to; does not convert it to a "canonical" type.NodePointer
getImmediateParentPointer()
Get the immediate parent pointer.NodePointer
getImmediateValuePointer()
int
getIndex()
If the pointer represents a collection, the index identifies an element of that collection.abstract int
getLength()
If the pointer represents a collection (or collection element), returns the length of the collection.java.util.Locale
getLocale()
If the Pointer has a parent, returns the parent's locale; otherwise returns the locale specified when this Pointer was created.abstract QName
getName()
Returns the name of this node.NamespaceResolver
getNamespaceResolver()
Get the NamespaceResolver associated with this NodePointer.java.lang.String
getNamespaceURI()
Returns the namespace URI associated with this Pointer.java.lang.String
getNamespaceURI(java.lang.String prefix)
Decodes a namespace prefix to the corresponding URI.java.lang.Object
getNode()
Returns the object the pointer points to; does not convert it to a "canonical" type.NodeSet
getNodeSetByKey(JXPathContext context, java.lang.String key, java.lang.Object value)
Find a NodeSet by key/value.java.lang.Object
getNodeValue()
Deprecated.1.1 Please use getNode()NodePointer
getParent()
Get the parent pointer.Pointer
getPointerByID(JXPathContext context, java.lang.String id)
Locates a node by ID.Pointer
getPointerByKey(JXPathContext context, java.lang.String key, java.lang.String value)
Locates a node by key and value.java.lang.Object
getRootNode()
Get the root node.java.lang.Object
getValue()
By default, returnsgetNode()
, can be overridden to return a "canonical" value, like for instance a DOM element should return its string value.NodePointer
getValuePointer()
If this pointer manages a transparent container, like a variable, this method returns the pointer to the contents.boolean
isActual()
An actual pointer points to an existing part of an object graph, even if it is null.boolean
isAttribute()
Returns true if the pointer represents the "attribute::" axis.abstract boolean
isCollection()
Returnstrue
if the value of the pointer is an array or a Collection.boolean
isContainer()
If true, this node is auxiliary and can only be used as an intermediate in the chain of pointers.protected boolean
isDefaultNamespace(java.lang.String prefix)
Returns true if the supplied prefix represents the default namespace in the context of the current node.boolean
isLanguage(java.lang.String lang)
Check whether our locale matches the specified language.abstract boolean
isLeaf()
If true, this node does not have childrenboolean
isNode()
Deprecated.Please use !isContainer()boolean
isRoot()
Returns true if this Pointer has no parent.NodeIterator
namespaceIterator()
Returns a NodeIterator that iterates over all namespaces of the value currently pointed at.NodePointer
namespacePointer(java.lang.String namespace)
Returns a NodePointer for the specified namespace.static NodePointer
newChildNodePointer(NodePointer parent, QName name, java.lang.Object bean)
Allocates an new child NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.static NodePointer
newNodePointer(QName name, java.lang.Object bean, java.util.Locale locale)
Allocates an entirely new NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.private static void
printDeep(NodePointer pointer, java.lang.String indent)
Print deepvoid
printPointerChain()
Print internal structure of a pointer for debuggingvoid
remove()
Remove the node of the object graph this pointer points to.void
setAttribute(boolean attribute)
Set to true if the pointer represents the "attribute::" axis.void
setIndex(int index)
Set the index of this NodePointer.void
setNamespaceResolver(NamespaceResolver namespaceResolver)
Set the NamespaceResolver for this NodePointer.abstract void
setValue(java.lang.Object value)
Converts the value to the required type and changes the corresponding object to that value.boolean
testNode(NodeTest test)
Checks if this Pointer matches the supplied NodeTest.java.lang.String
toString()
-
-
-
Field Detail
-
WHOLE_COLLECTION
public static final int WHOLE_COLLECTION
Whole collection index.- See Also:
- Constant Field Values
-
UNKNOWN_NAMESPACE
public static final java.lang.String UNKNOWN_NAMESPACE
Constant to indicate unknown namespace- See Also:
- Constant Field Values
-
index
protected int index
Index for this NodePointer
-
attribute
private boolean attribute
-
namespaceResolver
private NamespaceResolver namespaceResolver
-
rootNode
private transient java.lang.Object rootNode
-
parent
protected NodePointer parent
Parent pointer
-
locale
protected java.util.Locale locale
Locale
-
-
Constructor Detail
-
NodePointer
protected NodePointer(NodePointer parent)
Create a new NodePointer.- Parameters:
parent
- Pointer
-
NodePointer
protected NodePointer(NodePointer parent, java.util.Locale locale)
Create a new NodePointer.- Parameters:
parent
- Pointerlocale
- Locale
-
-
Method Detail
-
newNodePointer
public static NodePointer newNodePointer(QName name, java.lang.Object bean, java.util.Locale locale)
Allocates an entirely new NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.- Parameters:
name
- QNamebean
- Objectlocale
- Locale- Returns:
- NodePointer
-
newChildNodePointer
public static NodePointer newChildNodePointer(NodePointer parent, QName name, java.lang.Object bean)
Allocates an new child NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.- Parameters:
parent
- pointername
- QNamebean
- Object- Returns:
- NodePointer
-
getNamespaceResolver
public NamespaceResolver getNamespaceResolver()
Get the NamespaceResolver associated with this NodePointer.- Returns:
- NamespaceResolver
-
setNamespaceResolver
public void setNamespaceResolver(NamespaceResolver namespaceResolver)
Set the NamespaceResolver for this NodePointer.- Parameters:
namespaceResolver
- NamespaceResolver
-
getParent
public NodePointer getParent()
Get the parent pointer.- Returns:
- NodePointer
-
getImmediateParentPointer
public NodePointer getImmediateParentPointer()
Get the immediate parent pointer.- Returns:
- NodePointer
-
setAttribute
public void setAttribute(boolean attribute)
Set to true if the pointer represents the "attribute::" axis.- Parameters:
attribute
- boolean
-
isAttribute
public boolean isAttribute()
Returns true if the pointer represents the "attribute::" axis.- Returns:
- boolean
-
isRoot
public boolean isRoot()
Returns true if this Pointer has no parent.- Returns:
- boolean
-
isLeaf
public abstract boolean isLeaf()
If true, this node does not have children- Returns:
- boolean
-
isNode
public boolean isNode()
Deprecated.Please use !isContainer()Learn whether this pointer is considered to be a node.- Returns:
- boolean
-
isContainer
public boolean isContainer()
If true, this node is auxiliary and can only be used as an intermediate in the chain of pointers.- Returns:
- boolean
-
getIndex
public int getIndex()
If the pointer represents a collection, the index identifies an element of that collection. The default value ofindex
isWHOLE_COLLECTION
, which just means that the pointer is not indexed at all. Note: the index on NodePointer starts with 0, not 1.- Returns:
- int
-
setIndex
public void setIndex(int index)
Set the index of this NodePointer.- Parameters:
index
- int
-
isCollection
public abstract boolean isCollection()
Returnstrue
if the value of the pointer is an array or a Collection.- Returns:
- boolean
-
getLength
public abstract int getLength()
If the pointer represents a collection (or collection element), returns the length of the collection. Otherwise returns 1 (even if the value is null).- Returns:
- int
-
getValue
public java.lang.Object getValue()
By default, returnsgetNode()
, can be overridden to return a "canonical" value, like for instance a DOM element should return its string value.
-
getValuePointer
public NodePointer getValuePointer()
If this pointer manages a transparent container, like a variable, this method returns the pointer to the contents. Only an auxiliary (non-node) pointer can (and should) return a value pointer other than itself. Note that you probably don't want to overridegetValuePointer()
directly. Override thegetImmediateValuePointer()
method instead. ThegetValuePointer()
method is callsgetImmediateValuePointer()
and, if the result is notthis
, invokesgetValuePointer()
recursively. The idea here is to open all nested containers. Let's say we have a container within a container within a container. ThegetValuePointer()
method should then open all those containers and return the pointer to the ultimate contents. It does so with the above recursion.- Returns:
- NodePointer
-
getImmediateValuePointer
public NodePointer getImmediateValuePointer()
- Returns:
- NodePointer is either
this
or a pointer for the immediately contained value. - See Also:
getValuePointer()
-
isActual
public boolean isActual()
An actual pointer points to an existing part of an object graph, even if it is null. A non-actual pointer represents a part that does not exist at all. For instance consider the pointer "/address/street". If both address and street are not null, the pointer is actual. If address is not null, but street is null, the pointer is still actual. If address is null, the pointer is not actual. (In JavaBeans) if address is not a property of the root bean, a Pointer for this path cannot be obtained at all - actual or otherwise.- Returns:
- boolean
-
getName
public abstract QName getName()
Returns the name of this node. Can be null.- Returns:
- QName
-
getBaseValue
public abstract java.lang.Object getBaseValue()
Returns the value represented by the pointer before indexing. So, if the node represents an element of a collection, this method returns the collection itself.- Returns:
- Object value
-
getNodeValue
public java.lang.Object getNodeValue()
Deprecated.1.1 Please use getNode()Returns the object the pointer points to; does not convert it to a "canonical" type.- Returns:
- Object node value
-
getNode
public java.lang.Object getNode()
Returns the object the pointer points to; does not convert it to a "canonical" type. Opens containers, properties etc and returns the ultimate contents.
-
getRootNode
public java.lang.Object getRootNode()
Get the root node.- Specified by:
getRootNode
in interfacePointer
- Returns:
- Object value of this pointer's root (top parent).
-
getImmediateNode
public abstract java.lang.Object getImmediateNode()
Returns the object the pointer points to; does not convert it to a "canonical" type.- Returns:
- Object node
-
setValue
public abstract void setValue(java.lang.Object value)
Converts the value to the required type and changes the corresponding object to that value.
-
compareChildNodePointers
public abstract int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2)
Compares two child NodePointers and returns a positive number, zero or a positive number according to the order of the pointers.- Parameters:
pointer1
- first pointer to be comparedpointer2
- second pointer to be compared- Returns:
- int per Java comparison conventions
-
testNode
public boolean testNode(NodeTest test)
Checks if this Pointer matches the supplied NodeTest.- Parameters:
test
- the NodeTest to execute- Returns:
- true if a match
-
equalStrings
private static boolean equalStrings(java.lang.String s1, java.lang.String s2)
Compare two strings, either of which may be null, for equality.- Parameters:
s1
- the first String to compares2
- the second String to compare- Returns:
- true if both Strings are null, same or equal
-
createPath
public NodePointer createPath(JXPathContext context, java.lang.Object value)
Called directly by JXPathContext. Must create path and set value.- Parameters:
context
- the owning JXPathContextvalue
- the new value to set- Returns:
- created NodePointer
-
remove
public void remove()
Remove the node of the object graph this pointer points to.
-
createPath
public NodePointer createPath(JXPathContext context)
Called by a child pointer when it needs to create a parent object. Must create an object described by this pointer and return a new pointer that properly describes the new object.- Parameters:
context
- the owning JXPathContext- Returns:
- created NodePointer
-
createChild
public NodePointer createChild(JXPathContext context, QName name, int index, java.lang.Object value)
Called by a child pointer if that child needs to assign the value supplied in the createPath(context, value) call to a non-existent node. This method may have to expand the collection in order to assign the element.- Parameters:
context
- the owning JXPathCOntextname
- the QName at which a child should be createdindex
- child index.value
- node value to set- Returns:
- created NodePointer
-
createChild
public NodePointer createChild(JXPathContext context, QName name, int index)
Called by a child pointer when it needs to create a parent object for a non-existent collection element. It may have to expand the collection, then create an element object and return a new pointer describing the newly created element.- Parameters:
context
- the owning JXPathCOntextname
- the QName at which a child should be createdindex
- child index.- Returns:
- created NodePointer
-
createAttribute
public NodePointer createAttribute(JXPathContext context, QName name)
Called to create a non-existing attribute- Parameters:
context
- the owning JXPathCOntextname
- the QName at which an attribute should be created- Returns:
- created NodePointer
-
getLocale
public java.util.Locale getLocale()
If the Pointer has a parent, returns the parent's locale; otherwise returns the locale specified when this Pointer was created.- Returns:
- Locale for this NodePointer
-
isLanguage
public boolean isLanguage(java.lang.String lang)
Check whether our locale matches the specified language.- Parameters:
lang
- String language to check- Returns:
- true if the selected locale name starts with the specified prefix lang, case-insensitive.
-
childIterator
public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith)
Returns a NodeIterator that iterates over all children or all children that match the given NodeTest, starting with the specified one.- Parameters:
test
- NodeTest to filter childrenreverse
- specified iteration directionstartWith
- the NodePointer to start with- Returns:
- NodeIterator
-
attributeIterator
public NodeIterator attributeIterator(QName qname)
Returns a NodeIterator that iterates over all attributes of the current node matching the supplied node name (could have a wildcard). May return null if the object does not support the attributes.- Parameters:
qname
- the attribute name to test- Returns:
- NodeIterator
-
namespaceIterator
public NodeIterator namespaceIterator()
Returns a NodeIterator that iterates over all namespaces of the value currently pointed at. May return null if the object does not support the namespaces.- Returns:
- NodeIterator
-
namespacePointer
public NodePointer namespacePointer(java.lang.String namespace)
Returns a NodePointer for the specified namespace. Will return null if namespaces are not supported. Will return UNKNOWN_NAMESPACE if there is no such namespace.- Parameters:
namespace
- incoming namespace- Returns:
- NodePointer for
namespace
-
getNamespaceURI
public java.lang.String getNamespaceURI(java.lang.String prefix)
Decodes a namespace prefix to the corresponding URI.- Parameters:
prefix
- prefix to decode- Returns:
- String uri
-
getNamespaceURI
public java.lang.String getNamespaceURI()
Returns the namespace URI associated with this Pointer.- Returns:
- String uri
-
isDefaultNamespace
protected boolean isDefaultNamespace(java.lang.String prefix)
Returns true if the supplied prefix represents the default namespace in the context of the current node.- Parameters:
prefix
- the prefix to check- Returns:
true
if prefix is default
-
getDefaultNamespaceURI
protected java.lang.String getDefaultNamespaceURI()
Get the default ns uri- Returns:
- String uri
-
getPointerByID
public Pointer getPointerByID(JXPathContext context, java.lang.String id)
Locates a node by ID.- Parameters:
context
- JXPathContext owning contextid
- String id- Returns:
- Pointer found
-
getPointerByKey
public Pointer getPointerByKey(JXPathContext context, java.lang.String key, java.lang.String value)
Locates a node by key and value.- Parameters:
context
- owning JXPathContextkey
- key to search forvalue
- value to match- Returns:
- Pointer found
-
getNodeSetByKey
public NodeSet getNodeSetByKey(JXPathContext context, java.lang.String key, java.lang.Object value)
Find a NodeSet by key/value.- Parameters:
context
- owning JXPathContextkey
- key to search forvalue
- value to match- Returns:
- NodeSet found
-
asPath
public java.lang.String asPath()
Returns an XPath that maps to this Pointer.
-
clone
public java.lang.Object clone()
Clone this NodePointer.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
compareTo
public int compareTo(java.lang.Object object)
- Specified by:
compareTo
in interfacejava.lang.Comparable
-
compareNodePointers
private int compareNodePointers(NodePointer p1, int depth1, NodePointer p2, int depth2)
Compare node pointers.- Parameters:
p1
- pointer 1depth1
- depth 1p2
- pointer 2depth2
- depth 2- Returns:
- comparison result: (< 0) -> (p1 lt p2); (0) -> (p1 eq p2); (> 0) -> (p1 gt p2)
-
printPointerChain
public void printPointerChain()
Print internal structure of a pointer for debugging
-
escape
protected java.lang.String escape(java.lang.String string)
Return a string escaping single and double quotes.- Parameters:
string
- string to treat- Returns:
- string with any necessary changes made.
-
getAbstractFactory
protected AbstractFactory getAbstractFactory(JXPathContext context)
Get the AbstractFactory associated with the specified JXPathContext.- Parameters:
context
- JXPathContext- Returns:
- AbstractFactory
-
printDeep
private static void printDeep(NodePointer pointer, java.lang.String indent)
Print deep- Parameters:
pointer
- to printindent
- indentation level
-
-