| 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.XPathContext; |
| 11 | import net.sf.saxon.functions.AbstractFunction; |
| 12 | import net.sf.saxon.om.Function; |
| 13 | import net.sf.saxon.om.Item; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.StructuredQName; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.type.FunctionItemType; |
| 18 | import net.sf.saxon.type.SpecificFunctionType; |
| 19 | import net.sf.saxon.value.SequenceType; |
| 20 | |
| 21 | /** |
| 22 | * This abstract class is provided to allow user-written extension functions to be implemented |
| 23 | * with the full capabilities of functions that are an intrinsic part of the Saxon product. |
| 24 | * In particular, the class has the opportunity to save data from the static context and |
| 25 | * to optimize itself at compile time. |
| 26 | * <p>There should be one class implementing this interface for each function name; if there |
| 27 | * are several functions with the same name but different arity, the same class should implement |
| 28 | * them all.</p> |
| 29 | * <p>Note that an IntegratedFunction is trusted; calls are allowed even if the configuration option |
| 30 | * {@link net.sf.saxon.lib.FeatureKeys#ALLOW_EXTERNAL_FUNCTIONS} is false. In cases where an IntegratedFunction |
| 31 | * is used to load and execute untrusted code, it should check this configuration option before doing so.</p> |
| 32 | * |
| 33 | * @since 9.2 |
| 34 | */ |
| 35 | public abstract class ExtensionFunctionDefinition { |
| 36 | |
| 37 | /** |
| 38 | * Get the name of the function, as a QName. |
| 39 | * <p>This method must be implemented in all subclasses</p> |
| 40 | * |
| 41 | * @return the function name |
| 42 | */ |
| 43 | |
| 44 | public abstract StructuredQName getFunctionQName(); |
| 45 | |
| 46 | /** |
| 47 | * Get the minimum number of arguments required by the function |
| 48 | * <p>The default implementation returns the number of items in the result of calling |
| 49 | * {@link #getArgumentTypes}</p> |
| 50 | * |
| 51 | * @return the minimum number of arguments that must be supplied in a call to this function |
| 52 | */ |
| 53 | |
| 54 | public int getMinimumNumberOfArguments() { |
| 55 | return getArgumentTypes().length; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the maximum number of arguments allowed by the function. |
| 60 | * <p>The default implementation returns the value of {@link #getMinimumNumberOfArguments} |
| 61 | * |
| 62 | * @return the maximum number of arguments that may be supplied in a call to this function |
| 63 | */ |
| 64 | |
| 65 | public int getMaximumNumberOfArguments() { |
| 66 | return getMinimumNumberOfArguments(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the required types for the arguments of this function. |
| 71 | * <p>This method must be implemented in all subtypes.</p> |
| 72 | * |
| 73 | * @return the required types of the arguments, as defined by the function signature. Normally |
| 74 | * this should be an array of size {@link #getMaximumNumberOfArguments()}; however for functions |
| 75 | * that allow a variable number of arguments, the array can be smaller than this, with the last |
| 76 | * entry in the array providing the required type for all the remaining arguments. |
| 77 | */ |
| 78 | |
| 79 | public abstract SequenceType[] getArgumentTypes(); |
| 80 | |
| 81 | /** |
| 82 | * Get the type of the result of the function |
| 83 | * <p>This method must be implemented in all subtypes.</p> |
| 84 | * |
| 85 | * @param suppliedArgumentTypes the static types of the supplied arguments to the function. |
| 86 | * This is provided so that a more precise result type can be returned in the common |
| 87 | * case where the type of the result depends on the types of the arguments. |
| 88 | * @return the return type of the function, as defined by its function signature |
| 89 | */ |
| 90 | |
| 91 | public abstract SequenceType getResultType(SequenceType[] suppliedArgumentTypes); |
| 92 | |
| 93 | /** |
| 94 | * Ask whether the result actually returned by the function can be trusted, |
| 95 | * or whether it should be checked against the declared type. |
| 96 | * |
| 97 | * @return true if the function implementation warrants that the value it returns will |
| 98 | * be an instance of the declared result type. The default value is false, in which case |
| 99 | * the result will be checked at run-time to ensure that it conforms to the declared type. |
| 100 | * If the value true is returned, but the function returns a value of the wrong type, the |
| 101 | * consequences are unpredictable. |
| 102 | */ |
| 103 | |
| 104 | public boolean trustResultType() { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Ask whether the result of the function depends on the focus, or on other variable parts |
| 110 | * of the context. |
| 111 | * |
| 112 | * @return true if the result of the function depends on the context item, position, or size. |
| 113 | * Despite the method name, the method should also return true if the function depends on other |
| 114 | * parts of the context that vary from one part of the query/stylesheet to another, for example |
| 115 | * the XPath default namespace. |
| 116 | * <p>The default implementation returns false.</p> |
| 117 | * <p>The method must return true if the function |
| 118 | * makes use of any of these values from the dynamic context. Returning true inhibits certain |
| 119 | * optimizations, such as moving the function call out of the body of an xsl:for-each loop, |
| 120 | * or extracting it into a global variable.</p> |
| 121 | */ |
| 122 | |
| 123 | public boolean dependsOnFocus() { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Ask whether the function has side-effects. If the function does have side-effects, the optimizer |
| 129 | * will be less aggressive in moving or removing calls to the function. However, calls on functions |
| 130 | * with side-effects can never be guaranteed. |
| 131 | * |
| 132 | * @return true if the function has side-effects (including creation of new nodes, if the |
| 133 | * identity of those nodes is significant). The default implementation returns false. |
| 134 | */ |
| 135 | |
| 136 | public boolean hasSideEffects() { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Create a call on this function. This method is called by the compiler when it identifies |
| 142 | * a function call that calls this function. |
| 143 | * |
| 144 | * @return an expression representing a call of this extension function |
| 145 | */ |
| 146 | |
| 147 | public abstract ExtensionFunctionCall makeCallExpression(); |
| 148 | |
| 149 | /** |
| 150 | * Get a Function item representing this ExtensionFunctionDefinition |
| 151 | * @return a function item corresponding to this extension function |
| 152 | */ |
| 153 | |
| 154 | public final Function asFunction() { |
| 155 | |
| 156 | return new AbstractFunction() { |
| 157 | |
| 158 | /** |
| 159 | * Invoke the function |
| 160 | * |
| 161 | * @param context the XPath dynamic evaluation context |
| 162 | * @param args the actual arguments to be supplied |
| 163 | * @return the result of invoking the function |
| 164 | * @throws XPathException if a dynamic error occurs within the function |
| 165 | */ |
| 166 | @Override |
| 167 | public Sequence<?> call(XPathContext context, Sequence[] args) throws XPathException { |
| 168 | return makeCallExpression().call(context, args); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the item type of the function item |
| 173 | * |
| 174 | * @return the function item's type |
| 175 | */ |
| 176 | @Override |
| 177 | public FunctionItemType getFunctionItemType() { |
| 178 | return new SpecificFunctionType(getArgumentTypes(), getResultType(getArgumentTypes())); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the name of the function, or null if it is anonymous |
| 183 | * |
| 184 | * @return the function name, or null for an anonymous inline function |
| 185 | */ |
| 186 | @Override |
| 187 | public StructuredQName getFunctionName() { |
| 188 | return getFunctionQName(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the arity of the function |
| 193 | * |
| 194 | * @return the number of arguments in the function signature |
| 195 | */ |
| 196 | @Override |
| 197 | public int getArity() { |
| 198 | return getArgumentTypes().length; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get a description of this function for use in error messages. For named functions, the description |
| 203 | * is the function name (as a lexical QName). For others, it might be, for example, "inline function", |
| 204 | * or "partially-applied ends-with function". |
| 205 | * |
| 206 | * @return a description of the function for use in error messages |
| 207 | */ |
| 208 | @Override |
| 209 | public String getDescription() { |
| 210 | return getFunctionQName().getDisplayName(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Ask whether the result of the function should be checked against the declared return type |
| 215 | * @return true if the result does not need to be checked (which can cause catastrophic |
| 216 | * failure if this trust is misplaced) |
| 217 | */ |
| 218 | @Override |
| 219 | public boolean isTrustedResultType() { |
| 220 | return trustResultType(); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | } |
| 225 | |
| 226 | |
| 227 | } |
| 228 |