Debian Codegraph @

net/sf/saxon/s9api/XdmFunctionItem.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.s9api;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ExplicitLocation;
14 import net.sf.saxon.expr.parser.RoleDiagnostic;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.s9api.streams.Step;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.FunctionItemType;
19 import net.sf.saxon.type.TypeHierarchy;
20
21 import java.util.stream.Stream;
22
23 /**
24 * The class XdmFunctionItem represents a function item
25 */
26
27 public class XdmFunctionItem extends XdmItem {
28
29 protected XdmFunctionItem() {}
30
31 public XdmFunctionItem(Function fi) {
32 setValue(fi);
33 }
34
35 /**
36 * Get the name of the function
37 *
38 * @return the function name, as a QName, or null for an anonymous inline function item
39 */
40
41 public QName getName() {
42 Function fi = (Function) getUnderlyingValue();
43 StructuredQName sq = fi.getFunctionName();
44 return sq == null ? null : new QName(sq);
45 }
46
47 /**
48 * Get the arity of the function
49 *
50 * @return the arity of the function, that is, the number of arguments in the function's signature
51 */
52
53 public int getArity() {
54 Function fi = (Function) getUnderlyingValue();
55 return fi.getArity();
56 }
57
58 /**
59 * Determine whether the item is an atomic value
60 *
61 * @return false, the item is not an atomic value, it is a function item
62 */
63
64 @Override
65 public boolean isAtomicValue() {
66 return false;
67 }
68
69 /**
70 * Get a system function. This can be any function defined in XPath 3.1 functions and operators,
71 * including functions in the math, map, and array namespaces. It can also be a Saxon extension
72 * function, provided a licensed Processor is used.
73 * @return the requested function, or null if there is no such function. Note that some functions
74 * (those with particular context dependencies) may be unsuitable for dynamic calling.
75 * @throws SaxonApiException if dynamic function calls are not permitted by this Saxon Configuration
76 */
77
78 public static XdmFunctionItem getSystemFunction(Processor processor, QName name, int arity) throws SaxonApiException {
79 try {
80 Configuration config = processor.getUnderlyingConfiguration();
81 Function f = config.getSystemFunction(name.getStructuredQName(), arity);
82 return f==null ? null : new XdmFunctionItem(f);
83 } catch (XPathException e) {
84 throw new SaxonApiException(e);
85 }
86 }
87
88 /**
89 * Get an equivalent Java Function object representing this XdmFunction.
90 * This is possible only for arity-1 functions.
91 * @return a Java Function. This takes an XdmValue
92 * as its argument, and returns the function result in the form of an XdmValue.
93 * The Function throws an unchecked exception if evaluation fails
94 * @throws IllegalStateException if the arity of the function is not one (1).
95 */
96
97 public java.util.function.Function<? super XdmValue, ? extends XdmValue> asFunction(Processor processor) {
98 if (getArity() == 1) {
99 return (java.util.function.Function<XdmValue, XdmValue>) arg -> {
100 try {
101 return XdmFunctionItem.this.call(processor, arg);
102 } catch (SaxonApiException e) {
103 throw new SaxonApiUncheckedException(e);
104 }
105 };
106 } else {
107 throw new IllegalStateException("Function arity must be one");
108 }
109 }
110
111 /**
112 * Get an equivalent Step object representing this XdmFunction.
113 * This is possible only for arity-1 functions.
114 *
115 * @return a Step. This takes an XdmItem
116 * as its argument, and returns the function result in the form of an XdmStream.
117 * The Function throws an unchecked exception if evaluation fails
118 * @throws IllegalStateException if the arity of the function is not one (1).
119 */
120
121 public Step<XdmItem> asStep(Processor processor) {
122 if (getArity() == 1) {
123 return new Step<XdmItem>() {
124 public Stream<? extends XdmItem> apply(XdmItem arg) {
125 try {
126 return XdmFunctionItem.this.call(processor, arg).stream();
127 } catch (SaxonApiException e) {
128 throw new SaxonApiUncheckedException(e);
129 }
130 }
131 };
132 } else {
133 throw new IllegalStateException("Function arity must be one");
134 }
135 }
136
137
138 /**
139 * Call the function
140 *
141 * @param arguments the values to be supplied as arguments to the function. The "function
142 * conversion rules" will be applied to convert the arguments to the required
143 * type when necessary.
144 * @param processor the s9api Processor
145 * @return the result of calling the function
146 */
147
148 public XdmValue call(Processor processor, XdmValue... arguments) throws SaxonApiException {
149 if (arguments.length != getArity()) {
150 throw new SaxonApiException("Supplied " + arguments.length + " arguments, required " + getArity());
151 }
152 try {
153 Function fi = (Function) getUnderlyingValue();
154 FunctionItemType type = fi.getFunctionItemType();
155 Sequence<?>[] argVals = new Sequence[arguments.length];
156 TypeHierarchy th = processor.getUnderlyingConfiguration().getTypeHierarchy();
157 for (int i = 0; i < arguments.length; i++) {
158 net.sf.saxon.value.SequenceType required = type.getArgumentTypes()[i];
159 Sequence<?> val = arguments[i].getUnderlyingValue();
160 if (!required.matches(val, th)) {
161 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "", i);
162 val = th.applyFunctionConversionRules(val, required, role, ExplicitLocation.UNKNOWN_LOCATION);
163 }
164 argVals[i] = val;
165 }
166 Configuration config = processor.getUnderlyingConfiguration();
167 Controller controller = new Controller(config);
168 XPathContext context = controller.newXPathContext();
169 context = fi.makeNewContext(context, controller);
170
171 Sequence<?> result = fi.call(context, argVals);
172 if (!fi.isTrustedResultType()) {
173 result = result.materialize();
174 net.sf.saxon.value.SequenceType required = type.getResultType();
175 if (!required.matches(result, th)) {
176 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, "", 0);
177 result = th.applyFunctionConversionRules(result.materialize(), required, role, ExplicitLocation.UNKNOWN_LOCATION);
178 }
179 }
180 Sequence<?> se = result.iterate().materialize();
181 return XdmValue.wrap(se);
182 } catch (XPathException e) {
183 throw new SaxonApiException(e);
184 }
185 }
186
187 }