Debian Codegraph @

net/sf/saxon/query/UnboundFunctionLibrary.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.query;
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.UserFunctionResolvable;
15 import net.sf.saxon.expr.parser.XPathParser;
16 import net.sf.saxon.functions.CallableFunction;
17 import net.sf.saxon.functions.FunctionLibrary;
18 import net.sf.saxon.lib.Feature;
19 import net.sf.saxon.om.Function;
20 import net.sf.saxon.om.StructuredQName;
21 import net.sf.saxon.trans.SymbolicName;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.type.AnyFunctionType;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * An UnboundFunctionLibrary is not a real function library; rather, it is used to keep track of function calls
30 * that cannot yet be bound to a known declared function, but will have to be bound when all user-declared functions
31 * are available.
32 */
33
34 public class UnboundFunctionLibrary implements FunctionLibrary {
35
36 private List<UserFunctionResolvable> unboundFunctionReferences = new ArrayList<>(20);
37 private List<StaticContext> correspondingStaticContext = new ArrayList<>(20);
38 private List<List<String>> correspondingReasons = new ArrayList<>();
39 private boolean resolving = false;
40
41 /**
42 * Create an UnboundFunctionLibrary
43 */
44
45 public UnboundFunctionLibrary() {
46 }
47
48 /**
49 * Identify a (namespace-prefixed) function appearing in the expression. This
50 * method is called by the XQuery parser to resolve function calls found within
51 * the query.
52 * <p>Note that a function call may appear earlier in the query than the definition
53 * of the function to which it is bound. Unlike XSLT, we cannot search forwards to
54 * find the function definition. Binding of function calls is therefore a two-stage
55 * process; at the time the function call is parsed, we simply register it as
56 * pending; subsequently at the end of query parsing all the pending function
57 * calls are resolved. Another consequence of this is that we cannot tell at the time
58 * a function call is parsed whether it is a call to an internal (XSLT or XQuery)
59 * function or to an extension function written in Java.</p>
60 *
61 * @return an Expression representing the function call. This will normally be
62 * a FunctionCall, but it may be rewritten as some other expression.
63 */
64
65 /*@Nullable*/
66 public Expression bind(SymbolicName.F functionName, /*@NotNull*/ Expression[] arguments, StaticContext env, List<String> reasons) {
67 if (resolving) {
68 return null;
69 }
70 if (!reasons.isEmpty() && reasons.get(0).startsWith("Cannot call the private XQuery function")) {
71 // The function call matched a private function in another module; don't attempt a late binding
72 return null;
73 }
74 UserFunctionCall ufc = new UserFunctionCall();
75 ufc.setFunctionName(functionName.getComponentName());
76 ufc.setArguments(arguments);
77 unboundFunctionReferences.add(ufc);
78 correspondingStaticContext.add(env);
79 correspondingReasons.add(reasons);
80 return ufc;
81 }
82
83 //#ifdefined HOF
84
85 /**
86 * Test whether a function with a given name and arity is available; if so, return a function
87 * item that can be dynamically called.
88 * <p>This supports the function-lookup() function in XPath 3.0.</p>
89 *
90 * @param functionName the qualified name of the function being called
91 * @param staticContext the static context to be used by the function, in the event that
92 * it is a system function with dependencies on the static context
93 * @return if a function of this name and arity is available for calling, then a corresponding
94 * function item; or null if the function does not exist
95 */
96 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) {
97 if (resolving) {
98 return null;
99 }
100 XQueryFunctionLibrary.UnresolvedCallable uc = new XQueryFunctionLibrary.UnresolvedCallable(functionName);
101 unboundFunctionReferences.add(uc);
102 correspondingStaticContext.add(null);
103 correspondingReasons.add(new ArrayList<>());
104 CallableFunction fi = new CallableFunction(functionName, uc, AnyFunctionType.getInstance());
105 //uc.setFunctionItem(fi);
106 return fi;
107 }
108 //#endif
109
110
111 /**
112 * Test whether a function with a given name and arity is available
113 * <p>This supports the function-available() function in XSLT.</p>
114 *
115 * @param functionName the qualified name of the function being called
116 * @return true if a function of this name and arity is available for calling
117 */
118 public boolean isAvailable(SymbolicName.F functionName) {
119 return false; // function-available() is not used in XQuery
120 }
121
122 /**
123 * Bind function calls that could not be bound when first encountered. These
124 * will either be forwards references to functions declared later in the query,
125 * or errors. This method is for internal use.
126 *
127 * @param lib A library containing all the XQuery functions that have been declared;
128 * the method searches this library for this missing function call
129 * @param config The Saxon configuration
130 * @throws XPathException if a function call refers to a function that has
131 * not been declared
132 */
133
134 public void bindUnboundFunctionReferences(/*@NotNull*/ XQueryFunctionBinder lib, /*@NotNull*/ Configuration config) throws XPathException {
135 resolving = true;
136 for (int i = 0; i < unboundFunctionReferences.size(); i++) {
137 UserFunctionResolvable ref = unboundFunctionReferences.get(i);
138 if (ref instanceof UserFunctionCall) {
139 UserFunctionCall ufc = (UserFunctionCall) ref;
140 QueryModule importingModule = (QueryModule) correspondingStaticContext.get(i);
141 if (importingModule == null) {
142 // means we must have already been here
143 continue;
144 }
145 correspondingStaticContext.set(i, null); // for garbage collection purposes
146 // The original UserFunctionCall is effectively a dummy: we weren't able to find a function
147 // definition at the time. So we try again.
148 final StructuredQName q = ufc.getFunctionName();
149 final int arity = ufc.getArity();
150
151 XQueryFunction fd = lib.getDeclaration(q, arity);
152 if (fd != null) {
153 fd.registerReference(ufc);
154 ufc.setStaticType(fd.getResultType());
155 } else {
156 StringBuilder sb = new StringBuilder("Cannot find a " + arity +
157 "-argument function named " + q.getEQName() + "()");
158 List<String> reasons = correspondingReasons.get(i);
159 for (String reason : reasons) {
160 sb.append(". ").append(reason);
161 }
162 if (reasons.isEmpty()) {
163 String supplementary = XPathParser.getMissingFunctionExplanation(q, config);
164 if (supplementary != null) {
165 sb.append(". ").append(supplementary);
166 }
167 }
168 XPathException err = new XPathException(sb.toString(), "XPST0017", ufc.getLocation());
169 err.setIsStaticError(true);
170 throw err;
171 }
172 } else if (ref instanceof XQueryFunctionLibrary.UnresolvedCallable) {
173 XQueryFunctionLibrary.UnresolvedCallable uc = (XQueryFunctionLibrary.UnresolvedCallable) ref;
174 final StructuredQName q = uc.getFunctionName();
175 final int arity = uc.getArity();
176
177 XQueryFunction fd = lib.getDeclaration(q, arity);
178 if (fd != null) {
179 fd.registerReference(uc);
180 //uc.setStaticType(fd.getResultType());
181 // Check that the result type and all the argument types are in the static context of the
182 // calling module
183 //importingModule.checkImportedFunctionSignature(fd);
184 } else {
185 String msg = "Cannot find a " + arity +
186 "-argument function named " + q.getEQName() + "()";
187 if (!config.getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) {
188 msg += ". Note: external function calls have been disabled";
189 }
190 XPathException err = new XPathException(msg);
191 err.setErrorCode("XPST0017");
192 err.setIsStaticError(true);
193 throw err;
194 }
195 }
196 }
197 }
198
199 /**
200 * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
201 * new functions to be added, then additions to this copy will not affect the original, or
202 * vice versa.
203 *
204 * @return a copy of this function library. This must be an instance of the original class.
205 */
206
207 /*@NotNull*/
208 public FunctionLibrary copy() {
209 UnboundFunctionLibrary qfl = new UnboundFunctionLibrary();
210 qfl.unboundFunctionReferences = new ArrayList<>(unboundFunctionReferences);
211 qfl.correspondingStaticContext = new ArrayList<>(correspondingStaticContext);
212 qfl.resolving = resolving;
213 return qfl;
214 }
215
216 }
217