Debian Codegraph @

net/sf/saxon/functions/ScalarSystemFunction.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.SystemFunctionCall;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.One;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.om.ZeroOrOne;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.value.AtomicValue;
19 import net.sf.saxon.value.StringValue;
20
21 /**
22 * A Scalar system function is a pure function that accepts a single item as its operand,
23 * returns a single atomic value as its result, typically returns an empty sequence if the argument is an
24 * empty sequence, and has no context dependencies.
25 */
26
27 public abstract class ScalarSystemFunction extends SystemFunction {
28
29 /**
30 * Abstract method that must be supplied in subclasses to perform the evaluation
31 * @param arg the supplied argument
32 * @param context the dynamic context
33 * @return the result of the evaluation
34 * @throws XPathException if a dynamic error occurs
35 */
36
37 public abstract AtomicValue evaluate(Item arg, XPathContext context) throws XPathException;
38
39 /**
40 * Method that may be supplied in subclasses, to indicate the result that is returned
41 * when an empty sequence is supplied as the argument value. The default is to return the
42 * empty sequence
43 * @return the result of evaluation when the supplied argument is an empty sequence
44 */
45
46 public ZeroOrOne<? extends AtomicValue> resultWhenEmpty() {
47 return ZeroOrOne.empty();
48 }
49
50 /**
51 * Static constant representing a zero-length string
52 */
53
54 public final static One<StringValue> ZERO_LENGTH_STRING = One.string("");
55
56 /**
57 * Evaluate the expression
58 *
59 * @param context the dynamic evaluation context
60 * @param arguments the values of the arguments, supplied as Sequences
61 * @return the result of the evaluation, in the form of a Sequence
62 * @throws net.sf.saxon.trans.XPathException
63 * if a dynamic error occurs during the evaluation of the expression
64 */
65 public final ZeroOrOne<? extends AtomicValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
66 Item val0 = arguments[0].head();
67 if (val0 == null) {
68 return resultWhenEmpty();
69 }
70 return new ZeroOrOne<>(evaluate(val0, context));
71
72 }
73
74 @Override
75 public Expression makeFunctionCall(Expression[] arguments) {
76 SystemFunctionCall call = new SystemFunctionCall(this, arguments) {
77 @Override
78 public AtomicValue evaluateItem(XPathContext context) throws XPathException {
79 // cut out some of the call overhead
80 Item val = getArg(0).evaluateItem(context);
81 if (val == null) {
82 return resultWhenEmpty().head();
83 } else {
84 return evaluate(val, context);
85 }
86 }
87 };
88 call.setRetainedStaticContext(getRetainedStaticContext());
89 return call;
90 }
91 }
92