Top Description Inners Fields Constructors Methods
org.jdom2.xpath.util

public abstract Class AbstractXPathCompiled<T>

extends Object
implements XPathExpression<T>
Class Inheritance
All Implemented Interfaces
org.jdom2.xpath.XPathExpression, java.lang.Cloneable
Known Direct Subclasses
org.jdom2.xpath.jaxen.JaxenCompiled
Type Parameters
<T>
The generic type of the returned values.
Imports
java.util.Arrays, .Collections, .Comparator, .HashMap, .List, .Map, org.jdom2.Namespace, .Verifier, org.jdom2.filter.Filter, org.jdom2.xpath.XPathExpression, .XPathDiagnostic

A mostly-implemented XPathExpression that only needs two methods to be implemented in order to satisfy the complete API. Subclasses of this MUST correctly override the clone() method which in turn should call super.clone();
Author
Rolf Lear

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final AbstractXPathCompiled.NamespaceComparator
private final Filter<T>
private final Map<String, Namespace>
private final String
private Map<String, Map<String, Object>>

Constructor Summary

AccessConstructor and Description
public
AbstractXPathCompiled(final String
The XPath query
query
,
final Filter<T>
The coercion filter.
filter
,
final Map<String, Object>
A map of variables.
variables
,
final Namespace[]
The namespaces referenced from the query.
namespaces
)

Construct an XPathExpression.

Method Summary

Modifier and TypeMethod and Description
public XPathExpression<T>
clone()

Overrides java.lang.Object.clone.

Implements org.jdom2.xpath.XPathExpression.clone.

Subclasses of this AbstractXPathCompile class must call super.clone() in their clone methods!
public XPathDiagnostic<T>
diagnose(Object
The context against which to run the query.
context
,
boolean
Indicate whether the XPath expression can be terminated after the first successful result value.
firstonly
)

Implements org.jdom2.xpath.XPathExpression.diagnose.

Evaluate the XPath query against the supplied context, but return additional data which may be useful for diagnosing problems with XPath queries.
public List<T>
evaluate(Object
The context against which to process the query.
context
)

Implements org.jdom2.xpath.XPathExpression.evaluate.

Process the compiled XPathExpression against the specified context.
public T
evaluateFirst(Object
The context against which to evaluate the expression. This will typically be a Document, Element, or some other JDOM object.
context
)

Implements org.jdom2.xpath.XPathExpression.evaluateFirst.

Return the first value in the XPath query result set type-cast to the return type of this XPathExpression.
protected abstract List<?>

Returns:

A list of XPath results.
evaluateRawAll
(Object
The context against which to evaluate the query
context
)

This is the raw expression evaluator to be implemented by the back-end XPath library.

protected abstract Object

Returns:

The first item in the XPath results, or null if there are no results.
evaluateRawFirst
(Object
The context against which to evaluate the query
context
)

This is the raw expression evaluator to be implemented by the back-end XPath library.

public final String
getExpression()

Implements org.jdom2.xpath.XPathExpression.getExpression.

Get the XPath expression
public final Filter<T>
getFilter()

Implements org.jdom2.xpath.XPathExpression.getFilter.

Get the Filter<T> used to coerce the raw XPath results in to the correct Generic type.
public final Namespace
getNamespace(final String
The prefix to select the Namespace URI for.
prefix
)

Implements org.jdom2.xpath.XPathExpression.getNamespace.

Get the Namespace associated with a given prefix.
public Namespace[]
getNamespaces()

Implements org.jdom2.xpath.XPathExpression.getNamespaces.

Get the Namespaces that were used to compile this XPathExpression.
private static final String

Returns:

the prefix of the namespace
getPrefixForURI
(final String
the URI to search for
uri
,
final Namespace[]
the array of namespaces to search through
nsa
)

Utility method to find a Namespace that has a given URI, and return the prefix.

public final Object
getVariable(final String
the variable localname to retrieve the value for.
name
,
Namespace
the Namespace in which the variable name was declared.
uri
)

Implements org.jdom2.xpath.XPathExpression.getVariable.

Get the variable value associated to the given variable name.
public Object
getVariable(String
the variable qname to retrieve the value for.
qname
)

Implements org.jdom2.xpath.XPathExpression.getVariable.

Get the variable value associated to the given variable qname.
protected Map<String, Object>

Returns:

the variables set on this instance.
getVariables
()

utility method that allows descendant classes to access the variables that were set on this expression, in a format that can be used in a constructor (qname/value).

public Object
setVariable(String
The variable localname to change.
name
,
Namespace
the Namespace in which the variable name is declared.
uri
,
Object
The new value to set.
value
)

Implements org.jdom2.xpath.XPathExpression.setVariable.

Change the defined value for a variable to some new value.
public Object
setVariable(String
The variable qname to change.
qname
,
Object
The new value to set.
value
)

Implements org.jdom2.xpath.XPathExpression.setVariable.

Change the defined value for a variable to some new value.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

