| 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.style; |
| 9 | |
| 10 | import net.sf.saxon.expr.Component; |
| 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.functions.FunctionLibrary; |
| 16 | import net.sf.saxon.om.Function; |
| 17 | import net.sf.saxon.trans.SymbolicName; |
| 18 | import net.sf.saxon.trans.Visibility; |
| 19 | import net.sf.saxon.trans.XPathException; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * A PublicStylesheetFunctionLibrary filters a StylesheetFunctionLibrary to include only those functions |
| 26 | * whose visibility is final or public. Used by xsl:evaluate |
| 27 | */ |
| 28 | |
| 29 | public class PublicStylesheetFunctionLibrary implements FunctionLibrary { |
| 30 | |
| 31 | |
| 32 | |
| 33 | private FunctionLibrary base; |
| 34 | |
| 35 | /** |
| 36 | * Create a FunctionLibrary that provides access to public stylesheet functions |
| 37 | * |
| 38 | * @param base the base function library of which this one is a subset |
| 39 | */ |
| 40 | public PublicStylesheetFunctionLibrary(FunctionLibrary base) { |
| 41 | this.base = base; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Bind a function, given the URI and local parts of the function name, |
| 46 | * and the list of expressions supplied as arguments. This method is called at compile |
| 47 | * time. |
| 48 | * |
| 49 | * @param functionName The name of the function |
| 50 | * @param staticArgs The expressions supplied statically in the function call. The intention is |
| 51 | * that the static type of the arguments (obtainable via getItemType() and getCardinality() may |
| 52 | * be used as part of the binding algorithm. |
| 53 | * @param env The static context |
| 54 | * @param reasons If no matching function is found by the function library, it may add |
| 55 | * a diagnostic explanation to this list explaining why none of the available |
| 56 | * functions could be used. |
| 57 | * @return An object representing the extension function to be called, if one is found; |
| 58 | * null if no extension function was found matching the required name and arity. |
| 59 | * @throws XPathException |
| 60 | * if a function is found with the required name and arity, but |
| 61 | * the implementation of the function cannot be loaded or used; or if an error occurs |
| 62 | * while searching for the function; or if this function library "owns" the namespace containing |
| 63 | * the function call, but no function was found. |
| 64 | */ |
| 65 | |
| 66 | public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) { |
| 67 | Expression baseCall = base.bind(functionName, staticArgs, env, reasons); |
| 68 | if (baseCall instanceof UserFunctionCall) { |
| 69 | Component target = ((UserFunctionCall)baseCall).getTarget(); |
| 70 | Visibility v = target.getVisibility(); |
| 71 | if (v == Visibility.PUBLIC || v == Visibility.FINAL) { |
| 72 | return baseCall; |
| 73 | } else { |
| 74 | reasons.add("The function exists, but does not have public visibility"); |
| 75 | } |
| 76 | } |
| 77 | return null; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | //#ifdefined HOF |
| 82 | |
| 83 | /** |
| 84 | * Test whether a function with a given name and arity is available; if so, return a function |
| 85 | * item that can be dynamically called. |
| 86 | * <p>This supports the function-lookup() function in XPath 3.0.</p> |
| 87 | * |
| 88 | * @param functionName the qualified name of the function being called |
| 89 | * @param staticContext the static context to be used by the function, in the event that |
| 90 | * it is a system function with dependencies on the static context |
| 91 | * @return if a function of this name and arity is available for calling, then a corresponding |
| 92 | * function item; or null if the function does not exist |
| 93 | * @throws XPathException |
| 94 | * in the event of certain errors, for example attempting to get a function |
| 95 | * that is private |
| 96 | */ |
| 97 | public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException { |
| 98 | Function baseFunction = base.getFunctionItem(functionName, staticContext); |
| 99 | if (baseFunction instanceof UserFunction) { |
| 100 | Visibility v = ((UserFunction)baseFunction).getDeclaredVisibility(); |
| 101 | if (v == Visibility.PUBLIC || v == Visibility.FINAL) { |
| 102 | return baseFunction; |
| 103 | } |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | //#endif |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Test whether a function with a given name and arity is available |
| 112 | * <p>This supports the function-available() function in XSLT.</p> |
| 113 | * |
| 114 | * @param functionName the qualified name of the function being called |
| 115 | * @return true if a function of this name and arity is available for calling |
| 116 | */ |
| 117 | public boolean isAvailable(SymbolicName.F functionName) { |
| 118 | if (base instanceof StylesheetFunctionLibrary) { |
| 119 | StylesheetPackage pack = ((StylesheetFunctionLibrary)base).getStylesheetPackage(); |
| 120 | UserFunction fn = pack.getFunction(functionName); |
| 121 | if (fn != null) { |
| 122 | Visibility v = fn.getDeclaredVisibility(); |
| 123 | return v == Visibility.PUBLIC || v == Visibility.FINAL; |
| 124 | } else { |
| 125 | return false; |
| 126 | } |
| 127 | } else { |
| 128 | return base.isAvailable(functionName); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows |
| 134 | * new functions to be added, then additions to this copy will not affect the original, or |
| 135 | * vice versa. |
| 136 | * |
| 137 | * @return a copy of this function library. This must be an instance of the original class. |
| 138 | */ |
| 139 | |
| 140 | public FunctionLibrary copy() { |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | } |
| 145 |