Debian Codegraph @

net/sf/saxon/functions/ContextItemAccessorFunction.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.type.SpecificFunctionType;
11 import net.sf.saxon.expr.Callable;
12 import net.sf.saxon.expr.ContextItemExpression;
13 import net.sf.saxon.expr.Expression;
14 import net.sf.saxon.expr.XPathContext;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.FunctionItemType;
18 import net.sf.saxon.value.DoubleValue;
19 import net.sf.saxon.value.SequenceType;
20 import net.sf.saxon.value.StringValue;
21
22 /**
23 * A ContextItemAccessorFunction is a function that takes no arguments, but operates implicitly on the
24 * context item. In the case of a dynamic call, the context item that is used is the one at the point
25 * where the function item is created.
26 *
27 * Because these functions depend on the context only, with no additional arguments, the function can
28 * be evaluated as soon as the context is bound. The function is therefore replaced at this stage with a
29 * constant function, that is, one that always returns the same value.
30 */
31
32 public class ContextItemAccessorFunction extends ContextAccessorFunction {
33
34 /**
35 * Bind a context item to appear as part of the function's closure. If this method
36 * has been called, the supplied context item will be used in preference to the
37 * context item at the point where the function is actually called.
38 * @param context the context to which the function applies. Must not be null.
39 */
40
41 public Function bindContext(XPathContext context) throws XPathException {
42 final Item ci = context.getContextItem();
43 if (ci == null) {
44 Callable callable = (context1, arguments) -> {
45 throw new XPathException("Context item for " +
46 getFunctionName().getDisplayName() + " is absent", "XPDY0002");
47 };
48 FunctionItemType fit = new SpecificFunctionType(new SequenceType[]{}, SequenceType.ANY_SEQUENCE);
49 return new CallableFunction(0, callable, fit);
50 }
51 ConstantFunction fn = new ConstantFunction(evaluate(ci, context));
52 fn.setDetails(getDetails());
53 fn.setRetainedStaticContext(getRetainedStaticContext());
54 return fn;
55 }
56
57 /**
58 * Evaluate the function. This is done by creating a function of the same name, with the context item
59 * as an explicit argument, and evaluating that.
60 * @param item the context item
61 * @param context XPath dynamic context (not normally used)
62 * @return the result of the function
63 * @throws XPathException in the event of a dynamic error
64 */
65
66 public GroundedValue evaluate(Item item, XPathContext context) throws XPathException {
67 SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1);
68 return f.call(context, new Sequence[]{item}).materialize();
69 }
70
71 /**
72 * Evaluate the expression. This method is not normally used, but is provided to satisfy the
73 * interface.
74 *
75 * @param context the dynamic evaluation context
76 * @param arguments the values of the arguments, supplied as Sequences
77 * @return the result of the evaluation, in the form of a Sequence
78 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs during the evaluation of the expression
79 */
80 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
81 // Shouldn't be called; but we handle it if it is
82 return evaluate(context.getContextItem(), context);
83 }
84
85 /**
86 * Make a static call on this function, with specified arguments.
87 * @param arguments the supplied arguments to the function call. This will always
88 * be an empty array, since this is a zero-arity function.
89 * @return This implementation returns a call on the equivalent arity-1 version
90 * of the function, supplying "." as an explicit argument.
91 */
92
93 @Override
94 public Expression makeFunctionCall(Expression[] arguments) {
95 Expression arg = new ContextItemExpression();
96 return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), arg);
97 }
98
99 /**
100 * Create a system function call on this function in which the context item
101 * is supplied as an explicit argument
102 * @return an equivalent function call in which the context item is supplied explicitly.
103 */
104
105 public Expression makeContextItemExplicit() {
106 Expression[] args = new Expression[]{new ContextItemExpression()};
107 return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), args);
108 }
109
110 /**
111 * Subclass of ContextItemAccessorFunction to handle string-length() and normalize-space().
112 * These functions differ by taking string(.) rather than (.) as the implicit argument.
113 */
114
115 public static class StringAccessor extends ContextItemAccessorFunction {
116
117 @Override
118 public Expression makeFunctionCall(Expression[] arguments) {
119 Expression ci = new ContextItemExpression();
120 Expression sv = SystemFunction.makeCall("string", getRetainedStaticContext(), ci);
121 return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), sv);
122 }
123
124 @Override
125 public GroundedValue evaluate(Item item, XPathContext context) throws XPathException {
126 SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1);
127 StringValue val = new StringValue(item.getStringValueCS());
128 return f.call(context, new Sequence[]{val}).materialize();
129 }
130
131 }
132
133 /**
134 * Subclass of ContextItemAccessorFunction to handle number().
135 * This function differs by taking data(.) rather than (.) as the implicit argument.
136 */
137
138 public static class Number_0 extends ContextItemAccessorFunction {
139
140 @Override
141 public Expression makeFunctionCall(Expression[] arguments) {
142 Expression ci = new ContextItemExpression();
143 Expression sv = SystemFunction.makeCall("data", getRetainedStaticContext(), ci);
144 return SystemFunction.makeCall(getFunctionName().getLocalPart(), getRetainedStaticContext(), sv);
145 }
146
147 @Override
148 public GroundedValue evaluate(Item item, XPathContext context) throws XPathException {
149 SystemFunction f = SystemFunction.makeFunction(getDetails().name.getLocalPart(), getRetainedStaticContext(), 1);
150 AtomicSequence val = item.atomize();
151 switch (val.getLength()) {
152 case 0:
153 return DoubleValue.NaN;
154 case 1:
155 return f.call(context, new Sequence[]{val.head()}).materialize();
156 default:
157 XPathException err = new XPathException(
158 "When number() is called with no arguments, the atomized value of the context node must " +
159 "not be a sequence of several atomic values", "XPTY0004");
160 err.setIsTypeError(true);
161 throw err;
162 }
163 }
164
165 }
166
167
168 }
169