NSSORTback to summary
private static final AbstractXPathCompiled.NamespaceComparator NSSORT
xfilterback to summary
private final Filter<T> xfilter
xnamespacesback to summary
private final Map<String, Namespace> xnamespaces
xqueryback to summary
private final String xquery
xvariablesback to summary
private Map<String, Map<String, Object>> xvariables

Constructor Detail

AbstractXPathCompiledback to summary
public AbstractXPathCompiled(final String query, final Filter<T> filter, final Map<String, Object> variables, final Namespace[] namespaces)

Construct an XPathExpression.

Parameters
query:String

The XPath query

filter:Filter<T>

The coercion filter.

variables:Map<String, Object>

A map of variables.

namespaces:Namespace[]

The namespaces referenced from the query.

See Also
XPathExpression for conditions which throw NullPointerException or IllegalArgumentException.

Method Detail

cloneback to summary
public XPathExpression<T> clone()

Overrides java.lang.Object.clone.

Implements org.jdom2.xpath.XPathExpression.clone.

Doc from org.jdom2.xpath.XPathExpression.clone.

Subclasses of this AbstractXPathCompile class must call super.clone() in their clone methods!

This would be a sample clone method from a subclass:

		public XPathExpression<T> clone() {
			@SuppressWarnings("unchecked")
			final MyXPathCompiled<T> ret = (MyXPathCompiled<T>)super.clone();
			// change any fields that need to be cloned.
			....
			return ret;
		}
Here's the documentation from XPathExpression#clone()

Create a new instance of this XPathExpression that duplicates this instance.

The 'cloned' instance will have the same XPath query, namespace declarations, and variables. Changing a value associated with a variable on the cloned instance will not change this instance's values, and it is safe to run the evaluate methods on the cloned copy at the same time as this copy.

Returns:XPathExpression<T>

a new XPathExpression instance that shares the same core details as this.

Annotations
@Override
diagnoseback to summary
public XPathDiagnostic<T> diagnose(Object context, boolean firstonly)

Implements org.jdom2.xpath.XPathExpression.diagnose.

Doc from org.jdom2.xpath.XPathExpression.diagnose.

Evaluate the XPath query against the supplied context, but return additional data which may be useful for diagnosing problems with XPath queries.

Parameters
context:Object

The context against which to run the query.

firstonly:boolean

Indicate whether the XPath expression can be terminated after the first successful result value.

Returns:XPathDiagnostic<T>

an XPathDiagnostic instance.

Annotations
@Override
evaluateback to summary
public List<T> evaluate(Object context)

Implements org.jdom2.xpath.XPathExpression.evaluate.

Doc from org.jdom2.xpath.XPathExpression.evaluate.

Process the compiled XPathExpression against the specified context.

In the JDOM2 XPath API the results of the raw XPath query are processed by the attached Filter<T> instance to coerce the results in to the correct generic type for this XPathExpression. The Filter process may cause some XPath results to be removed from the final results. You may instead want to call the diagnose(Object, boolean) method to have access to both the raw XPath results as well as the filtered and generically typed results.

Parameters
context:Object

The context against which to process the query.

Returns:List<T>

a list of the XPath results.

Annotations
@Override
evaluateFirstback to summary
public T evaluateFirst(Object context)

Implements org.jdom2.xpath.XPathExpression.evaluateFirst.

Doc from org.jdom2.xpath.XPathExpression.evaluateFirst.

Return the first value in the XPath query result set type-cast to the return type of this XPathExpression.

The concept of the 'first' result is applied before any JDOM Filter is applied. Thus, if the underlying XPath query has some results, the first result is sent through the filter. If it matches it is returned, if it does not match, then null is returned (even if some subsequent result underlying XPath result would pass the filter).

This allows the XPath implementation to optimise the evaluateFirst method by potentially using 'short-circuit' conditions in the evaluation.

Parameters
context:Object

The context against which to evaluate the expression. This will typically be a Document, Element, or some other JDOM object.

Returns:T

The first XPath result (if there is any) coerced to the generic type of this XPathExpression, or null if it cannot be coerced.

Annotations
@Override
evaluateRawAllback to summary
protected abstract List<?> evaluateRawAll(Object context)

This is the raw expression evaluator to be implemented by the back-end XPath library.

Parameters
context:Object

The context against which to evaluate the query

Returns:List<?>

A list of XPath results.

evaluateRawFirstback to summary
protected abstract Object evaluateRawFirst(Object context)

This is the raw expression evaluator to be implemented by the back-end XPath library. When this method is processed the implementing library is free to stop processing when the result that would be the first result is retrieved.

Only the first value in the result will be processed (if any).

Parameters
context:Object

The context against which to evaluate the query

Returns:Object

The first item in the XPath results, or null if there are no results.

getExpressionback to summary
public final String getExpression()

Implements org.jdom2.xpath.XPathExpression.getExpression.

Doc from org.jdom2.xpath.XPathExpression.getExpression.

Get the XPath expression

Returns:String

the string representation of the XPath expression

