JDOM
2.0.5

org.jdom2.xpath
Interface XPathExpression<T>

Type Parameters:
T - The generic type of the results of the XPath query after being processed by the JDOM Filter<T>
All Superinterfaces:
java.lang.Cloneable
All Known Implementing Classes:
AbstractXPathCompiled

public interface XPathExpression<T>
extends java.lang.Cloneable

XPathExpression is a representation of a compiled XPath query and any Namespace or variable references the query may require.

Once an XPathExpression is created, the values associated with variable names can be changed. But new variables may not be added.

XPathExpression is not thread-safe. XPath libraries allow variable values to change between calls to their query routines, but require that the variable value is constant for the duration of any particular evaluation. It is easier to simply have separate XPathExpression instances in each thread than it is to manage the synchronization of a single instance. XPathExpression thus supports Cloneable to easily create another XPathExpression instance. It is the responsibility of the JDOM caller to ensure appropriate synchronisation of the XPathExpression if it is accessed from multiple threads.

Author:
Rolf Lear

Method Summary
 XPathExpression<T> clone()
          Create a new instance of this XPathExpression that duplicates this instance.
 XPathDiagnostic<T> diagnose(java.lang.Object context, boolean firstonly)
          Evaluate the XPath query against the supplied context, but return additional data which may be useful for diagnosing problems with XPath queries.
 java.util.List<T> evaluate(java.lang.Object context)
          Process the compiled XPathExpression against the specified context.
 T evaluateFirst(java.lang.Object context)
          Return the first value in the XPath query result set type-cast to the return type of this XPathExpression.
 java.lang.String getExpression()
          Get the XPath expression
 Filter<T> getFilter()
          Get the Filter<T> used to coerce the raw XPath results in to the correct Generic type.
 Namespace getNamespace(java.lang.String prefix)
          Get the Namespace associated with a given prefix.
 Namespace[] getNamespaces()
          Get the Namespaces that were used to compile this XPathExpression.
 java.lang.Object getVariable(java.lang.String qname)
          Get the variable value associated to the given variable qname.
 java.lang.Object getVariable(java.lang.String localname, Namespace uri)
          Get the variable value associated to the given variable name.
 java.lang.Object setVariable(java.lang.String localname, Namespace uri, java.lang.Object value)
          Change the defined value for a variable to some new value.
 java.lang.Object setVariable(java.lang.String qname, java.lang.Object value)
          Change the defined value for a variable to some new value.
 

Method Detail

clone

XPathExpression<T> 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:
a new XPathExpression instance that shares the same core details as this.

getExpression

java.lang.String getExpression()
Get the XPath expression

Returns:
the string representation of the XPath expression

getNamespace

Namespace getNamespace(java.lang.String prefix)
Get the Namespace associated with a given prefix.

Parameters:
prefix - The prefix to select the Namespace URI for.
Returns:
the URI of the specified Namespace prefix
Throws:
java.lang.IllegalArgumentException - if that prefix is not defined.

getNamespaces

Namespace[] getNamespaces()
Get the Namespaces that were used to compile this XPathExpression.

Returns:
a potentially empty array of Namespaces (never null).

setVariable

java.lang.Object setVariable(java.lang.String localname,
                             Namespace uri,
                             java.lang.Object value)
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:
localname - The variable localname to change.
uri - the Namespace in which the variable name is declared.
value - The new value to set.
Returns:
The value of the variable prior to this change.
Throws:
java.lang.NullPointerException - if name or uri is null
java.lang.IllegalArgumentException - if name is not already a variable.

setVariable

java.lang.Object setVariable(java.lang.String qname,
                             java.lang.Object value)
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 - The variable qname to change.
value - The new value to set.
Returns:
The value of the variable prior to this change.
Throws:
java.lang.NullPointerException - if qname is null
java.lang.IllegalArgumentException - if name is not already a variable.

getVariable

java.lang.Object getVariable(java.lang.String localname,
                             Namespace uri)
Get the variable value associated to the given variable name.

Parameters:
localname - the variable localname to retrieve the value for.
uri - the Namespace in which the variable name was declared.
Returns:
the value associated to a Variable name.
Throws:
java.lang.NullPointerException - if name or uri is null
java.lang.IllegalArgumentException - if that variable name is not defined.

getVariable

java.lang.Object getVariable(java.lang.String qname)
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 - the variable qname to retrieve the value for.
Returns:
the value associated to a Variable name.
Throws:
java.lang.NullPointerException - if qname is null
java.lang.IllegalArgumentException - if that variable name is not defined.

getFilter

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

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

evaluate

java.util.List<T> evaluate(java.lang.Object context)
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 - The context against which to process the query.
Returns:
a list of the XPath results.
Throws:
java.lang.NullPointerException - if the context is null
java.lang.IllegalStateException - if the expression is not runnable or if the context node is not appropriate for the expression.

evaluateFirst

T evaluateFirst(java.lang.Object context)
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 - The context against which to evaluate the expression. This will typically be a Document, Element, or some other JDOM object.
Returns:
The first XPath result (if there is any) coerced to the generic type of this XPathExpression, or null if it cannot be coerced.
Throws:
java.lang.NullPointerException - if the context is null
java.lang.IllegalStateException - if the expression is not runnable or if the context node is not appropriate for the expression.

diagnose

XPathDiagnostic<T> diagnose(java.lang.Object context,
                            boolean firstonly)
Evaluate the XPath query against the supplied context, but return additional data which may be useful for diagnosing problems with XPath queries.

Parameters:
context - The context against which to run the query.
firstonly - Indicate whether the XPath expression can be terminated after the first successful result value.
Returns:
an XPathDiagnostic instance.
Throws:
java.lang.NullPointerException - if the context is null
java.lang.IllegalStateException - if the expression is not runnable or if the context node is not appropriate for the expression.

JDOM
2.0.5

Copyright 2013 Jason Hunter, Brett McLaughlin. All Rights Reserved.