Debian Codegraph @

net/sf/saxon/functions/FunctionLibraryList.java

raw symbols SCIP
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.Feature;
13 import net.sf.saxon.lib.Logger;
14 import net.sf.saxon.om.Function;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.query.XQueryFunction;
17 import net.sf.saxon.query.XQueryFunctionBinder;
18 import net.sf.saxon.trans.SymbolicName;
19 import net.sf.saxon.trans.XPathException;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * A FunctionLibraryList is a list of FunctionLibraries. It is also a FunctionLibrary in its own right.
26 * When required, it searches the list of FunctionLibraries to find the required function.
27 */
28 public class FunctionLibraryList implements FunctionLibrary, XQueryFunctionBinder {
29
30 public List<FunctionLibrary> libraryList = new ArrayList<>(8);
31
32 public FunctionLibraryList() {
33 }
34
35 /**
36 * Add a new FunctionLibrary to the list of FunctionLibraries in this FunctionLibraryList. Note
37 * that libraries are searched in the order they are added to the list.
38 *
39 * @param lib A function library to be added to the list of function libraries to be searched.
40 * @return the position of the library in the list
41 */
42
43 public int addFunctionLibrary(FunctionLibrary lib) {
44 libraryList.add(lib);
45 return libraryList.size() - 1;
46 }
47
48 /**
49 * Get the n'th function library in the list
50 */
51
52 public FunctionLibrary get(int n) {
53 return libraryList.get(n);
54 }
55
56 //#ifdefined HOF
57
58 /**
59 * Test whether a function with a given name and arity is available; if so, return a function
60 * item that can be dynamically called.
61 * <p>This supports the function-lookup() function in XPath 3.0.</p>
62 *
63 * @param functionName the qualified name of the function being called
64 * @param staticContext the static context to be used by the function, in the event that
65 * it is a system function with dependencies on the static context
66 * @return if a function of this name and arity is available for calling, then a corresponding
67 * function item; or null if the function does not exist
68 */
69 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException {
70 for (FunctionLibrary lib : libraryList) {
71 Function fi = lib.getFunctionItem(functionName, staticContext);
72 if (fi != null) {
73 return fi;
74 }
75 }
76 return null;
77 }
78 //#endif
79
80
81 /**
82 * Test whether a function with a given name and arity is available
83 * <p>This supports the function-available() function in XSLT.</p>
84 *
85 * @param functionName the qualified name of the function being called
86 * @return true if a function of this name and arity is available for calling
87 */
88 public boolean isAvailable(SymbolicName.F functionName) {
89 for (FunctionLibrary lib : libraryList) {
90 if (lib.isAvailable(functionName)) {
91 return true;
92 }
93 }
94 return false;
95 }
96
97 /**
98 * Bind an extension function, given the URI and local parts of the function name,
99 * and the list of expressions supplied as arguments. This method is called at compile
100 * time.
101 *
102 * @param functionName The name of the function
103 * @param staticArgs The expressions supplied statically in arguments to the function call.
104 * The length of this array represents the arity of the function. The intention is
105 * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
106 * be used as part of the binding algorithm. In some cases it may be possible for the function
107 * to be pre-evaluated at compile time, for example if these expressions are all constant values.
108 * @param env The static context
109 * @param reasons If no matching function is found by the function library, it may add
110 * a diagnostic explanation to this list explaining why none of the available
111 * functions could be used.
112 * @return An object representing the extension function to be called, if one is found;
113 * null if no extension function was found matching the required name and arity.
114 */
115
116 public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) {
117 boolean debug = env.getConfiguration().getBooleanProperty(Feature.TRACE_EXTERNAL_FUNCTIONS);
118 Logger err = env.getConfiguration().getLogger();
119 if (debug) {
120 err.info("Looking for function " + functionName.getComponentName().getEQName() + "#" + functionName.getArity());
121 }
122 for (FunctionLibrary lib : libraryList) {
123 if (debug) {
124 err.info("Trying " + lib.getClass().getName());
125 }
126 Expression func = lib.bind(functionName, staticArgs, env, reasons);
127 if (func != null) {
128 return func;
129 }
130 }
131 if (debug) {
132 err.info("Function " + functionName.getComponentName().getEQName() + " not found!");
133 }
134 return null;
135 }
136
137 /**
138 * Get the function declaration corresponding to a given function name and arity
139 *
140 * @return the XQueryFunction if there is one, or null if not.
141 */
142
143 public XQueryFunction getDeclaration(StructuredQName functionName, int staticArgs) {
144 for (FunctionLibrary lib : libraryList) {
145 if (lib instanceof XQueryFunctionBinder) {
146 XQueryFunction func = ((XQueryFunctionBinder) lib).getDeclaration(functionName, staticArgs);
147 if (func != null) {
148 return func;
149 }
150 }
151 }
152 return null;
153 }
154
155 /**
156 * Get the list of contained FunctionLibraries. This method allows the caller to modify
157 * the library list, for example by adding a new FunctionLibrary at a chosen position,
158 * by removing a library from the list, or by changing the order of libraries in the list.
159 * Note that such changes may violate rules in the
160 * language specifications, or assumptions made within the product.
161 *
162 * @return a list whose members are of class FunctionLibrary
163 */
164
165 public List<FunctionLibrary> getLibraryList() {
166 return libraryList;
167 }
168
169 /**
170 * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
171 * new functions to be added, then additions to this copy will not affect the original, or
172 * vice versa.
173 *
174 * @return a copy of this function library. This must be an instance of the original class.
175 */
176
177 public FunctionLibrary copy() {
178 FunctionLibraryList fll = new FunctionLibraryList();
179 fll.libraryList = new ArrayList<>(libraryList.size());
180 for (int i = 0; i < libraryList.size(); i++) {
181 fll.libraryList.add(libraryList.get(i).copy());
182 }
183 return fll;
184 }
185 }