| 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.Configuration; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.StaticContext; |
| 13 | import net.sf.saxon.expr.UserFunctionCall; |
| 14 | import net.sf.saxon.expr.instruct.UserFunction; |
| 15 | import net.sf.saxon.om.Function; |
| 16 | import net.sf.saxon.trans.SymbolicName; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * An ExecutableFunctionLibrary is a function library that contains definitions of functions for use at |
| 25 | * run-time. Normally functions are bound at compile-time; however there are various situations in which |
| 26 | * the information is needed dynamically, for example (a) to support the XSLT function-available() call |
| 27 | * (in the pathological case where the argument is not known statically), (b) to allow functions to be |
| 28 | * called from saxon:evaluate(), (c) to allow functions to be called from a debugging breakpoint. |
| 29 | */ |
| 30 | |
| 31 | public class ExecutableFunctionLibrary implements FunctionLibrary { |
| 32 | |
| 33 | private transient Configuration config; |
| 34 | private HashMap<SymbolicName, UserFunction> functions = new HashMap<>(20); |
| 35 | // The key of the hash table is a String that combines the QName of the function with the arity. |
| 36 | |
| 37 | /** |
| 38 | * Create the ExecutableFunctionLibrary |
| 39 | * |
| 40 | * @param config the Saxon configuration |
| 41 | */ |
| 42 | |
| 43 | public ExecutableFunctionLibrary(Configuration config) { |
| 44 | this.config = config; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Register a function with the function library |
| 49 | * |
| 50 | * @param fn the function to be registered |
| 51 | */ |
| 52 | |
| 53 | public void addFunction(UserFunction fn) { |
| 54 | functions.put(fn.getSymbolicName(), fn); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Bind a function, given the URI and local parts of the function name, |
| 59 | * and the list of expressions supplied as arguments. This method is called at compile |
| 60 | * time. |
| 61 | * |
| 62 | * @param functionName The name of the function to be called |
| 63 | * @param staticArgs The expressions supplied statically in the function call. The intention is |
| 64 | * that the static type of the arguments (obtainable via getItemType() and getCardinality() may |
| 65 | * be used as part of the binding algorithm. |
| 66 | * @param env the static evaluation context |
| 67 | * @param reasons If no matching function is found by the function library, it may add |
| 68 | * a diagnostic explanation to this list explaining why none of the available |
| 69 | * functions could be used. |
| 70 | * @return An object representing the extension function to be called, if one is found; |
| 71 | * null if no extension function was found matching the required name and arity. |
| 72 | */ |
| 73 | |
| 74 | public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 75 | UserFunction fn = functions.get(functionName); |
| 76 | if (fn == null) { |
| 77 | return null; |
| 78 | } |
| 79 | UserFunctionCall fc = new UserFunctionCall(); |
| 80 | fc.setFunctionName(functionName.getComponentName()); |
| 81 | fc.setArguments(staticArgs); |
| 82 | fc.setFunction(fn); |
| 83 | fc.setStaticType(fn.getResultType()); |
| 84 | return fc; |
| 85 | } |
| 86 | |
| 87 | //#ifdefined HOF |
| 88 | |
| 89 | /** |
| 90 | * Test whether a function with a given name and arity is available; if so, return a function |
| 91 | * item that can be dynamically called. |
| 92 | * <p>This supports the function-lookup() function in XPath 3.0.</p> |
| 93 | * |
| 94 | * @param functionName the qualified name of the function being called |
| 95 | * @param staticContext the static context to be used by the function, in the event that |
| 96 | * it is a system function with dependencies on the static context |
| 97 | * @return if a function of this name and arity is available for calling, then a corresponding |
| 98 | * function item; or null if the function does not exist |
| 99 | * @throws net.sf.saxon.trans.XPathException |
| 100 | * in the event of certain errors, for example attempting to get a function |
| 101 | * that is private |
| 102 | */ |
| 103 | public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException { |
| 104 | UserFunction fn = functions.get(functionName); |
| 105 | if (fn != null && fn.isUpdating()) { |
| 106 | throw new XPathException("Cannot bind a function item to an updating function"); |
| 107 | } |
| 108 | return fn; |
| 109 | } |
| 110 | //#endif |
| 111 | |
| 112 | /** |
| 113 | * Test whether a function with a given name and arity is available |
| 114 | * <p>This supports the function-available() function in XSLT.</p> |
| 115 | * |
| 116 | * @param functionName the qualified name of the function being called |
| 117 | * @return true if a function of this name and arity is available for calling |
| 118 | */ |
| 119 | |
| 120 | public boolean isAvailable(SymbolicName.F functionName) { |
| 121 | return functions.get(functionName) != null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows |
| 126 | * new functions to be added, then additions to this copy will not affect the original, or |
| 127 | * vice versa. |
| 128 | * |
| 129 | * @return a copy of this function library. This must be an instance of the original class. |
| 130 | */ |
| 131 | |
| 132 | public FunctionLibrary copy() { |
| 133 | ExecutableFunctionLibrary efl = new ExecutableFunctionLibrary(config); |
| 134 | efl.functions = new HashMap<>(functions); |
| 135 | return efl; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Iterate over all the functions defined in this function library. The objects |
| 140 | * returned by the iterator are of class {@link UserFunction} |
| 141 | * |
| 142 | * @return an iterator delivering the {@link UserFunction} objects representing |
| 143 | * the user-defined functions in a stylesheet or query |
| 144 | */ |
| 145 | |
| 146 | public Iterator<UserFunction> iterateFunctions() { |
| 147 | return functions.values().iterator(); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 |