Debian Codegraph @

net/sf/saxon/functions/registry/OnDemandFunctionSet.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.registry;
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.functions.FunctionLibrary;
14 import net.sf.saxon.om.Function;
15 import net.sf.saxon.trans.SymbolicName;
16 import net.sf.saxon.trans.XPathException;
17
18 import java.util.List;
19
20 /**
21 * The <tt>OnDemandFunctionSet</tt> represents a function library where the implementation classes
22 * are loaded dynamically on demand. The idea is that no failure should occur if implementations
23 * are not available unless the functions are actually required. The class contains the name of
24 * a real FunctionLibrary containing the function implementations; that FunctionLibrary is dynamically
25 * loaded if an attempt is made to bind a function name in the namespace registered with this
26 * class.
27 * <p>This mechanism is currently used for the SQL function library, because the implementations
28 * of these functions are shipped in a separate JAR file (and this is not available on .NET).
29 * If the target function library cannot be loaded, the relevant functions will be reported as
30 * being not available.</p>
31 */
32
33 public class OnDemandFunctionSet implements FunctionLibrary {
34
35 private Configuration config;
36 private String namespace;
37 private String libraryClass;
38 private FunctionLibrary library;
39
40 public OnDemandFunctionSet(Configuration config, String namespace, String libraryClass) {
41 this.config = config;
42 this.namespace = namespace;
43 this.libraryClass = libraryClass;
44 }
45
46 /**
47 * Given a function name, test whether it is in the namespace handled by this
48 * {@code OnDemandFunctionSet}, and if so (and if the library is not already loaded),
49 * attempt to dynamically load the target {@code FunctionLibrary}.
50 * @param functionName the function that is required
51 * @param reasons either null, or a list to which reasons for failure can be added
52 * @return true if the function name is in the namespace recognized by this library.
53 */
54
55 private boolean load(SymbolicName.F functionName, List<String> reasons) {
56 if (functionName.getComponentName().hasURI(namespace)) {
57 if (library == null) {
58 try {
59 Object lib = config.getDynamicLoader().getInstance(libraryClass, null);
60 if (lib instanceof FunctionLibrary) {
61 library = (FunctionLibrary)lib;
62 } else {
63 if (reasons != null) {
64 reasons.add("Class " + libraryClass + " was loaded but it is not a FunctionLibrary");
65 }
66 return false;
67 }
68 } catch (XPathException e) {
69 if (reasons != null) {
70 reasons.add("Failed to load class " + libraryClass + ": " + e.getMessage());
71 }
72 return false;
73 }
74 }
75 library.setConfiguration(config);
76 return true;
77 } else {
78 return false;
79 }
80 }
81
82 @Override
83 public boolean isAvailable(SymbolicName.F functionName) {
84 boolean match = load(functionName, null);
85 return match && library.isAvailable(functionName);
86 }
87
88 @Override
89 public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) {
90 boolean match = load(functionName, reasons);
91 if (match) {
92 return library.bind(functionName, staticArgs, env, reasons);
93 } else {
94 return null;
95 }
96 }
97
98 @Override
99 public FunctionLibrary copy() {
100 return this;
101 }
102
103 //#ifdefined HOF
104 @Override
105 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException {
106 boolean match = load(functionName, null);
107 if (match) {
108 return library.getFunctionItem(functionName, staticContext);
109 } else {
110 return null;
111 }
112 }
113 //#endif
114
115 }
116