public abstract class XPathFactory
extends java.lang.Object
JDOM does not extend the core Java XPath API (javax.xml.xpath.XPath). Instead it creates a new API that is more JDOM and Java friendly leading to neater and more understandable code (in a JDOM context).
A JDOM XPathFactory instance is able to create JDOM XPathExpression instances that can be used to evaluate XPath expressions against JDOM Content.
The XPathFactory allows either default or custom XPathFactory instances to be
created. If you use the newInstance(String)
method then an
XPathFactory of that specific type will be created. If you use the
instance()
method then a default XPathFactory instance will be
returned.
Instances of XPathFactory are specified to be thread-safe. You can reuse an XPathFactory in multiple threads. Instances of XPathExpression are NOT thread-safe.
Constructor and Description |
---|
XPathFactory() |
Modifier and Type | Method and Description |
---|---|
XPathExpression<java.lang.Object> |
compile(java.lang.String expression)
Create a XPathExpression<Object> instance from this factory.
|
<T> XPathExpression<T> |
compile(java.lang.String expression,
Filter<T> filter)
Create a XPathExpression<T> instance from this factory.
|
<T> XPathExpression<T> |
compile(java.lang.String expression,
Filter<T> filter,
java.util.Map<java.lang.String,java.lang.Object> variables,
java.util.Collection<Namespace> namespaces)
Create a XPathExpression<> instance from this factory.
|
abstract <T> XPathExpression<T> |
compile(java.lang.String expression,
Filter<T> filter,
java.util.Map<java.lang.String,java.lang.Object> variables,
Namespace... namespaces)
Create a Compiled XPathExpression<> instance from this factory.
|
static XPathFactory |
instance()
Obtain an instance of an XPathFactory using the default mechanisms to
determine what XPathFactory implementation to use.
|
static XPathFactory |
newInstance(java.lang.String factoryclass)
Create a new instance of an explicit XPathFactory.
|
public static final XPathFactory instance()
The exact same XPathFactory instance will be returned from each call.
The default mechanism will inspect the system property (only once)
JDOMConstants.JDOM2_PROPERTY_XPATH_FACTORY
to determine what
class should be used for the XPathFactory. If that property is not set
then JDOM will use the JaxenXPathFactory
.
public static final XPathFactory newInstance(java.lang.String factoryclass)
This method is a convenience mechanism only, and JDOM users are free to
create a custom XPathFactory instance and use a simple:
XPathFactory fac = new MyXPathFactory(arg1, arg2, ...)
factoryclass
- The name of the XPathFactory class to create.public abstract <T> XPathExpression<T> compile(java.lang.String expression, Filter<T> filter, java.util.Map<java.lang.String,java.lang.Object> variables, Namespace... namespaces)
XPathFactory implementations override this method to implement support for the JDOM/XPath API.
A Filter is used to coerce resulting XPath data in to a suitable JDOM generic
type. Note that the Filters
class has a number of predefined, useful
filters.
This compile method ensures that these XPath/Namespace rules are followed and thus this method will throw IllegalArgumentException if:
Variables are referenced from XPath expressions using a
$varname
syntax. The variable name may be a Namespace
qualified variable name of the form $pfx:localname
.
Variables $pa:var
and $pb:var
are the identical
variables if the namespace URI for prefix 'pa' is the same URI as for
prefix 'pb'.
This compile method expects all variable names to be expressed in a prefix-qualified format, where all prefixes have to be available in one of the specified Namespace instances.
e.g. if you specify a variable name "ns:var" with value "value", you also
need to have some namespace provided with the prefix "ns" such as
Namespace.getNamespace("ns", "http://example.com/nsuri");
Some XPath libraries allow null variable values (Jaxen), some do not
(native Java). This compile method will silently convert any null
Variable value to an empty string ""
.
Variables are provided in the form of a Map where the key is the variable name and the mapped value is the variable value. If the entire map is null then the compile Method assumes there are no variables.
In light of the above, this compile method will throw an IllegalArgumentException if:
T
- The generic type of the results that the XPathExpression will
produce.expression
- The XPath expression.filter
- The Filter that is used to coerce the XPath result data in to the
generic-typed results.
Note that the Filters
class has a number of predefined, useful
filters.variables
- Any variable values that may be referenced from the query. A null
value indicates that there are no variables.namespaces
- Any namespaces that may be referenced from the queryjava.lang.NullPointerException
- if the query, filter, any namespace, any variable name or any
variable value is null (although the entire variables value may
be null).java.lang.IllegalArgumentException
- if any two Namespace values share the same prefix, or if there is
any other reason that the XPath query cannot be compiled.Filters
public <T> XPathExpression<T> compile(java.lang.String expression, Filter<T> filter, java.util.Map<java.lang.String,java.lang.Object> variables, java.util.Collection<Namespace> namespaces)
T
- The generic type of the results that the XPathExpression will
produce.expression
- The XPath expression.filter
- The Filter that is used to coerce the xpath result data in to the
generic-typed results.
Note that the Filters
class has a number of predefined, useful
filters.variables
- Any variable values that may be referenced from the query. A null
value indicates that there are no variables.namespaces
- List of all namespaces that may be referenced from the queryjava.lang.NullPointerException
- if the query, filter, namespaces, any variable name or any
variable value is null (although the entire variables value may
be null).java.lang.IllegalArgumentException
- if any two Namespace values share the same prefix, or if there is
any other reason that the XPath query cannot be compiled.public <T> XPathExpression<T> compile(java.lang.String expression, Filter<T> filter)
T
- The generic type of the results that the XPathExpression will
produce.expression
- The XPath expression.filter
- The Filter that is used to coerce the xpath result data in to the
generic-typed results.
Note that the Filters
class has a number of predefined, useful
filters.java.lang.NullPointerException
- if the query or filter is nulljava.lang.IllegalArgumentException
- if there is any reason that the XPath query cannot be compiled.public XPathExpression<java.lang.Object> compile(java.lang.String expression)
expression
- The XPath expression.java.lang.NullPointerException
- if the query or filter is nulljava.lang.IllegalArgumentException
- if there is any reason that the XPath query cannot be compiled.Copyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.