Class NodePointer
java.lang.Object
org.apache.commons.jxpath.ri.model.NodePointer
- All Implemented Interfaces:
Serializable, Cloneable, Comparable, Pointer
- Direct Known Subclasses:
CollectionPointer, ContainerPointer, DOMAttributePointer, DOMNodePointer, JDOMAttributePointer, JDOMNamespacePointer, JDOMNodePointer, LangAttributePointer, NamespacePointer, PropertyOwnerPointer, PropertyPointer, VariablePointer
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprotected intIndex for this NodePointerprotected LocaleLocaleprivate NamespaceResolverprotected NodePointerParent pointerprivate Objectstatic final StringConstant to indicate unknown namespacestatic final intWhole collection index. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNodePointer(NodePointer parent) Create a new NodePointer.protectedNodePointer(NodePointer parent, Locale locale) Create a new NodePointer. -
Method Summary
Modifier and TypeMethodDescriptionasPath()Returns an XPath that maps to this Pointer.attributeIterator(QName qname) Returns a NodeIterator that iterates over all attributes of the current node matching the supplied node name (could have a wildcard).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.clone()Clone this NodePointer.abstract intcompareChildNodePointers(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 intcompareNodePointers(NodePointer p1, int depth1, NodePointer p2, int depth2) Compare node pointers.intcreateAttribute(JXPathContext context, QName name) Called to create a non-existing attributecreateChild(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.createChild(JXPathContext context, QName name, int index, 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.createPath(JXPathContext context) Called by a child pointer when it needs to create a parent object.createPath(JXPathContext context, Object value) Called directly by JXPathContext.private static booleanequalStrings(String s1, String s2) Compare two strings, either of which may be null, for equality.protected StringReturn a string escaping single and double quotes.protected AbstractFactorygetAbstractFactory(JXPathContext context) Get the AbstractFactory associated with the specified JXPathContext.abstract ObjectReturns the value represented by the pointer before indexing.protected StringGet the default ns uriabstract ObjectReturns the object the pointer points to; does not convert it to a "canonical" type.Get the immediate parent pointer.intgetIndex()If the pointer represents a collection, the index identifies an element of that collection.abstract intIf the pointer represents a collection (or collection element), returns the length of the collection.If the Pointer has a parent, returns the parent's locale; otherwise returns the locale specified when this Pointer was created.abstract QNamegetName()Returns the name of this node.Get the NamespaceResolver associated with this NodePointer.Returns the namespace URI associated with this Pointer.getNamespaceURI(String prefix) Decodes a namespace prefix to the corresponding URI.getNode()Returns the object the pointer points to; does not convert it to a "canonical" type.getNodeSetByKey(JXPathContext context, String key, Object value) Find a NodeSet by key/value.Deprecated.1.1 Please use getNode()Get the parent pointer.getPointerByID(JXPathContext context, String id) Locates a node by ID.getPointerByKey(JXPathContext context, String key, String value) Locates a node by key and value.Get the root node.getValue()By default, returnsgetNode(), can be overridden to return a "canonical" value, like for instance a DOM element should return its string value.If this pointer manages a transparent container, like a variable, this method returns the pointer to the contents.booleanisActual()An actual pointer points to an existing part of an object graph, even if it is null.booleanReturns true if the pointer represents the "attribute::" axis.abstract booleanReturnstrueif the value of the pointer is an array or a Collection.booleanIf true, this node is auxiliary and can only be used as an intermediate in the chain of pointers.protected booleanisDefaultNamespace(String prefix) Returns true if the supplied prefix represents the default namespace in the context of the current node.booleanisLanguage(String lang) Check whether our locale matches the specified language.abstract booleanisLeaf()If true, this node does not have childrenbooleanisNode()Deprecated.Please use !isContainer()booleanisRoot()Returns true if this Pointer has no parent.Returns a NodeIterator that iterates over all namespaces of the value currently pointed at.namespacePointer(String namespace) Returns a NodePointer for the specified namespace.static NodePointernewChildNodePointer(NodePointer parent, QName name, Object bean) Allocates an new child NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.static NodePointernewNodePointer(QName name, Object bean, Locale locale) Allocates an entirely new NodePointer by iterating through all installed NodePointerFactories until it finds one that can create a pointer.private static voidprintDeep(NodePointer pointer, String indent) Print deepvoidPrint internal structure of a pointer for debuggingvoidremove()Remove the node of the object graph this pointer points to.voidsetAttribute(boolean attribute) Set to true if the pointer represents the "attribute::" axis.voidsetIndex(int index) Set the index of this NodePointer.voidsetNamespaceResolver(NamespaceResolver namespaceResolver) Set the NamespaceResolver for this NodePointer.abstract voidConverts the value to the required type and changes the corresponding object to that value.booleanChecks if this Pointer matches the supplied NodeTest.toString()
-
Field Details
-
WHOLE_COLLECTION
public static final int WHOLE_COLLECTIONWhole collection index.- See Also:
-
UNKNOWN_NAMESPACE
-
index
protected int indexIndex for this NodePointer -
attribute
private boolean attribute -
namespaceResolver
-
rootNode
-
parent
Parent pointer -
locale
Locale
-
-
Constructor Details
-
NodePointer
-
NodePointer
Create a new NodePointer.- Parameters:
parent- Pointerlocale- Locale
-
-
Method Details
-
newNodePointer
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
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
Get the NamespaceResolver associated with this NodePointer.- Returns:
- NamespaceResolver
-
setNamespaceResolver
Set the NamespaceResolver for this NodePointer.- Parameters:
namespaceResolver- NamespaceResolver
-
getParent
-
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 ofindexisWHOLE_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()Returnstrueif 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
-
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
- Returns:
- NodePointer is either
thisor a pointer for the immediately contained value. - See Also:
-
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
-
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
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
-
getRootNode
Get the root node.- Specified by:
getRootNodein interfacePointer- Returns:
- Object value of this pointer's root (top parent).
-
getImmediateNode
Returns the object the pointer points to; does not convert it to a "canonical" type.- Returns:
- Object node
-
setValue
-
compareChildNodePointers
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
Checks if this Pointer matches the supplied NodeTest.- Parameters:
test- the NodeTest to execute- Returns:
- true if a match
-
equalStrings
-
createPath
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
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
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
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
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
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
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
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
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
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
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
-
getNamespaceURI
Returns the namespace URI associated with this Pointer.- Returns:
- String uri
-
isDefaultNamespace
Returns true if the supplied prefix represents the default namespace in the context of the current node.- Parameters:
prefix- the prefix to check- Returns:
trueif prefix is default
-
getDefaultNamespaceURI
-
getPointerByID
Locates a node by ID.- Parameters:
context- JXPathContext owning contextid- String id- Returns:
- Pointer found
-
getPointerByKey
Locates a node by key and value.- Parameters:
context- owning JXPathContextkey- key to search forvalue- value to match- Returns:
- Pointer found
-
getNodeSetByKey
Find a NodeSet by key/value.- Parameters:
context- owning JXPathContextkey- key to search forvalue- value to match- Returns:
- NodeSet found
-
asPath
-
clone
-
toString
-
compareTo
- Specified by:
compareToin interfaceComparable
-
compareNodePointers
Compare node pointers.- Parameters:
p1- pointer 1depth1- depth 1p2- pointer 2depth2- depth 2- Returns:
- comparison result: (invalid input: '<' 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
-
getAbstractFactory
Get the AbstractFactory associated with the specified JXPathContext.- Parameters:
context- JXPathContext- Returns:
- AbstractFactory
-
printDeep
Print deep- Parameters:
pointer- to printindent- indentation level
-