| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public void setApplicableAccumulators(TreeInfo tree, Set<? extends Accumulator> accumulators) { |
| 57 |
applicableAccumulators.put(tree, accumulators); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
} |