| 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.xpath; |
| 9 | |
| 10 | import net.sf.saxon.functions.CallableFunction; |
| 11 | import net.sf.saxon.type.SpecificFunctionType; |
| 12 | import net.sf.saxon.expr.Expression; |
| 13 | import net.sf.saxon.expr.StaticContext; |
| 14 | import net.sf.saxon.functions.FunctionLibrary; |
| 15 | import net.sf.saxon.om.Function; |
| 16 | import net.sf.saxon.om.StructuredQName; |
| 17 | import net.sf.saxon.trans.SymbolicName; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.type.FunctionItemType; |
| 20 | import net.sf.saxon.value.SequenceType; |
| 21 | |
| 22 | import javax.xml.namespace.QName; |
| 23 | import javax.xml.xpath.XPathFunction; |
| 24 | import javax.xml.xpath.XPathFunctionResolver; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * The XPathFunctionLibrary is a FunctionLibrary that supports binding of XPath function |
| 30 | * calls to instances of the JAXP XPathFunction interface returned by an XPathFunctionResolver. |
| 31 | */ |
| 32 | |
| 33 | public class XPathFunctionLibrary implements FunctionLibrary { |
| 34 | |
| 35 | private XPathFunctionResolver resolver; |
| 36 | |
| 37 | /** |
| 38 | * Construct a XPathFunctionLibrary |
| 39 | */ |
| 40 | |
| 41 | public XPathFunctionLibrary() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the resolver |
| 46 | * |
| 47 | * @param resolver The XPathFunctionResolver wrapped by this FunctionLibrary |
| 48 | */ |
| 49 | |
| 50 | public void setXPathFunctionResolver(XPathFunctionResolver resolver) { |
| 51 | this.resolver = resolver; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the resolver |
| 56 | * |
| 57 | * @return the XPathFunctionResolver wrapped by this FunctionLibrary |
| 58 | */ |
| 59 | |
| 60 | public XPathFunctionResolver getXPathFunctionResolver() { |
| 61 | return resolver; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Bind a function, given the URI and local parts of the function name, |
| 66 | * and the list of expressions supplied as arguments. This method is called at compile |
| 67 | * time. |
| 68 | * |
| 69 | * @param functionName The name of the function |
| 70 | * @param staticArgs The expressions supplied statically in the function call. The intention is |
| 71 | * that the static type of the arguments (obtainable via getItemType() and getCardinality() may |
| 72 | * be used as part of the binding algorithm. |
| 73 | * @param env The static context |
| 74 | * @param reasons If no matching function is found by the function library, it may add |
| 75 | * a diagnostic explanation to this list explaining why none of the available |
| 76 | * functions could be used. |
| 77 | * @return An object representing the extension function to be called, if one is found; |
| 78 | * null if no extension function was found matching the required name, arity, or signature. |
| 79 | */ |
| 80 | |
| 81 | /*@Nullable*/ |
| 82 | public Expression bind(/*@NotNull*/ SymbolicName.F functionName, /*@NotNull*/ Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 83 | if (resolver == null) { |
| 84 | return null; |
| 85 | } |
| 86 | StructuredQName qn = functionName.getComponentName(); |
| 87 | QName name = new QName(qn.getURI(), qn.getLocalPart()); |
| 88 | XPathFunction function = resolver.resolveFunction(name, functionName.getArity()); |
| 89 | if (function == null) { |
| 90 | return null; |
| 91 | } |
| 92 | XPathFunctionCall fc = new XPathFunctionCall(qn, function); |
| 93 | fc.setArguments(staticArgs); |
| 94 | return fc; |
| 95 | } |
| 96 | |
| 97 | //#ifdefined HOF |
| 98 | |
| 99 | /** |
| 100 | * Test whether a function with a given name and arity is available; if so, return a function |
| 101 | * item that can be dynamically called. |
| 102 | * <p>This supports the function-lookup() function in XPath 3.0.</p> |
| 103 | * |
| 104 | * @param symbolicName the qualified name of the function being called |
| 105 | * @param staticContext the static context to be used by the function, in the event that |
| 106 | * it is a system function with dependencies on the static context |
| 107 | * @return if a function of this name and arity is available for calling, then a corresponding |
| 108 | * function item; or null if the function does not exist |
| 109 | * @throws net.sf.saxon.trans.XPathException |
| 110 | * in the event of certain errors, for example attempting to get a function |
| 111 | * that is private |
| 112 | */ |
| 113 | public Function getFunctionItem(SymbolicName.F symbolicName, StaticContext staticContext) throws XPathException { |
| 114 | if (resolver == null) { |
| 115 | return null; |
| 116 | } |
| 117 | StructuredQName functionName = symbolicName.getComponentName(); |
| 118 | int arity = symbolicName.getArity(); |
| 119 | QName name = new QName(functionName.getURI(), functionName.getLocalPart()); |
| 120 | XPathFunction function = resolver.resolveFunction(name, arity); |
| 121 | if (function == null) { |
| 122 | return null; |
| 123 | } |
| 124 | XPathFunctionCall functionCall = new XPathFunctionCall(functionName, function); |
| 125 | SequenceType[] argTypes = new SequenceType[arity]; |
| 126 | Arrays.fill(argTypes, SequenceType.ANY_SEQUENCE); |
| 127 | FunctionItemType functionType = new SpecificFunctionType(argTypes, SequenceType.ANY_SEQUENCE); |
| 128 | return new CallableFunction(symbolicName, functionCall, functionType); |
| 129 | } |
| 130 | //#endif |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Test whether a function with a given name and arity is available |
| 135 | * <p>This supports the function-available() function in XSLT.</p> |
| 136 | * |
| 137 | * @param functionName the qualified name of the function being called |
| 138 | * @return true if a function of this name and arity is available for calling |
| 139 | */ |
| 140 | public boolean isAvailable(SymbolicName.F functionName) { |
| 141 | return resolver != null && |
| 142 | resolver.resolveFunction( |
| 143 | new QName(functionName.getComponentName().getURI(), functionName.getComponentName().getLocalPart()), |
| 144 | functionName.getArity()) != null; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows |
| 149 | * new functions to be added, then additions to this copy will not affect the original, or |
| 150 | * vice versa. |
| 151 | * |
| 152 | * @return a copy of this function library. This must be an instance of the original class. |
| 153 | */ |
| 154 | |
| 155 | /*@NotNull*/ |
| 156 | public FunctionLibrary copy() { |
| 157 | XPathFunctionLibrary xfl = new XPathFunctionLibrary(); |
| 158 | xfl.resolver = resolver; |
| 159 | return xfl; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | } |
| 164 |