| 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.functions; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.StaticContext; |
| 12 | import net.sf.saxon.lib.ExtensionFunctionCall; |
| 13 | import net.sf.saxon.lib.ExtensionFunctionDefinition; |
| 14 | import net.sf.saxon.om.Function; |
| 15 | import net.sf.saxon.om.StructuredQName; |
| 16 | import net.sf.saxon.trans.SymbolicName; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * A library of integrated function calls, that is, user-written extension functions implemented |
| 25 | * as instances of the class IntegratedFunction. |
| 26 | */ |
| 27 | public class IntegratedFunctionLibrary implements FunctionLibrary { |
| 28 | |
| 29 | private HashMap<StructuredQName, ExtensionFunctionDefinition> functions = |
| 30 | new HashMap<>(); |
| 31 | |
| 32 | /** |
| 33 | * Register an integrated function with this function library |
| 34 | * |
| 35 | * @param function the implementation of the function (or set of functions) |
| 36 | */ |
| 37 | |
| 38 | public void registerFunction(ExtensionFunctionDefinition function) { |
| 39 | functions.put(function.getFunctionQName(), function); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Bind an extension function, given the URI and local parts of the function name, |
| 44 | * and the list of expressions supplied as arguments. This method is called at compile |
| 45 | * time. |
| 46 | * |
| 47 | * @param functionName the QName and arity of the function being called |
| 48 | * @param staticArgs The expressions supplied statically in arguments to the function call. |
| 49 | * The length of this array represents the arity of the function. The intention is |
| 50 | * that the static type of the arguments (obtainable via getItemType() and getCardinality()) may |
| 51 | * be used as part of the binding algorithm. In some cases it may be possible for the function |
| 52 | * to be pre-evaluated at compile time, for example if these expressions are all constant values. |
| 53 | * <p>The conventions of the XPath language demand that the results of a function depend only on the |
| 54 | * values of the expressions supplied as arguments, and not on the form of those expressions. For |
| 55 | * example, the result of f(4) is expected to be the same as f(2+2). The actual expression is supplied |
| 56 | * here to enable the binding mechanism to select the most efficient possible implementation (including |
| 57 | * compile-time pre-evaluation where appropriate).</p> |
| 58 | * @param env The static context of the function call |
| 59 | * @param reasons If no matching function is found by the function library, it may add |
| 60 | * a diagnostic explanation to this list explaining why none of the available |
| 61 | * functions could be used. |
| 62 | * @return An object representing the function to be called, if one is found; |
| 63 | * null if no function was found matching the required name and arity. |
| 64 | */ |
| 65 | |
| 66 | public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 67 | ExtensionFunctionDefinition defn = functions.get(functionName.getComponentName()); |
| 68 | if (defn == null) { |
| 69 | return null; |
| 70 | } |
| 71 | return makeFunctionCall(defn, staticArgs); |
| 72 | } |
| 73 | |
| 74 | public static Expression makeFunctionCall(ExtensionFunctionDefinition defn, Expression[] staticArgs) { |
| 75 | ExtensionFunctionCall f = defn.makeCallExpression(); |
| 76 | f.setDefinition(defn); |
| 77 | IntegratedFunctionCall fc = new IntegratedFunctionCall(defn.getFunctionQName(), f); |
| 78 | fc.setArguments(staticArgs); |
| 79 | return fc; |
| 80 | } |
| 81 | |
| 82 | //#ifdefined HOF |
| 83 | |
| 84 | /** |
| 85 | * Test whether a function with a given name and arity is available; if so, return a function |
| 86 | * item that can be dynamically called. |
| 87 | * <p>This supports the function-lookup() function in XPath 3.0.</p> |
| 88 | * |
| 89 | * @param functionName the qualified name of the function being called |
| 90 | * @param staticContext the static context to be used by the function, in the event that |
| 91 | * it is a system function with dependencies on the static context |
| 92 | * @return if a function of this name and arity is available for calling, then a corresponding |
| 93 | * function item; or null if the function does not exist |
| 94 | * @throws net.sf.saxon.trans.XPathException |
| 95 | * in the event of certain errors, for example attempting to get a function |
| 96 | * that is private |
| 97 | */ |
| 98 | public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException { |
| 99 | ExtensionFunctionDefinition defn = functions.get(functionName.getComponentName()); |
| 100 | if (defn == null) { |
| 101 | return null; |
| 102 | } |
| 103 | try { |
| 104 | return defn.asFunction(); |
| 105 | // ExtensionFunctionCall f = defn.makeCallExpression(); |
| 106 | // FunctionItemType type = new SpecificFunctionType( |
| 107 | // defn.getArgumentTypes(), defn.getResultType(defn.getArgumentTypes())); |
| 108 | // return new CallableFunction(functionName, f, type); |
| 109 | } catch (Exception err) { |
| 110 | throw new XPathException("Failed to create call to extension function " + functionName.getComponentName().getDisplayName(), err); |
| 111 | } |
| 112 | } |
| 113 | //#endif |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Test whether a function with a given name and arity is available |
| 118 | * <p>This supports the function-available() function in XSLT.</p> |
| 119 | * |
| 120 | * @param functionName the qualified name of the function being called |
| 121 | * @return true if a function of this name and arity is available for calling |
| 122 | */ |
| 123 | public boolean isAvailable(SymbolicName.F functionName) { |
| 124 | ExtensionFunctionDefinition defn = functions.get(functionName.getComponentName()); |
| 125 | int arity = functionName.getArity(); |
| 126 | return defn != null && defn.getMaximumNumberOfArguments() >= arity && defn.getMinimumNumberOfArguments() <= arity; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows |
| 131 | * new functions to be added, then additions to this copy will not affect the original, or |
| 132 | * vice versa. |
| 133 | * |
| 134 | * @return a copy of this function library. This must be an instance of the original class. |
| 135 | */ |
| 136 | |
| 137 | public FunctionLibrary copy() { |
| 138 | IntegratedFunctionLibrary lib = new IntegratedFunctionLibrary(); |
| 139 | lib.functions = new HashMap<>(functions); |
| 140 | return lib; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 |