| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.lib; |
| 9 | |
| 10 | import net.sf.saxon.expr.Callable; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.StaticContext; |
| 13 | import net.sf.saxon.expr.XPathContext; |
| 14 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 15 | import net.sf.saxon.om.Item; |
| 16 | import net.sf.saxon.om.Sequence; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | |
| 19 | /** |
| 20 | * This abstract class is provided to allow user-written extension functions to be implemented |
| 21 | * with the full capabilities of functions that are an intrinsic part of the Saxon product. |
| 22 | * In particular, the class has the opportunity to save data from the static context and |
| 23 | * to optimize itself at compile time. |
| 24 | * <p>Instances of this class are created by calling the method makeCallExpression() on the |
| 25 | * {@link ExtensionFunctionDefinition} object that represents the definition of the function.</p> |
| 26 | * <p>The compiler will create one instance of this class for each function call appearing in the |
| 27 | * expression being compiled. The class must therefore have a public zero-argument constructor.</p> |
| 28 | * <p>The compiler will ensure that the supplied arguments in the extension function call are converted |
| 29 | * if necessary to the declared argument types, by applying the standard conversion rules. The result |
| 30 | * returned by the function is checked against the declared return type, but no conversion takes place: |
| 31 | * the returned value must strictly conform to the declared type.</p> |
| 32 | * <p>Note that an <code>ExtensionFunctionCall</code> is trusted; calls are allowed even if the configuration option |
| 33 | * {@link net.sf.saxon.lib.FeatureKeys#ALLOW_EXTERNAL_FUNCTIONS} is false. In cases where an <code>ExtensionFunctionCall</code> |
| 34 | * is used to load and execute untrusted code, it should check this configuration option before doing so.</p> |
| 35 | * |
| 36 | * @since 9.2; modified in 9.5 to use Sequence rather than SequenceIterator for the arguments and result |
| 37 | */ |
| 38 | public abstract class ExtensionFunctionCall implements Callable { |
| 39 | |
| 40 | ExtensionFunctionDefinition definition; |
| 41 | |
| 42 | /** |
| 43 | * This method is called by the system to provide information about the extension function call. |
| 44 | * It should not be called by the implementor of the extension function. |
| 45 | * |
| 46 | * @param definition the extension function definition |
| 47 | */ |
| 48 | |
| 49 | public final void setDefinition(ExtensionFunctionDefinition definition) { |
| 50 | this.definition = definition; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the definition of this extension function |
| 55 | * |
| 56 | * @return the function definition from which this <code>ExtensionFunctionCall</code> was created |
| 57 | */ |
| 58 | |
| 59 | public final ExtensionFunctionDefinition getDefinition() { |
| 60 | return definition; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Supply static context information. |
| 65 | * <p>This method is called during compilation to provide information about the static context in which |
| 66 | * the function call appears. If the implementation of the function needs information from the static context, |
| 67 | * then it should save it now, as it will not be available later at run-time.</p> |
| 68 | * <p>The implementation also has the opportunity to examine the expressions that appear in the |
| 69 | * arguments to the function call at this stage. These might already have been modified from the original |
| 70 | * expressions as written by the user. The implementation must not modify any of these expressions.</p> |
| 71 | * <p>The default implementation of this method does nothing.</p> |
| 72 | * |
| 73 | * @param context The static context in which the function call appears. The method must not modify |
| 74 | * the static context. |
| 75 | * @param locationId An integer code representing the location of the call to the extension function |
| 76 | * in the stylesheet; can be used in conjunction with the locationMap held in the static context for diagnostics |
| 77 | * @param arguments The XPath expressions supplied in the call to this function. The method must not |
| 78 | * modify this array, or any of the expressions contained in the array. This parameter will |
| 79 | * be null in the case where the supplied arguments are null, that is when binding a |
| 80 | * higher-order function reference or a call on fn:function-lookup(). |
| 81 | * @throws XPathException if the implementation is able to detect a static error in the way the |
| 82 | * function is being called (for example it might require that the types of the arguments are |
| 83 | * consistent with each other). |
| 84 | */ |
| 85 | |
| 86 | public void supplyStaticContext(StaticContext context, int locationId, Expression[] arguments) throws XPathException { |
| 87 | // default implementation does nothing |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Rewrite the function call. This method is called at compile time. It gives the implementation |
| 92 | * an opportunity to replace itself with an optimized implementation that returns the same result. |
| 93 | * This includes the ability to pre-evaluate the function and return its result as a literal value. |
| 94 | * <p>There is also a further opportunity to perform static checking at this stage and to throw an error |
| 95 | * if the arguments are invalid.</p> |
| 96 | * |
| 97 | * @param context The static context in which the function call appears. The method must not modify |
| 98 | * the static context. |
| 99 | * @param arguments The XPath expressions supplied in the call to this function. This method is called |
| 100 | * after type-checking, so these expressions may have been modified by adding atomization operators |
| 101 | * or type-checking operations, for example. |
| 102 | * @return an expression to be evaluated at run-time in place of this function call. Return null |
| 103 | * if no rewriting is required and the function call should be used unchanged. Return a |
| 104 | * {@link net.sf.saxon.expr.Literal} representing the result of the function call if the function call |
| 105 | * can be precomputed at compile time |
| 106 | * @throws XPathException if the implementation is able to detect a static error in the way the |
| 107 | * function is being called (for example it might require that the types of the arguments are |
| 108 | * consistent with each other). |
| 109 | */ |
| 110 | |
| 111 | /*@Nullable*/ |
| 112 | public Expression rewrite(StaticContext context, Expression[] arguments) throws XPathException { |
| 113 | // default implementation does nothing |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Copy local data from one copy of the function to another. This method must be implemented |
| 119 | * in any implementation that maintains local data retained from the static context; the job of the |
| 120 | * method is to copy this local data to the supplied destination function. |
| 121 | * <p>This method is called if a call to the extension function needs to be copied during |
| 122 | * the process of optimization. For example, this occurs if the function containing the call |
| 123 | * to the extension function is inlined.</p> |
| 124 | * <p>If any objects held as local data for the function call are mutable then deep copies must |
| 125 | * be made.</p> |
| 126 | * |
| 127 | * @param destination the function to which the local data must be copied. This will always |
| 128 | * be an instance of the same function class as the source function. |
| 129 | */ |
| 130 | |
| 131 | public void copyLocalData(ExtensionFunctionCall destination) { |
| 132 | // default implementation does nothing |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Evaluate this function call at run-time |
| 137 | * |
| 138 | * @param context The XPath dynamic evaluation context |
| 139 | * @param arguments The values of the arguments to the function call. Each argument value (which is in general |
| 140 | * a sequence) is supplied in the form of <code>Sequence</code>. Any required conversions |
| 141 | * to the declared types of the arguments will already have been performed. |
| 142 | * <p>If the argument is always a singleton, then the single item may be obtained by calling |
| 143 | * <code>arguments[i].head()</code>.</p> |
| 144 | * <p>The implementation is not obliged to read all the items in each <code>SequenceIterator</code> |
| 145 | * if they are not required to compute the result; but if any <code>SequenceIterator</code> is not read |
| 146 | * to completion, it is good practice to call its <code>close()</code> method.</p> |
| 147 | * @return the results of the function as a <code>Sequence</code>. |
| 148 | * <p>The implementation is responsible for ensuring that the result is a valid instance of the declared |
| 149 | * result type. Saxon will check that this is the case if the {@link net.sf.saxon.lib.ExtensionFunctionDefinition#trustResultType()} |
| 150 | * method returns false, but it will never convert the supplied result value to the declared result type.</p> |
| 151 | * <p>The <code>Sequence</code> objects used for the arguments will often be instances of <code>LazySequence</code>, |
| 152 | * which means that the items in the sequence are computed lazily on demand. This means that any errors that occur |
| 153 | * while computing the sequence might not be thrown until the relevant item is actually read from the sequence.</p> |
| 154 | * <p>If the result is a single item, it can be returned directly, since single items all implement <code>Sequence</code>. |
| 155 | * For example a string can be returned as an instance of {@link net.sf.saxon.value.StringValue}, and a boolean as an instance |
| 156 | * of {@link net.sf.saxon.value.BooleanValue}. |
| 157 | * If the result is an empty sequence, the method should return {@link net.sf.saxon.value.EmptySequence#getInstance()}</p> |
| 158 | * @throws XPathException if a dynamic error occurs during evaluation of the function. The Saxon run-time |
| 159 | * code will add information about the error location. |
| 160 | */ |
| 161 | |
| 162 | public abstract Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException; |
| 163 | |
| 164 | /** |
| 165 | * Compute the effective boolean value of the result. |
| 166 | * <p>Implementations can override this method but are not required to do so. If it is overridden, |
| 167 | * the result must be consistent with the rules for calculating effective boolean value. The method |
| 168 | * should be implemented in cases where computing the effective boolean value is significantly cheaper |
| 169 | * than computing the full result.</p> |
| 170 | * |
| 171 | * @param context The XPath dynamic evaluation context |
| 172 | * @param arguments The values of the arguments to the function call. Each argument value (which is in general |
| 173 | * a sequence) is supplied in the form of <code>Sequence</code>. Any required conversions |
| 174 | * to the declared types of the arguments will already have been performed. |
| 175 | * <p>If the argument is always a singleton, then the single item may be obtained by calling |
| 176 | * <code>arguments[i].head()</code>.</p> |
| 177 | * <p>The implementation is not obliged to read all the items in each <code>Sequence</code> |
| 178 | * if they are not required to compute the result; but if any <code>Sequence</code> is not read |
| 179 | * to completion, it is good practice to call <code>close()</code> on the iterator.</p> |
| 180 | * @return the effective boolean value of the result |
| 181 | * @throws XPathException if a dynamic error occurs during evaluation of the function. The Saxon run-time |
| 182 | * code will add information about the error location. |
| 183 | */ |
| 184 | |
| 185 | public boolean effectiveBooleanValue(XPathContext context, Sequence[] arguments) throws XPathException { |
| 186 | return ExpressionTool.effectiveBooleanValue(call(context, arguments).iterate()); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get a streamable implementation of this extension function. This interface is provisional |
| 191 | * and currently only really intended for internal use; it is subject to change. If the value |
| 192 | * returned is non-null, then it must be an instance of com.saxonica.ee.stream.adjunct |
| 193 | * @return the streaming implementation of the extension function |
| 194 | */ |
| 195 | |
| 196 | public Object getStreamingImplementation() { |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | } |
| 202 |