Debian Codegraph @

net/sf/saxon/functions/registry/ConstructorFunctionLibrary.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 com.saxonica.functions.hof.AtomicConstructorFunction;
11 import net.sf.saxon.Configuration;
12 import net.sf.saxon.expr.*;
13 import net.sf.saxon.functions.CallableFunction;
14 import net.sf.saxon.functions.FunctionLibrary;
15 import net.sf.saxon.lib.NamespaceConstant;
16 import net.sf.saxon.om.Function;
17 import net.sf.saxon.om.NamespaceResolver;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trans.SymbolicName;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.*;
22 import net.sf.saxon.value.AtomicValue;
23 import net.sf.saxon.value.EmptySequence;
24 import net.sf.saxon.value.SequenceType;
25
26 import java.util.List;
27
28 /**
29 * The ConstructorFunctionLibrary represents the collection of constructor functions for atomic types. These
30 * are provided for the built-in types such as xs:integer and xs:date, and also for user-defined atomic types.
31 */
32
33 public class ConstructorFunctionLibrary implements FunctionLibrary {
34
35 private Configuration config;
36
37 /**
38 * Create a SystemFunctionLibrary
39 *
40 * @param config the Configuration
41 */
42
43 public ConstructorFunctionLibrary(Configuration config) {
44 this.config = config;
45 }
46
47 //#ifdefined HOF
48
49 /**
50 * Test whether a function with a given name and arity is available; if so, return a function
51 * item that can be dynamically called.
52 * <p>This supports the function-lookup() function in XPath 3.0.</p>
53 *
54 * @param functionName the qualified name of the function being called
55 * @param staticContext the static context to be used by the function, in the event that
56 * it is a system function with dependencies on the static context
57 * @return if a function of this name and arity is available for calling, then a corresponding
58 * function item; or null if the function does not exist
59 * @throws net.sf.saxon.trans.XPathException
60 * in the event of certain errors, for example attempting to get a function
61 * that is private
62 */
63 public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException {
64 if (functionName.getArity() != 1) {
65 return null;
66 }
67 final String uri = functionName.getComponentName().getURI();
68 final String localName = functionName.getComponentName().getLocalPart();
69 final SchemaType type = config.getSchemaType(new StructuredQName("", uri, localName));
70 if (type == null || type.isComplexType()) {
71 return null;
72 }
73 final NamespaceResolver resolver = ((SimpleType) type).isNamespaceSensitive() ? staticContext.getNamespaceResolver() : null;
74 if (type instanceof AtomicType) {
75 return new AtomicConstructorFunction((AtomicType) type, resolver);
76 } else if (type instanceof ListType) {
77 return new ListConstructorFunction((ListType)type, resolver, true);
78 } else {
79 Callable callable = (context, arguments) -> {
80 AtomicValue value = (AtomicValue) arguments[0].head();
81 if (value == null) {
82 return EmptySequence.getInstance();
83 }
84 return UnionConstructorFunction.cast(value, (UnionType) type, resolver, context.getConfiguration().getConversionRules());
85 };
86 SequenceType returnType = ((UnionType) type).getResultTypeOfCast();
87 return new CallableFunction(1, callable,
88 new SpecificFunctionType(new SequenceType[]{SequenceType.OPTIONAL_ATOMIC}, returnType));
89 }
90 }
91
92 //#endif
93
94
95 public boolean isAvailable(SymbolicName.F functionName) {
96 if (functionName.getArity() != 1) {
97 return false;
98 }
99 final String uri = functionName.getComponentName().getURI();
100 final String localName = functionName.getComponentName().getLocalPart();
101 final SchemaType type = config.getSchemaType(new StructuredQName("", uri, localName));
102 if (type == null || type.isComplexType()) {
103 return false;
104 }
105 if (type.isAtomicType() && ((AtomicType) type).isAbstract()) {
106 return false;
107 }
108 return type != AnySimpleType.getInstance();
109 }
110
111 /**
112 * Bind a static function call, given the URI and local parts of the function name,
113 * and the list of expressions supplied as arguments. This method is called at compile
114 * time.
115 *
116 * @param functionName The QName of the function
117 * @param arguments The expressions supplied statically in the function call. The intention is
118 * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
119 * be used as part of the binding algorithm.
120 * @param env The static context
121 * @param reasons If no matching function is found by the function library, it may add
122 * a diagnostic explanation to this list explaining why none of the available
123 * functions could be used.
124 * @return An object representing the constructor function to be called, if one is found;
125 * null if no constructor function was found matching the required name and arity.
126 * @throws net.sf.saxon.trans.XPathException
127 * if a function is found with the required name and arity, but
128 * the implementation of the function cannot be loaded or used; or if an error occurs
129 * while searching for the function; or if this function library "owns" the namespace containing
130 * the function call, but no function was found.
131 */
132
133 public Expression bind(SymbolicName.F functionName, Expression[] arguments, StaticContext env, List<String> reasons) {
134 final String uri = functionName.getComponentName().getURI();
135 final String localName = functionName.getComponentName().getLocalPart();
136 boolean builtInNamespace = uri.equals(NamespaceConstant.SCHEMA);
137 if (builtInNamespace) {
138 // it's a constructor function: treat it as shorthand for a cast expression
139 if (functionName.getArity() != 1) {
140 reasons.add("A constructor function must have exactly one argument");
141 return null;
142 }
143 SimpleType type = Type.getBuiltInSimpleType(uri, localName);
144 if (type != null) {
145 if (type.isAtomicType()) {
146 if (((AtomicType) type).isAbstract()) {
147 reasons.add("Abstract type used in constructor function: {" + uri + '}' + localName);
148 return null;
149 } else {
150 CastExpression cast = new CastExpression(arguments[0], (AtomicType) type, true);
151 if (arguments[0] instanceof StringLiteral) {
152 cast.setOperandIsStringLiteral(true);
153 }
154 return cast;
155 }
156 } else if (type.isUnionType()) {
157 NamespaceResolver resolver = env.getNamespaceResolver();
158 UnionConstructorFunction ucf = new UnionConstructorFunction((UnionType) type, resolver, true);
159 return new StaticFunctionCall(ucf, arguments);
160 } else {
161 NamespaceResolver resolver = env.getNamespaceResolver();
162 try {
163 ListConstructorFunction lcf = new ListConstructorFunction((ListType)type, resolver, true);
164 return new StaticFunctionCall(lcf, arguments);
165 } catch (MissingComponentException e) {
166 reasons.add("Missing schema component: " + e.getMessage());
167 return null;
168 }
169 }
170 } else {
171 reasons.add("Unknown constructor function: {" + uri + '}' + localName);
172 return null;
173 }
174
175 }
176
177 // Now see if it's a constructor function for a user-defined type
178
179 if (arguments.length == 1) {
180 SchemaType st = config.getSchemaType(new StructuredQName("", uri, localName));
181 if (st instanceof SimpleType) {
182 if (st instanceof AtomicType) {
183 return new CastExpression(arguments[0], (AtomicType) st, true);
184 } else if (st instanceof ListType && env.getXPathVersion() >= 30) {
185 NamespaceResolver resolver = env.getNamespaceResolver();
186 try {
187 ListConstructorFunction lcf = new ListConstructorFunction((ListType) st, resolver, true);
188 return new StaticFunctionCall(lcf, arguments);
189 } catch (MissingComponentException e) {
190 reasons.add("Missing schema component: " + e.getMessage());
191 return null;
192 }
193 } else if (((SimpleType) st).isUnionType() && env.getXPathVersion() >= 30) {
194 NamespaceResolver resolver = env.getNamespaceResolver();
195 UnionConstructorFunction ucf = new UnionConstructorFunction((UnionType) st, resolver, true);
196 return new StaticFunctionCall(ucf, arguments);
197 }
198 }
199 }
200
201 return null;
202 }
203
204 /**
205 * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
206 * new functions to be added, then additions to this copy will not affect the original, or
207 * vice versa.
208 *
209 * @return a copy of this function library. This must be an instance of the original class.
210 */
211
212 public FunctionLibrary copy() {
213 return this;
214 }
215
216 }