Debian Codegraph @

net/sf/saxon/functions/CallableFunction.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.Callable;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.instruct.UserFunction;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.query.AnnotationList;
17 import net.sf.saxon.query.XQueryFunctionLibrary;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trans.SymbolicName;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.AnyFunctionType;
22 import net.sf.saxon.type.FunctionItemType;
23
24 /**
25 * A function item that wraps a Callable
26 */
27
28 public class CallableFunction extends AbstractFunction {
29
30 private Callable callable;
31 private SymbolicName.F name;
32 private FunctionItemType type;
33 private AnnotationList annotations;
34
35 public CallableFunction(SymbolicName.F name, Callable callable, FunctionItemType type) {
36 this.name = name;
37 this.callable = callable;
38 this.type = type;
39 }
40
41 public CallableFunction(int arity, Callable callable, FunctionItemType type) {
42 this.name = new SymbolicName.F(new StructuredQName("", "anon", "anon"), arity);
43 this.callable = callable;
44 this.type = type;
45 }
46
47 public Callable getCallable() {
48 return callable;
49 }
50
51 public void setCallable(Callable callable) {
52 this.callable = callable;
53 }
54
55 public void setType(FunctionItemType type) {
56 this.type = type;
57 }
58
59 /**
60 * Get the item type of the function item
61 *
62 * @return the function item's type
63 */
64 public FunctionItemType getFunctionItemType() {
65 if (type == AnyFunctionType.getInstance() && callable instanceof XQueryFunctionLibrary.UnresolvedCallable) {
66 UserFunction uf = ((XQueryFunctionLibrary.UnresolvedCallable) callable).getFunction();
67 if (uf != null) {
68 // the previously unresolved function reference is now resolved
69 type = uf.getFunctionItemType();
70 }
71 }
72 return type;
73 }
74
75 /**
76 * Get the name of the function, or null if it is anonymous
77 *
78 * @return the function name, or null for an anonymous inline function
79 */
80 public StructuredQName getFunctionName() {
81 return name.getComponentName();
82 }
83
84 /**
85 * Get a description of this function for use in error messages. For named functions, the description
86 * is the function name (as a lexical QName). For others, it might be, for example, "inline function",
87 * or "partially-applied ends-with function".
88 *
89 * @return a description of the function for use in error messages
90 */
91 public String getDescription() {
92 return callable.toString();
93 }
94
95 /**
96 * Get the arity of the function
97 *
98 * @return the number of arguments in the function signature
99 */
100 public int getArity() {
101 return name.getArity();
102 }
103
104 public void setAnnotations(AnnotationList annotations) {
105 this.annotations = annotations;
106 }
107
108 public AnnotationList getAnnotations() {
109 return annotations;
110 }
111
112 /**
113 * Invoke the function
114 *
115 * @param context the XPath dynamic evaluation context
116 * @param args the actual arguments to be supplied
117 * @return the result of invoking the function
118 * @throws net.sf.saxon.trans.XPathException
119 * if a dynamic error occurs within the function
120 */
121 public Sequence<?> call(XPathContext context, Sequence[] args) throws XPathException {
122 return callable.call(context, args);
123 }
124
125
126 /**
127 * Output information about this function item to the diagnostic explain() output
128 *
129 * @param out the destination for the output
130 */
131 @Override
132 public void export(ExpressionPresenter out) {
133 throw new UnsupportedOperationException("A CallableFunction is a transient value that cannot be exported");
134 }
135 }