Debian Codegraph @

net/sf/saxon/expr/accum/AccumulatorManager.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.expr.XPathContext;
11 import net.sf.saxon.expr.XPathContextMajor;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.om.TreeInfo;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.tiny.TinyTree;
16 import net.sf.saxon.tree.wrapper.VirtualCopy;
17 import net.sf.saxon.tree.wrapper.VirtualTreeInfo;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.WeakHashMap;
23
24 /**
25 * Manager for accumulator functions (XSLT 3.0).
26 *
27 * <p>In principle there is a dataset containing accumulator values for every accumulator/document pair.
28 * In addition, if the same stylesheet is run concurrently in multiple transformations, and a document
29 * is shared between these transformations, the data values for the accumulator can differ between the
30 * two transformations, because the accumulator rules can depend on values of stylesheet parameters
31 * or on context variables such as current-dateTime.</p>
32 *
33 * <p>It is important that the accumulator data for a tree does not cause the tree to be locked in
34 * memory for the duration of a transformation. We therefore keep a weak reference to the tree. For
35 * each tree, until it is garbage collected, there is a map from accumulator names to accumulator data.</p>
36 */
37 public class AccumulatorManager {
38
39
40 private transient WeakHashMap<TreeInfo, Map<Accumulator, IAccumulatorData>> accumulatorDataIndex =
41 new WeakHashMap<TreeInfo, Map<Accumulator, IAccumulatorData>>();
42
43 private transient WeakHashMap<TreeInfo, Set<? extends Accumulator>> applicableAccumulators =
44 new WeakHashMap<TreeInfo, Set<? extends Accumulator>>();
45
46 public AccumulatorManager() {
47 }
48
49 /**
50 * By default, all accumulators are applicable to any given tree. If this method is called,
51 * a specific set of accumulators are registered as applicable. This set may be empty.
52 * @param tree the document tree in question
53 * @param accumulators the set of accumulators that are appicable
54 */
55
56 public void setApplicableAccumulators(TreeInfo tree, Set<? extends Accumulator> accumulators) {
57 applicableAccumulators.put(tree, accumulators);
58 }
59
60 /**
61 * Ask whether a particular accumulator is applicable to a particular tree
62 * @param tree the tree in question
63 * @param accumulator the accumulator in question
64 * @return true if the accumulator is applicable to this tree, otherwise false
65 */
66
67 public boolean isApplicable(TreeInfo tree, Accumulator accumulator) {
68 Set<? extends Accumulator> accSet = applicableAccumulators.get(tree);
69 return accSet == null || accSet.contains(accumulator);
70 }
71
72 private static AccumulatorData MARKER = new AccumulatorData(null);
73
74 /**
75 * Get the data relating to a particular accumulator for a particular unstreamed document tree
76 *
77 * @param doc the source document tree in question
78 * @param acc the required accumulator
79 * @param context the XPath dynamic evaluation context
80 * @return a data structure holding the evaluated values of the accumulator for this source document
81 * @throws XPathException if any dynamic error occurs
82 */
83
84 public synchronized IAccumulatorData getAccumulatorData(TreeInfo doc, Accumulator acc, XPathContext context)
85 throws XPathException {
86 Map<Accumulator, IAccumulatorData> map = accumulatorDataIndex.get(doc);
87 if (map != null) {
88 IAccumulatorData data = map.get(acc);
89 if (data != null) {
90 if (data == MARKER) {
91 throw new XPathException("Accumulator " + acc.getAccumulatorName().getDisplayName() +
92 " requires access to its own value", "XTDE3400");
93 }
94 return data;
95 }
96 } else {
97 map = new HashMap<Accumulator, IAccumulatorData>();
98 map.put(acc, MARKER);
99 accumulatorDataIndex.put(doc, map);
100 }
101 if (doc instanceof VirtualTreeInfo && ((VirtualTreeInfo)doc).isCopyAccumulators()) {
102 NodeInfo original = ((VirtualCopy) doc.getRootNode()).getOriginalNode();
103 IAccumulatorData originalData = getAccumulatorData(original.getTreeInfo(), acc, context);
104 VirtualAccumulatorData vad = new VirtualAccumulatorData(originalData);
105 map.put(acc, vad);
106 return vad;
107 } else if (doc instanceof TinyTree && ((TinyTree)doc).getCopiedFrom() != null) {
108 IAccumulatorData original = getAccumulatorData(((TinyTree) doc).getCopiedFrom().getTreeInfo(), acc, context);
109 return new PathMappedAccumulatorData(original, ((TinyTree) doc).getCopiedFrom());
110 } else {
111 AccumulatorData d = new AccumulatorData(acc);
112 XPathContextMajor c2 = context.newCleanContext();
113 c2.setCurrentComponent(acc.getDeclaringComponent());
114 try {
115 d.buildIndex(doc.getRootNode(), c2);
116 map.put(acc, d);
117 return d;
118 } catch (XPathException err) {
119 IAccumulatorData failed = new FailedAccumulatorData(acc, err);
120 map.put(acc, failed);
121 return failed;
122 }
123 }
124 }
125
126 /**
127 * Add the accumulator data for a particular accumulator, if it does not already exist
128 * @param doc the document/tree whose accumulator data is being added
129 * @param acc the accumulator in question
130 * @param accData the data holding the values of the accumulator for the nodes in this tree
131 */
132
133 public synchronized void addAccumulatorData(TreeInfo doc, Accumulator acc, IAccumulatorData accData) {
134 Map<Accumulator, IAccumulatorData> map = accumulatorDataIndex.get(doc);
135 if (map != null) {
136 IAccumulatorData data = map.get(acc);
137 if (data != null) {
138 return;
139 }
140 } else {
141 map = new HashMap<Accumulator, IAccumulatorData>();
142 accumulatorDataIndex.put(doc, map);
143 }
144 map.put(acc, accData);
145 }
146
147
148 }