Debian Codegraph @

net/sf/saxon/functions/ApplyFn.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;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExplicitLocation;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RoleDiagnostic;
16 import net.sf.saxon.functions.registry.BuiltInFunctionSet;
17 import net.sf.saxon.ma.arrays.ArrayFunctionSet;
18 import net.sf.saxon.ma.arrays.ArrayItem;
19 import net.sf.saxon.ma.arrays.ArrayItemType;
20 import net.sf.saxon.ma.arrays.SquareArrayConstructor;
21 import net.sf.saxon.ma.map.MapFunctionSet;
22 import net.sf.saxon.ma.map.MapType;
23 import net.sf.saxon.om.*;
24 import net.sf.saxon.trace.ExpressionPresenter;
25 import net.sf.saxon.trans.XPathException;
26 import net.sf.saxon.type.*;
27 import net.sf.saxon.value.SequenceType;
28
29 import java.util.Properties;
30
31 /**
32 * This class implements the function fn:apply(), which is a standard function in XQuery 3.1.
33 * The fn:apply function is also used internally to implement dynamic function calls.
34 */
35
36 public class ApplyFn extends SystemFunction {
37
38 // This flag is nonNull when the fn:apply() call actually implements a dynamic function call.
39 // It only affects the error code/message if the call fails
40 private String dynamicFunctionCall;
41
42 public ApplyFn() {
43 }
44
45 /**
46 * Say that this call to fn:apply was originally written as a dynamic function call
47 * @param fnExpr string representation of the expression used as the dynamic call
48 */
49
50 public void setDynamicFunctionCall(String fnExpr) {
51 dynamicFunctionCall = fnExpr;
52 }
53
54 /**
55 * Ask whether this call to fn:apply was originally written as a dynamic function call
56 *
57 * @return true if it was originally a dynamic function call
58 */
59
60 public boolean isDynamicFunctionCall() {
61 return dynamicFunctionCall != null;
62 }
63
64 /**
65 * Get the return type, given knowledge of the actual arguments
66 *
67 * @param args the actual arguments supplied
68 * @return the best available item type that the function will return
69 */
70 @Override
71 public ItemType getResultItemType(Expression[] args) {
72 // Item type of the result is the same as that of the supplied function
73 ItemType fnType = args[0].getItemType();
74 if (fnType instanceof MapType) {
75 return ((MapType)fnType).getValueType().getPrimaryType();
76 } else if (fnType instanceof ArrayItemType) {
77 return ((ArrayItemType) fnType).getMemberType().getPrimaryType();
78 } else if (fnType instanceof FunctionItemType) {
79 return ((FunctionItemType) fnType).getResultType().getPrimaryType();
80 } else if (fnType instanceof AnyFunctionType) {
81 return AnyItemType.getInstance();
82 } else {
83 return AnyItemType.getInstance();
84 }
85 }
86
87 @Override
88 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
89 if (arguments.length == 2 && arguments[1] instanceof SquareArrayConstructor) {
90 Expression target = arguments[0];
91 if (target.getItemType() instanceof MapType) {
92 // Convert $map($key) to map:get($map, $key)
93 // This improves streamability analysis - see accumulator-053
94 return makeGetCall(visitor, MapFunctionSet.getInstance(), contextInfo, arguments);
95 } else if (target.getItemType() instanceof ArrayItemType) {
96 // Convert $array($key) to array:get($array, $key)
97 return makeGetCall(visitor, ArrayFunctionSet.getInstance(), contextInfo, arguments);
98 }
99 }
100 return null;
101 }
102
103 private Expression makeGetCall(ExpressionVisitor visitor, BuiltInFunctionSet fnSet, ContextItemStaticInfo contextInfo, Expression[] arguments) throws XPathException {
104 Expression target = arguments[0];
105 Expression key = ((SquareArrayConstructor)arguments[1]).getOperanda().getOperand(0).getChildExpression();
106 Expression getter = fnSet.makeFunction("get", 2).makeFunctionCall(target, key);
107 getter.setRetainedStaticContext(target.getRetainedStaticContext());
108 return getter.typeCheck(visitor, contextInfo);
109 }
110
111
112 /**
113 * Evaluate the expression
114 *
115 * @param context the dynamic evaluation context
116 * @param arguments the values of the arguments. The first argument is the function item
117 * to be called; the second argument is an array containing
118 * the arguments to be passed to that function, before conversion.
119 * @return the result of the evaluation, in the form of a SequenceIterator
120 * @throws net.sf.saxon.trans.XPathException
121 * if a dynamic error occurs during the evaluation of the expression
122 */
123 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
124 final Function function = (Function) arguments[0].head();
125 ArrayItem args = (ArrayItem)arguments[1].head();
126
127 if (function.getArity() != args.arrayLength()) {
128 String errorCode = isDynamicFunctionCall() ? "XPTY0004" : "FOAP0001";
129 XPathException err = new XPathException(
130 "Number of arguments required for dynamic call to " + function.getDescription() + " is " + function.getArity() +
131 "; number supplied = " + args.arrayLength(), errorCode);
132 err.setIsTypeError(isDynamicFunctionCall());
133 err.setXPathContext(context);
134 throw err;
135 }
136 TypeHierarchy th = context.getConfiguration().getTypeHierarchy();
137 FunctionItemType fit = function.getFunctionItemType();
138 Sequence[] argArray = new Sequence[args.arrayLength()];
139 if (fit == AnyFunctionType.ANY_FUNCTION) {
140 for (int i = 0; i < argArray.length; i++) {
141 argArray[i] = args.get(i);
142 }
143 } else {
144 for (int i = 0; i < argArray.length; i++) {
145 SequenceType expected = fit.getArgumentTypes()[i];
146 RoleDiagnostic role;
147 if (isDynamicFunctionCall()) {
148 role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "result of " + dynamicFunctionCall, i);
149 } else {
150 role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "fn:apply", i + 1);
151 }
152 Sequence<?> converted = th.applyFunctionConversionRules(
153 args.get(i), expected, role, ExplicitLocation.UNKNOWN_LOCATION);
154 argArray[i] = converted.materialize();
155 }
156 }
157 Sequence<?> rawResult = dynamicCall(function, context, argArray);
158 if (function.isTrustedResultType()) {
159 // trust system functions to return a result of the correct type
160 return rawResult;
161 } else {
162 // Check the result of the function
163 RoleDiagnostic resultRole = new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, "fn:apply", -1);
164 return th.applyFunctionConversionRules(
165 rawResult, fit.getResultType(), resultRole, ExplicitLocation.UNKNOWN_LOCATION);
166 }
167 }
168
169 @Override
170 public void exportAttributes(ExpressionPresenter out) {
171 out.emitAttribute("dyn", dynamicFunctionCall);
172 }
173
174 /**
175 * Import any attributes found in the export file, that is, any attributes output using
176 * the exportAttributes method
177 *
178 * @param attributes the attributes, as a properties object
179 */
180 @Override
181 public void importAttributes(Properties attributes) {
182 dynamicFunctionCall = attributes.getProperty("dyn");
183 }
184 }
185