Debian Codegraph @

net/sf/saxon/style/StylesheetFunctionLibrary.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.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.expr.parser.ExpressionVisitor;
16 import net.sf.saxon.functions.FunctionLibrary;
17 import net.sf.saxon.om.Function;
18 import net.sf.saxon.trans.SymbolicName;
19 import net.sf.saxon.trans.XPathException;
20
21 import java.util.List;
22
23
24 /**
25 * A StylesheetFunctionLibrary contains functions defined by the user in a stylesheet. This library is used at
26 * compile time only, as it contains references to the actual XSLFunction objects. Binding to a function in this
27 * library registers the function call on a fix-up list to be notified when the actual compiled function becomes
28 * available.
29 */
30
31 public class StylesheetFunctionLibrary implements FunctionLibrary {
32
33 private StylesheetPackage pack;
34 private boolean overrideExtensionFunction;
35
36 /**
37 * Create a FunctionLibrary that provides access to stylesheet functions
38 *
39 * @param sheet The XSLT package
40 * @param overrideExtensionFunction set to true if this library is to contain functions specifying override="yes",
41 * or to false if it is to contain functions specifying override="no". (XSLT uses two instances
42 * of this class, one for overrideExtensionFunction functions and one for non-overrideExtensionFunction functions.)
43 */
44 public StylesheetFunctionLibrary(StylesheetPackage sheet, boolean overrideExtensionFunction) {
45 this.pack = sheet;
46 this.overrideExtensionFunction = overrideExtensionFunction;
47 }
48
49 /**
50 * Ask whether the functions in this library are "overrideExtensionFunction" functions, that is, defined with
51 * xsl:function override="yes".
52 *
53 * @return true if these are overrideExtensionFunction functions, false otherwise
54 */
55
56 public boolean isOverrideExtensionFunction() {
57 return overrideExtensionFunction;
58 }
59
60 /**
61 * Get the stylesheet package to which this function library relates
62 * @return the stylesheet package
63 */
64
65 public StylesheetPackage getStylesheetPackage() {
66 return pack;
67 }
68
69 /**
70 * Bind a function, given the URI and local parts of the function name,
71 * and the list of expressions supplied as arguments. This method is called at compile
72 * time.
73 *
74 * @param functionName The name of the function
75 * @param staticArgs The expressions supplied statically in the function call. The intention is
76 * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
77 * be used as part of the binding algorithm.
78 * @param env The static context
79 * @param reasons In the event that a function cannot be bound, this output parameter may be populated with one
80 * or more diagnostic messages indicating possible reasons why no function binding was possible.
81 * @return An object representing the extension function to be called, if one is found;
82 * null if no extension function was found matching the required name and arity.
83 */
84
85 public Expression bind(SymbolicName.F functionName, Expression[] staticArgs, StaticContext env, List<String> reasons) {
86 UserFunction fn = pack.getFunction(functionName);
87 if (fn == null) {
88 return null;
89 }
90 if (fn.isOverrideExtensionFunction() != this.overrideExtensionFunction) {
91 return null;
92 }
93
94 final UserFunctionCall fc = new UserFunctionCall();
95 fc.setFunctionName(functionName.getComponentName());
96 fc.setArguments(staticArgs);
97 fc.setFunction(fn);
98
99 if (env instanceof ExpressionContext) {
100 // compile-time binding of a static function call in XSLT
101 final PrincipalStylesheetModule psm = ((ExpressionContext) env).getStyleElement().getCompilation().getPrincipalStylesheetModule();
102 final ExpressionVisitor visitor = ExpressionVisitor.make(env);
103 psm.addFixupAction(() -> {
104 if (fc.getFunction() == null) {
105 Component target = psm.getComponent(fc.getSymbolicName());
106 UserFunction fn1 = (UserFunction) target.getActor();
107 if (fn1 != null) {
108 fc.allocateArgumentEvaluators();
109 fc.setStaticType(fn1.getResultType());
110 } else {
111 XPathException err = new XPathException("There is no available function named " + fc.getDisplayName() +
112 " with " + fc.getArity() + " arguments", "XPST0017");
113 err.setLocator(fc.getLocation());
114 throw err;
115 }
116 }
117 });
118 } else {
119 // must be a call within xsl:evaluate
120 }
121
122
123 return fc;
124 }
125
126 //#ifdefined HOF
127
128 /**
129 * Test whether a function with a given name and arity is available; if so, return a function
130 * item that can be dynamically called.
131 * <p>This supports the function-lookup() function in XPath 3.0.</p>
132 *
133 * @param functionName the qualified name of the function being called
134 * @param staticContext the static context to be used by the function, in the event that
135 * it is a system function with dependencies on the static context
136 * @return if a function of this name and arity is available for calling, then a corresponding
137 * function item; or null if the function does not exist
138 * @throws net.sf.saxon.trans.XPathException
139 * in the event of certain errors, for example attempting to get a function
140 * that is private
141 */
142 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException {
143 return pack.getFunction(functionName);
144 }
145 //#endif
146
147
148 /**
149 * Test whether a function with a given name and arity is available
150 * <p>This supports the function-available() function in XSLT.</p>
151 *
152 * @param functionName the qualified name of the function being called
153 * @return true if a function of this name and arity is available for calling
154 */
155 public boolean isAvailable(SymbolicName.F functionName) {
156 return pack.getFunction(functionName) != null;
157 }
158
159 /**
160 * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
161 * new functions to be added, then additions to this copy will not affect the original, or
162 * vice versa.
163 *
164 * @return a copy of this function library. This must be an instance of the original class.
165 */
166
167 public FunctionLibrary copy() {
168 return this;
169 }
170
171 }
172