Annotations
@Override
getFilterback to summary
public final Filter<T> getFilter()

Implements org.jdom2.xpath.XPathExpression.getFilter.

Doc from org.jdom2.xpath.XPathExpression.getFilter.

Get the Filter<T> used to coerce the raw XPath results in to the correct Generic type.

Returns:Filter<T>

the Filter<T> used to coerce the raw XPath results in to the correct Generic type.

Annotations
@Override
getNamespaceback to summary
public final Namespace getNamespace(final String prefix)

Implements org.jdom2.xpath.XPathExpression.getNamespace.

Doc from org.jdom2.xpath.XPathExpression.getNamespace.

Get the Namespace associated with a given prefix.

Parameters
prefix:String

The prefix to select the Namespace URI for.

Returns:Namespace

the URI of the specified Namespace prefix

Annotations
@Override
getNamespacesback to summary
public Namespace[] getNamespaces()

Implements org.jdom2.xpath.XPathExpression.getNamespaces.

Doc from org.jdom2.xpath.XPathExpression.getNamespaces.

Get the Namespaces that were used to compile this XPathExpression.

Returns:Namespace[]

a potentially empty array of Namespaces (never null).

Annotations
@Override
getPrefixForURIback to summary
private static final String getPrefixForURI(final String uri, final Namespace[] nsa)

Utility method to find a Namespace that has a given URI, and return the prefix.

Parameters
uri:String

the URI to search for

nsa:Namespace[]

the array of namespaces to search through

Returns:String

the prefix of the namespace

getVariableback to summary
public final Object getVariable(final String name, Namespace uri)

Implements org.jdom2.xpath.XPathExpression.getVariable.

Doc from org.jdom2.xpath.XPathExpression.getVariable.

Get the variable value associated to the given variable name.

Parameters
name:String

the variable localname to retrieve the value for.

uri:Namespace

the Namespace in which the variable name was declared.

Returns:Object

the value associated to a Variable name.

Annotations
@Override
getVariableback to summary
public Object getVariable(String qname)

Implements org.jdom2.xpath.XPathExpression.getVariable.

Doc from org.jdom2.xpath.XPathExpression.getVariable.

Get the variable value associated to the given variable qname.

qname must consist of an optional namespace prefix and colon, followed by a mandatory variable localname. If the prefix is not specified, then the Namespace is assumed to be the Namespace#NO_NAMESPACE. If the prefix is specified, it must match with one of the declared Namespaces for this XPathExpression

Parameters
qname:String

the variable qname to retrieve the value for.

Returns:Object

the value associated to a Variable name.

Annotations
@Override
getVariablesback to summary
protected Map<String, Object> getVariables()

utility method that allows descendant classes to access the variables that were set on this expression, in a format that can be used in a constructor (qname/value).

Returns:Map<String, Object>

the variables set on this instance.

setVariableback to summary
public Object setVariable(String name, Namespace uri, Object value)

Implements org.jdom2.xpath.XPathExpression.setVariable.

Doc from org.jdom2.xpath.XPathExpression.setVariable.

Change the defined value for a variable to some new value. You may not use this method to add new variables to the compiled XPath, you can only change existing variable values.

The value of the variable may be null. Some XPath libraries support a null value, and if the library that this expression is for does not support a null value it should be translated to something meaningful for that library, typically the empty string.

Parameters
name:String

The variable localname to change.

uri:Namespace

the Namespace in which the variable name is declared.

value:Object

The new value to set.

Returns:Object

The value of the variable prior to this change.

Annotations
@Override
setVariableback to summary
public Object setVariable(String qname, Object value)

Implements org.jdom2.xpath.XPathExpression.setVariable.

Doc from org.jdom2.xpath.XPathExpression.setVariable.

Change the defined value for a variable to some new value. You may not use this method to add new variables to the compiled XPath, you can only change existing variable values.

The value of the variable may be null. Some XPath libraries support a null value, and if the library that this expression is for does not support a null value it should be translated to something meaningful for that library, typically the empty string.

qname must consist of an optional namespace prefix and colon, followed by a mandatory variable localname. If the prefix is not specified, then the Namespace is assumed to be the Namespace#NO_NAMESPACE. If the prefix is specified, it must match with one of the declared Namespaces for this XPathExpression

Parameters
qname:String

The variable qname to change.

value:Object

The new value to set.

Returns:Object

The value of the variable prior to this change.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.jdom2.xpath.util back to summary

private final Class AbstractXPathCompiled.NamespaceComparator

extends Object
implements Comparator<Namespace>
Class Inheritance
  • java.lang.Object
  • org.jdom2.xpath.util.AbstractXPathCompiled.NamespaceComparator
All Implemented Interfaces
java.util.Comparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(Namespace
the first object to be compared.
ns1
,
Namespace
the second object to be compared.
ns2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NamespaceComparatorback to summary
private NamespaceComparator()

Method Detail

compareback to summary
public int compare(Namespace ns1, Namespace ns2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
ns1:Namespace

the first object to be compared.

ns2:Namespace

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override