Debian Codegraph @

net/sf/saxon/xpath/XPathFunctionCall.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.xpath;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.*;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.PathMap;
14 import net.sf.saxon.expr.parser.RebindingMap;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.ItemType;
18 import net.sf.saxon.type.Type;
19 import net.sf.saxon.value.EmptySequence;
20
21 import javax.xml.xpath.XPathFunction;
22 import javax.xml.xpath.XPathFunctionException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27 /**
28 * This class is an expression that calls an external function supplied using the
29 * JAXP XPathFunction interface
30 */
31
32 public class XPathFunctionCall extends FunctionCall implements Callable {
33
34 private StructuredQName name;
35 private XPathFunction function;
36
37 /**
38 * Default constructor
39 */
40
41 public XPathFunctionCall(StructuredQName name, XPathFunction function) {
42 this.function = function;
43 }
44
45 /**
46 * Get the qualified name of the function being called
47 *
48 * @return the qualified name
49 */
50 @Override
51 public StructuredQName getFunctionName() {
52 return name;
53 }
54
55 /**
56 * Get the target function to be called
57 *
58 * @param context the dynamic evaluation context
59 * @return always null
60 */
61 @Override
62 public Function getTargetFunction(XPathContext context) {
63 return null;
64 }
65
66 /**
67 * preEvaluate: this method suppresses compile-time evaluation by doing nothing
68 * (because the external function might have side-effects and might use the context)
69 *
70 * @param visitor an expression visitor
71 */
72
73 /*@NotNull*/
74 public Expression preEvaluate(ExpressionVisitor visitor) {
75 return this;
76 }
77
78 /**
79 * Determine which aspects of the context the expression depends on. XPath external
80 * functions are given no access to context information so they cannot have any
81 * dependencies on it.
82 */
83
84 public int getIntrinsicDependencies() {
85 return 0;
86 }
87
88
89 /**
90 * Copy an expression. This makes a deep copy.
91 *
92 * @return the copy of the original expression
93 * @param rebindings variables that need to be re-bound
94 */
95
96 /*@NotNull*/
97 public Expression copy(RebindingMap rebindings) {
98 return new XPathFunctionCall(name, function);
99 }
100
101
102 /**
103 * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited
104 * by an expression in a source tree.
105 * <p>The default implementation of this method assumes that an expression does no navigation other than
106 * the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the
107 * same context as the containing expression. The method must be overridden for any expression
108 * where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression,
109 * and RootExpression (because they perform navigation), and for the doc(), document(), and collection()
110 * functions because they create a new navigation root. Implementations also exist for PathExpression and
111 * FilterExpression because they have subexpressions that are evaluated in a different context from the
112 * calling expression.</p>
113 *
114 * @param pathMap the PathMap to which the expression should be added
115 * @param pathMapNodeSet
116 * @return the pathMapNode representing the focus established by this expression, in the case where this
117 * expression is the first operand of a path expression or filter expression. For an expression that does
118 * navigation, it represents the end of the arc in the path map that describes the navigation route. For other
119 * expressions, it is the same as the input pathMapNode.
120 */
121
122 /*@NotNull*/
123 public PathMap.PathMapNodeSet addToPathMap(/*@NotNull*/ PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
124 return addExternalFunctionCallToPathMap(pathMap, pathMapNodeSet);
125 }
126
127 /**
128 * Evaluate the function. <br>
129 *
130 * @param context The context in which the function is to be evaluated
131 * @return a Value representing the result of the function.
132 * @throws XPathException if the function cannot be evaluated.
133 */
134
135 /*@NotNull*/
136 public SequenceIterator<?> iterate(/*@NotNull*/ XPathContext context) throws XPathException {
137 Sequence[] argValues = new Sequence[getArity()];
138 for (int i = 0; i < argValues.length; i++) {
139 argValues[i] = SequenceTool.toLazySequence(getArg(i).iterate(context));
140 }
141 return call(context, argValues).iterate();
142 }
143
144
145 /**
146 * Call an extension function previously identified using the bind() method. A subclass
147 * can override this method.
148 *
149 * @param context The XPath dynamic context
150 * @param argValues The values of the arguments
151 * @return The value returned by the extension function
152 */
153
154 /*@Nullable*/
155 public Sequence<?> call(/*@NotNull*/ XPathContext context, Sequence[] argValues /*@NotNull*/) throws XPathException {
156 // An argument is supplied to the extension function as a List, unless it is a singleton.
157 // The items within the list are converted to the "natural Java representation", for example
158 // a double is passed as a Double, a string as a String.
159 List<Object> convertedArgs = new ArrayList<>(argValues.length);
160 Configuration config = context.getConfiguration();
161 for (Sequence<?> argValue : argValues) {
162 List<Object> target = new ArrayList<>();
163 argValue.iterate().forEachOrFail(item -> {
164 PJConverter converter = PJConverter.allocate(
165 config, Type.getItemType(item, config.getTypeHierarchy()), StaticProperty.ALLOWS_ONE, Object.class);
166 target.add(converter.convert(item, Object.class, context));
167 });
168 if (target.size() == 1) {
169 convertedArgs.add(target.get(0));
170 } else {
171 convertedArgs.add(target);
172 }
173 }
174
175 try {
176 Object result = function.evaluate(convertedArgs);
177 if (result == null) {
178 return EmptySequence.getInstance();
179 }
180 JPConverter converter = JPConverter.allocate(result.getClass(), null, config);
181 return converter.convert(result, context);
182 } catch (XPathFunctionException e) {
183 throw new XPathException(e);
184 }
185 }
186
187 /**
188 * Determine the data type of the expression, if possible. All expressions return
189 * sequences, in general; this method determines the type of the items within the
190 * sequence, assuming that (a) this is known in advance, and (b) it is the same for
191 * all items in the sequence.
192 * <p>This method will always return a result, though it may be the best approximation
193 * that is available at the time.</p>
194 *
195 * @return the item type
196 */
197
198 /*@NotNull*/
199 public ItemType getItemType() {
200 return Type.ITEM_TYPE;
201 }
202
203 /**
204 * Determine the cardinality of the result
205 *
206 * @return ZERO_OR_MORE (we don't know)
207 */
208 public int computeCardinality() {
209 return StaticProperty.ALLOWS_ZERO_OR_MORE;
210 }
211
212
213
214 }
215