Debian Codegraph @

net/sf/saxon/expr/accum/AccumulatorRegistry.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.expr.accum;
9
10 import net.sf.saxon.functions.AccumulatorFn;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.StructuredQName;
15 import net.sf.saxon.style.StyleElement;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.Whitespace;
18
19 import java.util.*;
20
21 /**
22 * Static registry for accumulators (XSLT 3.0) defined within a single package. Generally accessed
23 * via the PackageData object for the package.
24 */
25 public class AccumulatorRegistry {
26
27 protected Map<StructuredQName, Accumulator> accumulatorsByName = new HashMap<StructuredQName, Accumulator>();
28
29
30 public AccumulatorRegistry() {
31 //
32 }
33
34 /**
35 * Process the use-accumulators attribute of instructions such as xsl:stream, xsl:mode, etc
36 * @param useAccumulatorsAtt the value of the use-accumulators attribute
37 * @return the list of accumulators referenced
38 * @throws net.sf.saxon.trans.XPathException if the attribute is invalid
39 */
40
41 public Set<Accumulator> getUsedAccumulators(String useAccumulatorsAtt, StyleElement styleElement) throws XPathException {
42 Set<Accumulator> accumulators = new HashSet<Accumulator>();
43 String attNames = Whitespace.trim(useAccumulatorsAtt);
44 String[] tokens = attNames.split("[ \t\r\n]+");
45 if (tokens.length == 1 && tokens[0].equals("#all")) {
46 for (Accumulator acc : getAllAccumulators()) {
47 accumulators.add(acc);
48 }
49 } else if (tokens.length == 1 && tokens[0].isEmpty()) {
50 // do nothing - empty list
51 } else {
52 List<StructuredQName> names = new ArrayList<StructuredQName>(tokens.length);
53 for (String token : tokens) {
54 if (token.equals("#all")) {
55 throw new XPathException("If use-accumulators contains the token '#all', it must be the only token", "XTSE3300");
56 }
57 StructuredQName name = styleElement.makeQName(token);
58 if (names.contains(name)) {
59 throw new XPathException("Duplicate QName in use-accumulators attribute: " + token, "XTSE3300");
60 }
61 Accumulator acc = getAccumulator(name);
62 if (acc == null) {
63 throw new XPathException("Unknown accumulator name: " + token, "XTSE3300");
64 }
65 names.add(name);
66 accumulators.add(acc);
67 }
68 }
69 return accumulators;
70 }
71
72 /**
73 * Register an accumulator
74 *
75 * @param acc the accumulator to be registered
76 */
77
78 public void addAccumulator(Accumulator acc) {
79 if (acc.getAccumulatorName() != null) {
80 accumulatorsByName.put(acc.getAccumulatorName(), acc);
81 }
82 }
83
84 /**
85 * Get the accumulator with a given name
86 * @param name the name of the accumulator
87 * @return the accumulator with this name
88 */
89
90 public Accumulator getAccumulator(StructuredQName name) {
91 return accumulatorsByName.get(name);
92 }
93
94 /**
95 * Get all the registered accumulators
96 * @return a collection of accumulators
97 */
98 public Iterable<Accumulator> getAllAccumulators() {
99 return accumulatorsByName.values();
100 }
101
102 /**
103 * Get the run-time value of a streamed accumulator
104 *
105 * @param node the context node, which must be a streamed node
106 * @param accumulator the accumulator whose value is required
107 * @param phase pre-descent or post-descent
108 * @return the value of the accumulator, or null if the context node is not streamed
109 * @throws XPathException if a dynamic error occurs
110 */
111
112 public Sequence<?> getStreamingAccumulatorValue(NodeInfo node, Accumulator accumulator, AccumulatorFn.Phase phase)
113 throws XPathException {
114 return null;
115 }
116
117 }
118