public class AbstractFunction<T> extends FunctionResolver.AbstractSelfDescribingFunction
init()
is invoked.
processArg(int, java.lang.String, T)
method is called.
getResult(T)
is invoked, and
its return value is returned from evaluate(java.util.List)
.
Subclasses define their name, namespace, and the number of arguments they accept (which may be undefined).
To support thread safety, subclasses are expected to make use of a helper
object that holds intermediate results. The helper object is created by
init()
, is an argument to and return from processArg()
, and is an argument to getResult()
. Subclasses are
parameterized with the helper object type, and the default behavior of
getResult()
is to return the helper (this makes life easier
for a function that takes zero or one arguments).
Note: in JDK 1.5, DOM objects implement both Node
and NodeList
. While this means that processArg(Node)
would never be called as part of the normal dispatch loop (unless
the implementation changes), it is called as part of the default
behavior of processArg(NodeList)
, and is given the first
element in the list.
Implementation note: The design of this class is arguably over-complex.
If all you want is a simple function to process string content, use
AbstractStringFunction
.
Modifier | Constructor and Description |
---|---|
protected |
AbstractFunction(QName qname,
int minArgs,
int maxArgs)
Base constructor.
|
protected |
AbstractFunction(String nsUri,
String localName)
Constructor for a function that can take any number of arguments.
|
protected |
AbstractFunction(String nsUri,
String localName,
int numArgs)
Constructor for a function that has a fixed number of arguments.
|
protected |
AbstractFunction(String nsUri,
String localName,
int minArgs,
int maxArgs)
Constructor for a function that has a variable number of arguments.
|
Modifier and Type | Method and Description |
---|---|
Object |
evaluate(List args)
Invokes methods defined by the subclasses to evaluate the function.
|
protected Object |
getResult(T helper)
Returns the result of this invocation.
|
protected T |
init()
Creates a helper object to preserve intermediate results.
|
protected T |
processArg(int index,
Boolean value,
T helper)
Processes a Boolean argument.
|
protected T |
processArg(int index,
NodeList value,
T helper)
Processes a NodeList argument.
|
protected T |
processArg(int index,
Node value,
T helper)
Processes a Node argument.
|
protected T |
processArg(int index,
Number value,
T helper)
Processes a Number argument.
|
protected T |
processArg(int index,
String value,
T helper)
Processes a String argument.
|
protected T |
processNullArg(int index,
T helper)
Processes a
null argument — it's unclear whether
this can ever happen. |
protected T |
processUnexpectedArg(int index,
Object value,
T helper)
Processes an argument that is not one of the defined types for XPath
evaluation — it is unclear whether this can actually happen.
|
compareTo, equals, getMaxArgCount, getMinArgCount, getName, getNamespaceUri, getQName, hashCode, isArityMatch, isMatch
protected AbstractFunction(String nsUri, String localName)
protected AbstractFunction(String nsUri, String localName, int numArgs)
protected AbstractFunction(String nsUri, String localName, int minArgs, int maxArgs)
protected AbstractFunction(QName qname, int minArgs, int maxArgs)
public Object evaluate(List args) throws XPathFunctionException
processArg()
method in turn
for each argument, then invokes getResult()
to retrieve
the function's result.XPathFunctionException
- if the supplied argument list does
not match the min/max for this function, or if any called
method throws an exception.protected T init() throws Exception
processArg()
and getResult()
.
The default implementation returns null
.
Subclasses are permitted to throw any exception; it will be wrapped in
an XPathFunctionException
.
Exception
protected T processArg(int index, String value, T helper) throws Exception
index
- Index of the argument, numbered from 0.value
- Value of the argument.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processArg(int index, Number value, T helper) throws Exception
Double
index
- Index of the argument, numbered from 0.value
- Value of the argument.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processArg(int index, Boolean value, T helper) throws Exception
index
- Index of the argument, numbered from 0.value
- Value of the argument.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processArg(int index, Node value, T helper) throws Exception
processArg(NodeList)
.index
- Index of the argument, numbered from 0.value
- Value of the argument. May be null
.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processArg(int index, NodeList value, T helper) throws Exception
The default implementation calls processArg(Node)
with
the first element in the list (null
if the list is empty).
index
- Index of the argument, numbered from 0.value
- Value of the argument.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processNullArg(int index, T helper) throws Exception
null
argument — it's unclear whether
this can ever happen. The default implementation throws
IllegalArgumentException
.index
- Index of the argument, numbered from 0.helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected T processUnexpectedArg(int index, Object value, T helper) throws Exception
IllegalArgumentException
.index
- Index of the argument, numbered from 0.value
- The argument valuehelper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception
protected Object getResult(T helper) throws Exception
helper
- Helper object to preserve intermediate results.Subclasses
- are permitted to throw any exception. It will be
wrapped in XPathFunctionException
and cause
function processing to abort.Exception