Debian Codegraph @

net/sf/saxon/functions/DynamicContextAccessor.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.StaticProperty;
12 import net.sf.saxon.expr.SystemFunctionCall;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.BuiltInAtomicType;
18 import net.sf.saxon.value.AtomicValue;
19 import net.sf.saxon.value.DateTimeValue;
20 import net.sf.saxon.value.StringValue;
21
22 /**
23 * A DynamicContextAccessor is a function that takes no arguments, but operates implicitly on the
24 * dynamic context. 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
28 public abstract class DynamicContextAccessor extends SystemFunction {
29
30 private AtomicValue boundValue;
31
32 public void bindContext(XPathContext context) throws XPathException {
33 boundValue = evaluate(context);
34 }
35
36 public abstract AtomicValue evaluate(XPathContext context) throws XPathException;
37
38 /**
39 * Evaluate the expression
40 *
41 * @param context the dynamic evaluation context
42 * @param arguments the values of the arguments, supplied as Sequences
43 * @return the result of the evaluation, in the form of a Sequence
44 * @throws net.sf.saxon.trans.XPathException
45 * if a dynamic error occurs during the evaluation of the expression
46 */
47 public AtomicValue call(XPathContext context, Sequence[] arguments) throws XPathException {
48 if (boundValue != null) {
49 return boundValue;
50 } else {
51 return evaluate(context);
52 }
53 }
54
55 @Override
56 public Expression makeFunctionCall(Expression[] arguments) {
57 return new SystemFunctionCall(this, arguments) {
58 @Override
59 public Item evaluateItem(XPathContext context) throws XPathException {
60 // Cut some of the call overhead
61 return evaluate(context);
62 }
63
64 @Override
65 public int getIntrinsicDependencies() {
66 return StaticProperty.DEPENDS_ON_RUNTIME_ENVIRONMENT;
67 }
68 };
69 }
70
71
72 public static class ImplicitTimezone extends DynamicContextAccessor {
73 @Override
74 public AtomicValue evaluate(XPathContext context) throws XPathException {
75 DateTimeValue now = DateTimeValue.getCurrentDateTime(context);
76 return now.getComponent(AccessorFn.Component.TIMEZONE);
77 }
78 }
79
80 public static class CurrentDateTime extends DynamicContextAccessor {
81 @Override
82 public AtomicValue evaluate(XPathContext context) throws XPathException {
83 return DateTimeValue.getCurrentDateTime(context);
84 }
85 }
86
87 public static class CurrentDate extends DynamicContextAccessor {
88 @Override
89 public AtomicValue evaluate(XPathContext context) throws XPathException {
90 DateTimeValue now = DateTimeValue.getCurrentDateTime(context);
91 return now.toDateValue();
92 }
93 }
94
95 public static class CurrentTime extends DynamicContextAccessor {
96 @Override
97 public AtomicValue evaluate(XPathContext context) throws XPathException {
98 DateTimeValue now = DateTimeValue.getCurrentDateTime(context);
99 return now.toTimeValue();
100 }
101 }
102
103 public static class DefaultLanguage extends DynamicContextAccessor {
104 @Override
105 public AtomicValue evaluate(XPathContext context) throws XPathException {
106 String lang = context.getConfiguration().getDefaultLanguage();
107 return new StringValue(lang, BuiltInAtomicType.LANGUAGE);
108 }
109 }
110
111 }
112