Debian Codegraph @

net/sf/saxon/functions/AccumulatorFn.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.XPathContext;
11 import net.sf.saxon.expr.accum.Accumulator;
12 import net.sf.saxon.expr.accum.AccumulatorManager;
13 import net.sf.saxon.expr.accum.AccumulatorRegistry;
14 import net.sf.saxon.expr.accum.IAccumulatorData;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.trans.XsltController;
18 import net.sf.saxon.type.Type;
19
20 /**
21 * Non-streaming implementation of accumulator-before() and accumulator-after()
22 */
23 public abstract class AccumulatorFn extends SystemFunction {
24
25 public enum Phase {AFTER, BEFORE}
26
27 public abstract Phase getPhase();
28
29
30 private Sequence<?> getAccumulatorValue(String name, Phase phase, XPathContext context) throws XPathException {
31 StructuredQName qName;
32 try {
33 qName = StructuredQName.fromLexicalQName(name, false, true, getRetainedStaticContext());
34 } catch (XPathException err) {
35 throw new XPathException("Invalid accumulator name: " + err.getMessage(), "XTDE3340", context);
36 }
37 AccumulatorRegistry registry = getRetainedStaticContext().getPackageData().getAccumulatorRegistry();
38 Accumulator accumulator = registry == null ? null : registry.getAccumulator(qName);
39 if (accumulator == null) {
40 throw new XPathException("Accumulator " + name + " has not been declared", "XTDE3340");
41 }
42
43 Item node = context.getContextItem();
44 if (node == null) {
45 throw new XPathException("No context item for evaluation of accumulator function", "XTDE3350", context);
46 }
47 if (!(node instanceof NodeInfo)) {
48 throw new XPathException("Context item for evaluation of accumulator function must be a node", "XTTE3360", context);
49 }
50 int kind = ((NodeInfo)node).getNodeKind();
51 if (kind == Type.ATTRIBUTE || kind == Type.NAMESPACE) {
52 throw new XPathException("Context item for evaluation of accumulator function must not be an attribute or namespace node", "XTTE3360", context);
53 }
54
55 Sequence<?> streamedAccVal = registry.getStreamingAccumulatorValue((NodeInfo) node, accumulator, phase);
56 if (streamedAccVal != null) {
57 return streamedAccVal;
58 }
59
60 TreeInfo root = ((NodeInfo)node).getTreeInfo();
61 XsltController controller = (XsltController)context.getController();
62 if (!accumulator.isUniversallyApplicable() && !controller.getAccumulatorManager().isApplicable(root, accumulator)) {
63 throw new XPathException("Accumulator " + name + " is not applicable to the current document", "XTDE3362");
64 }
65 AccumulatorManager manager = controller.getAccumulatorManager();
66 IAccumulatorData data = manager.getAccumulatorData(root, accumulator, context);
67 return data.getValue((NodeInfo) node, phase == Phase.AFTER);
68 }
69
70 /**
71 * Call the Callable.
72 *
73 * @param context the dynamic evaluation context
74 * @param arguments the values of the arguments, supplied as Sequences.
75 * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to
76 * call it only once; if the value is required more than once, it should first be converted
77 * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd
78 * SequenceTool.toGroundedValue().</p>
79 * <p>If the expected value is a single item, the item should be obtained by calling
80 * Sequence.head(): it cannot be assumed that the item will be passed as an instance of
81 * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p>
82 * <p>It is the caller's responsibility to perform any type conversions required
83 * to convert arguments to the type expected by the callee. An exception is where
84 * this Callable is explicitly an argument-converting wrapper around the original
85 * Callable.</p>
86 * @return the result of the evaluation, in the form of a Sequence. It is the responsibility
87 * of the callee to ensure that the type of result conforms to the expected result type.
88 * @throws net.sf.saxon.trans.XPathException
89 * if a dynamic error occurs during the evaluation of the expression
90 */
91 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
92 String name = arguments[0].head().getStringValue();
93 return getAccumulatorValue(name, getPhase(), context);
94 }
95
96 public static class AccumulatorBefore extends AccumulatorFn {
97
98 public AccumulatorBefore() {
99 //System.err.println("acc-before");
100 }
101 @Override
102 public Phase getPhase() {
103 return Phase.BEFORE;
104 }
105 }
106
107 public static class AccumulatorAfter extends AccumulatorFn {
108 @Override
109 public Phase getPhase() {
110 return Phase.AFTER;
111 }
112
113 public String getStreamerName() {
114 return "AccumulatorAfter";
115 }
116 }
117 }