Debian Codegraph @

net/sf/saxon/query/ImportedFunctionLibrary.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.expr.Expression;
11 import net.sf.saxon.expr.StaticContext;
12 import net.sf.saxon.expr.parser.RetainedStaticContext;
13 import net.sf.saxon.functions.FunctionLibrary;
14 import net.sf.saxon.om.Function;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.trans.SymbolicName;
17 import net.sf.saxon.trans.XPathException;
18
19 import java.util.HashSet;
20 import java.util.List;
21
22 /**
23 * This implementation of FunctionLibrary contains all the functions imported into a Query Module.
24 * It is implemented as a view of the "global" XQueryFunctionLibrary for the whole query, selecting
25 * only those functions that are in an imported namespace.
26 */
27
28 public class ImportedFunctionLibrary implements FunctionLibrary, XQueryFunctionBinder {
29
30 private transient QueryModule importingModule;
31 private XQueryFunctionLibrary baseLibrary;
32 private HashSet<String> namespaces = new HashSet<>(5);
33
34 /**
35 * Create an imported function library
36 *
37 * @param importingModule the module importing the library
38 * @param baseLibrary the function library of which this is a subset view
39 */
40
41 public ImportedFunctionLibrary(QueryModule importingModule, XQueryFunctionLibrary baseLibrary) {
42 this.importingModule = importingModule;
43 this.baseLibrary = baseLibrary;
44 }
45
46 /**
47 * Add an imported namespace
48 *
49 * @param namespace the imported namespace
50 */
51
52 public void addImportedNamespace(String namespace) {
53 namespaces.add(namespace);
54 }
55
56 /**
57 * Bind an extension function, given the URI and local parts of the function name,
58 * and the list of expressions supplied as arguments. This method is called at compile
59 * time.
60 *
61 * @param symbolicName the name of the function to be bound
62 * @param staticArgs The expressions supplied statically in arguments to the function call.
63 * The length of this array represents the arity of the function. 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. In some cases it may be possible for the function
66 * to be pre-evaluated at compile time, for example if these expressions are all constant values.
67 * <p>The conventions of the XPath language demand that the results of a function depend only on the
68 * values of the expressions supplied as arguments, and not on the form of those expressions. For
69 * example, the result of f(4) is expected to be the same as f(2+2). The actual expression is supplied
70 * here to enable the binding mechanism to select the most efficient possible implementation (including
71 * compile-time pre-evaluation where appropriate).</p>
72 * @param env The static context of the function call
73 * @param reasons If no matching function is found by the function library, it may add
74 * a diagnostic explanation to this list explaining why none of the available
75 * functions could be used.
76 * @return An object representing the function to be called, if one is found;
77 * null if no function was found matching the required name and arity.
78 */
79
80 /*@Nullable*/
81 public Expression bind(/*@NotNull*/ SymbolicName.F symbolicName, Expression[] staticArgs, StaticContext env, List<String> reasons) {
82 final StructuredQName functionName = symbolicName.getComponentName();
83 final String uri = functionName.getURI();
84 RetainedStaticContext rsc = new RetainedStaticContext(env);
85 for (Expression arg : staticArgs) {
86 if (arg.getLocalRetainedStaticContext() == null) {
87 arg.setRetainedStaticContext(rsc);
88 }
89 }
90 if (namespaces.contains(uri)) {
91 return baseLibrary.bind(symbolicName, staticArgs, env, reasons);
92 } else {
93 return null;
94 }
95 }
96
97 /**
98 * Get the function declaration corresponding to a given function name and arity
99 *
100 * @return the XQueryFunction if there is one, or null if not.
101 */
102
103 /*@Nullable*/
104 public XQueryFunction getDeclaration(/*@NotNull*/ StructuredQName functionName, int staticArgs) {
105 String uri = functionName.getURI();
106 if (namespaces.contains(uri)) {
107 return baseLibrary.getDeclaration(functionName, staticArgs);
108 } else {
109 return null;
110 }
111 }
112
113 /**
114 * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
115 * new functions to be added, then additions to this copy will not affect the original, or
116 * vice versa.
117 *
118 * @return a copy of this function library. This must be an instance of the original class.
119 */
120
121 /*@NotNull*/
122 public FunctionLibrary copy() {
123 ImportedFunctionLibrary lib = new ImportedFunctionLibrary(importingModule, baseLibrary);
124 for (String ns : namespaces) {
125 lib.addImportedNamespace(ns);
126 }
127 return lib;
128 }
129
130 /**
131 * Set the module that imports this function libary
132 *
133 * @param importingModule the importing module
134 */
135
136 public void setImportingModule(QueryModule importingModule) {
137 this.importingModule = importingModule;
138 }
139
140 //#ifdefined HOF
141
142 /**
143 * Test whether a function with a given name and arity is available; if so, return a function
144 * item that can be dynamically called.
145 * <p>This supports the function-lookup() function in XPath 3.0.</p>
146 *
147 * @param functionName the qualified name of the function being called
148 * @param staticContext the static context to be used by the function, in the event that
149 * it is a system function with dependencies on the static context
150 * @return if a function of this name and arity is available for calling, then a corresponding
151 * function item; or null if the function does not exist
152 * @throws net.sf.saxon.trans.XPathException
153 * in the event of certain errors, for example attempting to get a function
154 * that is private
155 */
156 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException {
157 if (namespaces.contains(functionName.getComponentName().getURI())) {
158 return baseLibrary.getFunctionItem(functionName, staticContext);
159 } else {
160 return null;
161 }
162 }
163 //#endif
164
165
166 /**
167 * Test whether a function with a given name and arity is available
168 * <p>This supports the function-available() function in XSLT.</p>
169 *
170 * @param functionName the qualified name of the function being called
171 * @return true if a function of this name and arity is available for calling
172 */
173 public boolean isAvailable(SymbolicName.F functionName) {
174 return namespaces.contains(functionName.getComponentName().getURI()) && baseLibrary.isAvailable(functionName);
175 }
176 }
177