Package net.sf.saxon.expr.instruct
Class UserFunction
java.lang.Object
net.sf.saxon.expr.instruct.Actor
net.sf.saxon.expr.instruct.UserFunction
- All Implemented Interfaces:
SourceLocator
,Callable
,ContextOriginator
,ExpressionOwner
,Locatable
,FunctionDefinition
,FunctionItem
,GroundedValue
,Item
,Sequence
,Location
,Traceable
,TraceableComponent
,Locator
- Direct Known Subclasses:
MemoFunction
,StreamableUserFunction
public class UserFunction
extends Actor
implements FunctionItem, FunctionDefinition, ContextOriginator, TraceableComponent
This object represents the compiled form of a user-written function
(the source can be either an XSLT stylesheet function or an XQuery function).
It is assumed that type-checking, of both the arguments and the results, has been handled at compile time. That is, the expression supplied as the body of the function must be wrapped in code to check or convert the result to the required type, and calls on the function must be wrapped at compile time to check or convert the supplied arguments.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a user-defined function (the body must be added later) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Ask whether any of the declared arguments accept nodes without atomizing thematomize()
Atomize the item.call
(XPathContext context, Sequence[] actualArgs) Call this function to return a value.call
(Sequence[] actualArgs, Controller controller) Call this function.void
callUpdating
(Sequence[] actualArgs, XPathContextMajor context, PendingUpdateList pul) Call an updating function.void
Determine the preferred evaluation mode for this functionboolean
Determine whether the function contains tail calls (on this or other functions)boolean
deepEqual40
(FunctionItem other, XPathContext context, DeepEqual.DeepEqualOptions options) boolean
deepEquals
(FunctionItem other, XPathContext context, AtomicComparer comparer, int flags) Test whether this FunctionItem is deep-equal to another function item, under the rules of the deep-equal functionboolean
Get the effective boolean value of this sequencevoid
export
(ExpressionPresenter presenter) Diagnostic print of expression structure.void
gatherProperties
(BiConsumer<String, Object> consumer) Get the properties of this object to be included in trace messages, by supplying the property values to a supplied consumer functionGet the annotations defined on this functiongetArgumentType
(int n) Get the required types of an argument to this functionint
getArity()
Get the arity of this functionGet the evaluation mode.Get the declared result typeGet the declared streamability (XSLT 3.0 attribute)getDefaultValueExpression
(int i) Get the default value expression of the Nth parameter, if anyGet a description of this function for use in error messages.Get the determinism of the function.Get the type of the functiongetFunctionItemType
(int arity) Get the type of the function when invoked with a specific number of argumentsGet the function nameint
Get the size of the value (the number of items)int
Get the minumum arity of this function, that is, the number of mandatory parametersint
Get the number of declared parameters (the upper bound of the arity range)Get a name identifying the object of the expression, for example a function name, template name, variable name, key name, element name, etc.Get the roles of the arguments, for the purposes of streamingGet the definitions of the declared parameters for this functiongetParameterName
(int i) Get the name (keyword) of the Nth parameterint
Get the position in the parameter list of a given parameter nameint
Get the type of value returned by this functionGet the symbolic name of the componentGet a string used to identify this kind of component when used in tracing outputGet the value of the item as a string.head()
Get the first item in the sequence.void
boolean
isArray()
Ask whether this function item is an arrayboolean
Ask whether the function can be inlinedboolean
isMap()
Ask whether this function item is a mapboolean
Ask whether this function is a memo functionboolean
boolean
Determine whether the function contains a tail call, calling itselfboolean
Ask if the function can be trusted to return a result of the correct typeboolean
Ask whether this is an updating function (as defined in XQuery Update)itemAt
(int n) Get the n'th item in the value, counting from 0static Expression[]
makeExpandedArgumentArray
(Expression[] arguments, Map<StructuredQName, Integer> keywords, FunctionDefinition fd) Combine supplied positional and keyword arguments into a single array of positional arguments, completing the array using the default value expression for any unsupplied argumentsmakeNewContext
(XPathContext oldContext, ContextOriginator originator) Create a context for evaluating this functionvoid
Indicate that the function is being inlined, incrementing a count of how many times it has been inlinedvoid
void
process
(XPathContextMajor context, Sequence[] actualArgs, Outputter output) Call this function in "push" mode, writing the results to the current output destination.reduce()
Reduce the sequence to its simplest form.void
setAnnotations
(AnnotationList list) Supply a set of annotationsvoid
setArityRange
(int min, int max) void
Set the evaluation mode.void
setDeclaredStreamability
(FunctionStreamability streamability) Set the declared streamability (XSLT 3.0 attribute)void
setDeterminism
(UserFunction.Determinism determinism) Set the determinism of the function.void
Set the function namevoid
setInlineable
(boolean inlineable) Say whether this function can be inlinedvoid
setIxslUpdating
(boolean isUpdating) Set whether this is an ixsl:updating function, as defined for use with IXSL promises in SaxonJS, allowing the function to have updating side effectsvoid
setMinimumArity
(int minimumArity) void
setOverrideExtensionFunction
(boolean overrideExtensionFunction) void
setParameterDefinitions
(UserFunctionParameter[] params) Set the definitions of the declared parameters for this function.void
setResultType
(SequenceType resultType) Set the declared result type of the functionvoid
setTailRecursive
(boolean tailCalls, boolean recursiveTailCalls) Indicate whether the function contains a tail callvoid
setUpdating
(boolean isUpdating) Set whether this is an updating function (as defined in XQuery Update)subsequence
(int start, int length) Get a subsequence of the valuevoid
typeCheck
(ExpressionVisitor visitor) Methods inherited from class net.sf.saxon.expr.instruct.Actor
allocateAllBindingSlots, allocateBindingSlotsRecursive, getBody, getChildExpression, getColumnNumber, getComponentName, getDeclaredVisibility, getDeclaringComponent, getLineNumber, getLocation, getPackageData, getProperty, getPublicId, getRetainedStaticContext, getStackFrameMap, getSystemId, makeDeclaringComponent, obtainDeclaringComponent, process, saveLocation, setBody, setChildExpression, setColumnNumber, setDeclaredVisibility, setDeclaringComponent, setLineNumber, setPackageData, setRetainedStaticContext, setStackFrameMap, setSystemId
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.sf.saxon.om.FunctionItem
getGenre, isSequenceVariadic, toShortString
Methods inherited from interface net.sf.saxon.om.GroundedValue
asIterable, concatenate, containsNode, materialize
Methods inherited from interface net.sf.saxon.om.Item
getStringValue, isStreamed, iterate
Methods inherited from interface net.sf.saxon.expr.Locatable
getLocation
Methods inherited from interface net.sf.saxon.om.Sequence
makeRepeatable
Methods inherited from interface net.sf.saxon.trace.TraceableComponent
getBody, setBody
-
Field Details
-
bodyEvaluator
-
pushEvaluator
-
-
Constructor Details
-
UserFunction
public UserFunction()Create a user-defined function (the body must be added later)
-
-
Method Details
-
setFunctionName
Set the function name- Parameters:
name
- the function name
-
getFunctionName
Get the function name- Specified by:
getFunctionName
in interfaceFunctionDefinition
- Specified by:
getFunctionName
in interfaceFunctionItem
- Returns:
- the function name, as a StructuredQName. Returns null for an anonymous function
-
getDescription
Get a description of this function for use in error messages. For named functions, the description is the function name (as a lexical QName). For others, it might be, for example, "inline function", or "partially-applied ends-with function".- Specified by:
getDescription
in interfaceFunctionItem
- Returns:
- a description of the function for use in error messages
-
getTracingTag
Description copied from class:Actor
Get a string used to identify this kind of component when used in tracing output- Specified by:
getTracingTag
in interfaceTraceableComponent
- Overrides:
getTracingTag
in classActor
- Returns:
- a diagnostic string used to identify the component when tracing
-
gatherProperties
Get the properties of this object to be included in trace messages, by supplying the property values to a supplied consumer function- Specified by:
gatherProperties
in interfaceTraceable
- Parameters:
consumer
- the function to which the properties should be supplied, as (property name, value) pairs.
-
getObjectName
Get a name identifying the object of the expression, for example a function name, template name, variable name, key name, element name, etc. This is used only where the name is known statically.- Specified by:
getObjectName
in interfaceTraceable
- Returns:
- the QName of the object declared or manipulated by this instruction or expression
-
getSymbolicName
Description copied from class:Actor
Get the symbolic name of the component- Specified by:
getSymbolicName
in classActor
- Returns:
- the symbolic name
-
getFunctionItemType
Get the type of the function- Specified by:
getFunctionItemType
in interfaceFunctionItem
- Returns:
- the function type
-
getFunctionItemType
Get the type of the function when invoked with a specific number of arguments- Returns:
- the function type
-
getOperandRoles
Get the roles of the arguments, for the purposes of streaming- Specified by:
getOperandRoles
in interfaceFunctionItem
- Returns:
- an array of OperandRole objects, one for each argument
-
acceptsNodesWithoutAtomization
public boolean acceptsNodesWithoutAtomization()Ask whether any of the declared arguments accept nodes without atomizing them- Returns:
- true if this is the case
-
isOverrideExtensionFunction
public boolean isOverrideExtensionFunction() -
setOverrideExtensionFunction
public void setOverrideExtensionFunction(boolean overrideExtensionFunction) -
setAnnotations
Supply a set of annotations- Parameters:
list
- the new set of annotations, which will replace any previous annotations on the function
-
getAnnotations
Get the annotations defined on this function- Specified by:
getAnnotations
in interfaceFunctionItem
- Returns:
- the list of annotations defined on this function
-
setDeterminism
Set the determinism of the function. Corresponds to the XSLT 3.0 values new-each-time=yes|no|maybe- Parameters:
determinism
- the determinism value for the function
-
getDeterminism
Get the determinism of the function. Corresponds to the XSLT 3.0 values new-each-time=yes|no|maybe- Returns:
- the determinism value for the function
-
computeEvaluationMode
public void computeEvaluationMode()Determine the preferred evaluation mode for this function -
isInlineable
Ask whether the function can be inlined- Returns:
- true (yes), false (no), or null (don't know)
-
setInlineable
public void setInlineable(boolean inlineable) Say whether this function can be inlined- Parameters:
inlineable
- true or false
-
markAsInlined
public void markAsInlined()Indicate that the function is being inlined, incrementing a count of how many times it has been inlined -
setParameterDefinitions
Set the definitions of the declared parameters for this function. Note that at the time this is called, full analysis of the xsl:param element is yet to be done.- Parameters:
params
- an array of parameter definitions
-
setMinimumArity
public void setMinimumArity(int minimumArity) -
setArityRange
public void setArityRange(int min, int max) -
getParameterDefinitions
Get the definitions of the declared parameters for this function- Returns:
- an array of parameter definitions
-
getMinimumArity
public int getMinimumArity()Get the minumum arity of this function, that is, the number of mandatory parameters- Specified by:
getMinimumArity
in interfaceFunctionDefinition
- Returns:
- the minimum arity
-
setResultType
Set the declared result type of the function- Parameters:
resultType
- the declared return type
-
setTailRecursive
public void setTailRecursive(boolean tailCalls, boolean recursiveTailCalls) Indicate whether the function contains a tail call- Parameters:
tailCalls
- true if the function contains a tail call (on any function)recursiveTailCalls
- true if the function contains a tail call (on itself)
-
containsTailCalls
public boolean containsTailCalls()Determine whether the function contains tail calls (on this or other functions)- Returns:
- true if the function contains tail calls
-
isTailRecursive
public boolean isTailRecursive()Determine whether the function contains a tail call, calling itself- Returns:
- true if the function contains a directly-recursive tail call
-
setUpdating
public void setUpdating(boolean isUpdating) Set whether this is an updating function (as defined in XQuery Update)- Parameters:
isUpdating
- true if this is an updating function
-
isUpdating
public boolean isUpdating()Ask whether this is an updating function (as defined in XQuery Update)- Returns:
- true if this is an updating function
-
setIxslUpdating
public void setIxslUpdating(boolean isUpdating) Set whether this is an ixsl:updating function, as defined for use with IXSL promises in SaxonJS, allowing the function to have updating side effects- Parameters:
isUpdating
- true if this is an updating function
-
setDeclaredStreamability
Set the declared streamability (XSLT 3.0 attribute)- Parameters:
streamability
- the declared streamability (defaults to "unclassified")
-
getDeclaredStreamability
Get the declared streamability (XSLT 3.0 attribute)- Returns:
- the declared streamability (defaults to "unclassified")
-
getResultType
Get the type of value returned by this function- Returns:
- the declared result type, or the inferred result type if this is more precise
-
getDeclaredResultType
Get the declared result type- Returns:
- the declared result type
-
getArgumentType
Get the required types of an argument to this function- Parameters:
n
- identifies the argument in question, starting at 0- Returns:
- a SequenceType object, indicating the required type of the argument
-
getBodyEvaluator
Get the evaluation mode. The evaluation mode will be computed if this has not already been done- Returns:
- the computed evaluation mode
-
setBodyEvaluator
Set the evaluation mode. (Used when reloading a saved package)- Parameters:
mode
- the evaluation mode
-
getArity
public int getArity()Get the arity of this function- Specified by:
getArity
in interfaceFunctionItem
- Returns:
- the number of arguments
-
isMemoFunction
public boolean isMemoFunction()Ask whether this function is a memo function- Returns:
- false (overridden in a subclass)
-
typeCheck
- Throws:
XPathException
-
makeNewContext
Create a context for evaluating this function- Specified by:
makeNewContext
in interfaceFunctionItem
- Parameters:
oldContext
- the existing context of the calleroriginator
- identifies the location of the caller for diagnostics- Returns:
- a new context which should be supplied to the call() method.
-
call
Call this function to return a value.- Specified by:
call
in interfaceCallable
- Parameters:
context
- This provides the run-time context for evaluating the function. This should be created usingFunctionItem.makeNewContext(XPathContext, ContextOriginator)
. It must be an instance of XPathContextMajor.actualArgs
- the arguments supplied to the function. These must have the correct types required by the function signature (it is the caller's responsibility to check this). It is acceptable to supply aClosure
to represent a value whose evaluation will be delayed until it is needed. The array must be the correct size to match the number of arguments: again, it is the caller's responsibility to check this.- Returns:
- a Value representing the result of the function.
- Throws:
XPathException
- if a dynamic error occurs while evaluating the function
-
process
public void process(XPathContextMajor context, Sequence[] actualArgs, Outputter output) throws XPathException Call this function in "push" mode, writing the results to the current output destination.- Parameters:
context
- This provides the run-time context for evaluating the function. It is the caller's responsibility to allocate a "clean" context for the function to use; the context that is provided will be overwritten by the function.actualArgs
- the arguments supplied to the function. These must have the correct types required by the function signature (it is the caller's responsibility to check this). It is acceptable to supply aClosure
to represent a value whose evaluation will be delayed until it is needed. The array must be the correct size to match the number of arguments: again, it is the caller's responsibility to check this.output
- the destination for the result- Throws:
XPathException
- if a dynamic error occurs while evaluating the function
-
call
Call this function. This method allows an XQuery function to be called directly from a Java application. It creates the environment needed to achieve this- Parameters:
actualArgs
- the arguments supplied to the function. These must have the correct types required by the function signature (it is the caller's responsibility to check this). It is acceptable to supply aClosure
to represent a value whose evaluation will be delayed until it is needed. The array must be the correct size to match the number of arguments: again, it is the caller's responsibility to check this.controller
- This provides the run-time context for evaluating the function. A Controller may be obtained by callingXQueryExpression.newController(net.sf.saxon.query.DynamicQueryContext)
. This may be used for a series of calls on functions defined in the same module as the XQueryExpression.- Returns:
- a Value representing the result of the function.
- Throws:
XPathException
- if a dynamic error occurs while evaluating the function.
-
callUpdating
public void callUpdating(Sequence[] actualArgs, XPathContextMajor context, PendingUpdateList pul) throws XPathException Call an updating function.- Parameters:
actualArgs
- the arguments supplied to the function. These must have the correct types required by the function signature (it is the caller's responsibility to check this). It is acceptable to supply aClosure
to represent a value whose evaluation will be delayed until it is needed. The array must be the correct size to match the number of arguments: again, it is the caller's responsibility to check this.context
- the dynamic evaluation contextpul
- the pending updates list, to which the function's update actions are to be added.- Throws:
XPathException
- if a dynamic error occurs while evaluating the function.
-
export
Diagnostic print of expression structure. The abstract expression tree is written to the supplied outputstream.- Specified by:
export
in interfaceFunctionItem
- Specified by:
export
in classActor
- Parameters:
presenter
- the expression presenter used to display the structure- Throws:
XPathException
- if things go wrong, for example an I/O failure
-
isExportable
public boolean isExportable()- Overrides:
isExportable
in classActor
-
isTrustedResultType
public boolean isTrustedResultType()Description copied from interface:FunctionItem
Ask if the function can be trusted to return a result of the correct type- Specified by:
isTrustedResultType
in interfaceFunctionItem
- Returns:
- true if the implementation can be trusted
-
isMap
public boolean isMap()Ask whether this function item is a map- Specified by:
isMap
in interfaceFunctionItem
- Returns:
- true if this function item is a map, otherwise false
-
isArray
public boolean isArray()Ask whether this function item is an array- Specified by:
isArray
in interfaceFunctionItem
- Returns:
- true if this function item is an array, otherwise false
-
deepEquals
public boolean deepEquals(FunctionItem other, XPathContext context, AtomicComparer comparer, int flags) throws XPathException Test whether this FunctionItem is deep-equal to another function item, under the rules of the deep-equal function- Specified by:
deepEquals
in interfaceFunctionItem
- Parameters:
other
- the other function itemcontext
- the dynamic evaluation contextcomparer
- the object to perform the comparisonflags
- options for how the comparison is performed- Returns:
- true if the two function items are deep-equal
- Throws:
XPathException
- if the comparison cannot be performed
-
deepEqual40
public boolean deepEqual40(FunctionItem other, XPathContext context, DeepEqual.DeepEqualOptions options) throws XPathException - Specified by:
deepEqual40
in interfaceFunctionItem
- Throws:
XPathException
-
itemAt
Get the n'th item in the value, counting from 0- Specified by:
itemAt
in interfaceGroundedValue
- Specified by:
itemAt
in interfaceItem
- Parameters:
n
- the index of the required item, with 0 representing the first item in the sequence- Returns:
- the n'th item if it exists, or null otherwise
-
subsequence
Get a subsequence of the value- Specified by:
subsequence
in interfaceGroundedValue
- Specified by:
subsequence
in interfaceItem
- Parameters:
start
- the index of the first item to be included in the result, counting from zero. A negative value is taken as zero. If the value is beyond the end of the sequence, an empty sequence is returnedlength
- the number of items to be included in the result. Specify Integer.MAX_VALUE to get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence is returned. If the value goes off the end of the sequence, the result returns items up to the end of the sequence- Returns:
- the required subsequence.
-
getLength
public int getLength()Get the size of the value (the number of items)- Specified by:
getLength
in interfaceGroundedValue
- Specified by:
getLength
in interfaceItem
- Returns:
- the number of items in the sequence
-
effectiveBooleanValue
Get the effective boolean value of this sequence- Specified by:
effectiveBooleanValue
in interfaceGroundedValue
- Returns:
- the effective boolean value
- Throws:
XPathException
- if the sequence has no effective boolean value (for example a sequence of two integers)
-
reduce
Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance of AtomicValue. If the value is a single item of any other kind, the result will be an instance of One. Otherwise, the result will typically be unchanged.- Specified by:
reduce
in interfaceGroundedValue
- Specified by:
reduce
in interfaceItem
- Returns:
- the simplified sequence
-
head
Get the first item in the sequence. Differs from the superclassSequence
in that no exception is thrown. -
getUnicodeStringValue
Get the value of the item as a string. For nodes, this is the string value of the node as defined in the XPath 2.0 data model, except that all nodes are treated as being untyped: it is not an error to get the string value of a node with a complex type. For atomic values, the method returns the result of casting the atomic value to a string.- Specified by:
getUnicodeStringValue
in interfaceGroundedValue
- Specified by:
getUnicodeStringValue
in interfaceItem
- Returns:
- the string value of the item
- Throws:
UnsupportedOperationException
- if the item is a function item (an unchecked exception is used here to avoid introducing exception handling to a large number of paths where it is not needed)- Since:
- 8.4
-
atomize
Atomize the item.- Specified by:
atomize
in interfaceItem
- Returns:
- the result of atomization
- Throws:
XPathException
- if atomization is not allowed for this kind of item
-
incrementReferenceCount
public void incrementReferenceCount() -
getReferenceCount
public int getReferenceCount() -
prepareForStreaming
- Throws:
XPathException
-
getNumberOfParameters
public int getNumberOfParameters()Get the number of declared parameters (the upper bound of the arity range)- Specified by:
getNumberOfParameters
in interfaceFunctionDefinition
- Returns:
- the number of declared parameters
-
getParameterName
Get the name (keyword) of the Nth parameter- Specified by:
getParameterName
in interfaceFunctionDefinition
- Parameters:
i
- the position of the required parameter- Returns:
- the expression for computing the value of the Nth parameter
-
getDefaultValueExpression
Get the default value expression of the Nth parameter, if any- Specified by:
getDefaultValueExpression
in interfaceFunctionDefinition
- Parameters:
i
- the position of the required parameter- Returns:
- the expression for computing the value of the Nth parameter, or null if there is none
-
getPositionOfParameter
Get the position in the parameter list of a given parameter name- Specified by:
getPositionOfParameter
in interfaceFunctionDefinition
- Parameters:
name
- the name of the required parameter- Returns:
- the position of the parameter in the parameter list, or -1 if absent
-
makeExpandedArgumentArray
public static Expression[] makeExpandedArgumentArray(Expression[] arguments, Map<StructuredQName, Integer> keywords, FunctionDefinition fd) throws XPathExceptionCombine supplied positional and keyword arguments into a single array of positional arguments, completing the array using the default value expression for any unsupplied arguments- Parameters:
arguments
- the expressions supplied in the function call, including both positional and keyword arguments, in the order suppliedkeywords
- a list of keywords used in the function call, with the 0-based position at which each keyword was used; or null if no keywords were usedfd
- the XQuery function definition, used to identify the position in the list of declared parameters at which a particular parameter name appears- Returns:
- the expanded list of positional parameters
- Throws:
XPathException
- in the event of duplicate or undeclared keywords, or keywords that duplicate positional arguments
-