public class XPathWrapperFactory extends Object implements Cloneable
XPathWrapper
instances with the same
configuration (namespace bindings and functions), reducing the amount
of code needed when applying multiple expressions to the same complex
XML.
As with XPathWrapper
itself, this object is intended to
be configured using the builder pattern: all configuration methods
return the factory instance. Binding methods are the same as
XPathWrapper
, excepting the deprecated
bindDefaultNamespace()
.
As a potential performance improvement, for XPath expressions that are repeatedly executed, you can create an optional cache for created wrapper instances. Note that changes to the factory configuration will not affect cached wrapper instances. Note: At the present time, there is no way to clear the cache.
The newXPath(java.lang.String)
method is reentrant, and may be invoked from multiple
threads concurrently. However, this class is not generally thread-safe: you
cannot configure in one thread and create wrapper instances in another.
Modifier and Type | Class and Description |
---|---|
static class |
XPathWrapperFactory.CacheType
Defines the various options for caching.
|
Constructor and Description |
---|
XPathWrapperFactory()
Creates a factory that will always return new
XPathWrapper
instances. |
XPathWrapperFactory(XPathWrapperFactory.CacheType cacheType)
Creates a factory that will optionally store
XPathWrapper
instances in a cache. |
Modifier and Type | Method and Description |
---|---|
XPathWrapperFactory |
bindFunction(FunctionResolver.SelfDescribingFunction func)
Binds a self-describing function to this expression.
|
XPathWrapperFactory |
bindFunction(FunctionResolver.SelfDescribingFunction func,
String prefix)
Binds a self-describing function to this expression, along with the
prefix used to access that function.
|
XPathWrapperFactory |
bindFunction(QName name,
XPathFunction func)
Binds a standard
XPathFunction to this expression,
handling any number of arguments. |
XPathWrapperFactory |
bindFunction(QName name,
XPathFunction func,
int arity)
Binds a standard
XPathFunction to this expression,
handling a specific number of arguments. |
XPathWrapperFactory |
bindFunction(QName name,
XPathFunction func,
int minArity,
int maxArity)
Binds a standard
XPathFunction to this expression,
handling a specific range of arguments. |
XPathWrapperFactory |
bindNamespace(String prefix,
String nsURI)
Adds a namespace binding, which will be used for subsequent wrappers
created from this factory.
|
XPathWrapperFactory |
bindVariable(QName name,
Object value)
Binds a value to a variable, replacing any previous value for that
variable.
|
XPathWrapperFactory |
bindVariable(String name,
Object value)
Binds a value to a variable, replacing any previous value for that
variable.
|
XPathWrapper |
newXPath(String xpath)
Returns an
XPathWrapper instance for the specified expression. |
public XPathWrapperFactory()
XPathWrapper
instances.public XPathWrapperFactory(XPathWrapperFactory.CacheType cacheType)
XPathWrapper
instances in a cache.cacheType
- One of the XPathWrapperFactory.CacheType
values.public XPathWrapperFactory bindNamespace(String prefix, String nsURI)
prefix
- The prefix used to reference this namespace in the
XPath expression. Note that this does not
need to be the same prefix used by the document.nsURI
- The namespace URI to associate with this prefix.public XPathWrapperFactory bindVariable(String name, Object value)
evaluate()
; the new values will be used
for subsequent evaluations.name
- The name of the variable; this is turned into a
QName
without namespace.value
- The value of the variable; the XPath evaluator must
be able to convert this value into a type usable in
an XPath expression.public XPathWrapperFactory bindVariable(QName name, Object value)
evaluate()
; the new values will be used
for subsequent evaluations.name
- The fully-qualified name of the variable.value
- The value of the variable; the XPath evaluator must
be able to convert this value into a type usable in
an XPath expression.public XPathWrapperFactory bindFunction(FunctionResolver.SelfDescribingFunction func)
All XPath instances created from this factory will share a single instance of the bound function. If your function maintains state, you are responsible for proper synchronization.
Per the JDK documentation, user-defined functions must occupy their
own namespace. You must bind a namespace for this function, and use
the bound prefix to reference it in your expression. Alternatively,
you can call the variant of bindFunction()
that binds
a prefix to the function's namespace.
func
- The function.public XPathWrapperFactory bindFunction(FunctionResolver.SelfDescribingFunction func, String prefix)
All XPath instances created from this factory will share a single instance of the bound function. If your function maintains state, you are responsible for proper synchronization.
Per the JDK documentation, user-defined functions must occupy their own namespace. This method will retrieve the namespace from the function, and bind it to the passed prefix.
func
- The function.prefix
- The prefix to bind to this function's namespace.public XPathWrapperFactory bindFunction(QName name, XPathFunction func)
XPathFunction
to this expression,
handling any number of arguments. Subsequent calls to this method
with the same name will silently replace the binding.
All XPath instances created from this factory will share a single instance of the bound function. If your function maintains state, you are responsible for proper synchronization.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.public XPathWrapperFactory bindFunction(QName name, XPathFunction func, int arity)
XPathFunction
to this expression,
handling a specific number of arguments. Subsequent calls to this
method with the same name and arity will silently replace the binding.
All XPath instances created from this factory will share a single instance of the bound function. If your function maintains state, you are responsible for proper synchronization.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.arity
- The number of arguments accepted by this function.public XPathWrapperFactory bindFunction(QName name, XPathFunction func, int minArity, int maxArity)
XPathFunction
to this expression,
handling a specific range of arguments. Subsequent calls to this
method with the same name and range of arguments will silently
replace the binding.
All XPath instances created from this factory will share a single instance of the bound function. If your function maintains state, you are responsible for proper synchronization.
Per the JDK documentation, user-defined functions must occupy their own namespace. If the qualified name that you pass to this method includes a prefix, the associated namespace will be bound to that prefix. If not, you must bind the namespace explicitly. In either case, you must refer to the function in your expression using a bound prefix.
name
- The qualified name for this function. Must contain
a name and namespace, may contain a prefix.func
- The function to bind to this name.minArity
- The minimum number of arguments accepted by this
function.maxArity
- The maximum number of arguments accepted by this
function.public XPathWrapper newXPath(String xpath)
XPathWrapper
instance for the specified expression.
This may either be a new or cached instance.