| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.accum; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.XPathContextMajor; |
| 14 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 15 |
import net.sf.saxon.expr.parser.Evaluator; |
| 16 |
import net.sf.saxon.om.*; |
| 17 |
import net.sf.saxon.trans.Err; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.trans.rules.Rule; |
| 20 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 21 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 22 |
import net.sf.saxon.tree.util.Navigator; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class AccumulatorData implements IAccumulatorData { |
| 31 |
|
| 32 |
private Accumulator accumulator; |
| 33 |
private List<DataPoint> values = new ArrayList<>(); |
| 34 |
private boolean building = false; |
| 35 |
|
| 36 |
public AccumulatorData(Accumulator acc) { |
| 37 |
this.accumulator = acc; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public Accumulator getAccumulator() { |
| 46 |
return accumulator; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public void buildIndex(NodeInfo doc, XPathContext context) throws XPathException { |
| 60 |
|
| 61 |
if (building) { |
| 62 |
throw new XPathException("Accumulator " + accumulator.getAccumulatorName().getDisplayName() + |
| 63 |
" requires access to its own value", "XTDE3400"); |
| 64 |
} |
| 65 |
building = true; |
| 66 |
Expression initialValue = accumulator.getInitialValueExpression(); |
| 67 |
XPathContextMajor c2 = context.newContext(); |
| 68 |
SlotManager sf = accumulator.getSlotManagerForInitialValueExpression(); |
| 69 |
Sequence[] slots = new Sequence[sf.getNumberOfVariables()]; |
| 70 |
c2.setStackFrame(sf, slots); |
| 71 |
c2.setCurrentIterator(new ManualIterator<>(doc)); |
| 72 |
Sequence<?> val = initialValue.iterate(c2).materialize(); |
| 73 |
values.add(new DataPoint(new Visit(doc, false), val)); |
| 74 |
val = visit(doc, val, c2); |
| 75 |
values.add(new DataPoint(new Visit(doc, true), val)); |
| 76 |
((ArrayList) values).trimToSize(); |
| 77 |
building = false; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@SuppressWarnings({"InfiniteRecursion"}) |
| 103 |
private Sequence<?> visit(NodeInfo node, Sequence<?> value, XPathContext context) throws XPathException { |
| 104 |
((ManualIterator)context.getCurrentIterator()).setContextItem(node); |
| 105 |
Rule rule = accumulator.getPreDescentRules().getRule(node, context); |
| 106 |
if (rule != null) { |
| 107 |
value = processRule(rule, node, false, value, context); |
| 108 |
if (accumulator.isTracing()) { |
| 109 |
context.getConfiguration().getLogger().info(accumulator.getAccumulatorName().getDisplayName() + |
| 110 |
" BEFORE " + Navigator.getPath(node) + ": " + Err.depictSequence(value)); |
| 111 |
} |
| 112 |
} |
| 113 |
AxisIterator iter = node.iterateAxis(AxisInfo.CHILD); |
| 114 |
NodeInfo kid; |
| 115 |
while ((kid = iter.next()) != null) { |
| 116 |
value = visit(kid, value, context); |
| 117 |
} |
| 118 |
((ManualIterator) context.getCurrentIterator()).setContextItem(node); |
| 119 |
rule = accumulator.getPostDescentRules().getRule(node, context); |
| 120 |
if (rule != null) { |
| 121 |
value = processRule(rule, node, true, value, context); |
| 122 |
if (accumulator.isTracing()) { |
| 123 |
context.getConfiguration().getLogger().info(accumulator.getAccumulatorName().getDisplayName() + |
| 124 |
" AFTER " + Navigator.getPath(node) + ": " + Err.depictSequence(value)); |
| 125 |
} |
| 126 |
} |
| 127 |
return value; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private Sequence<?> processRule(Rule rule, NodeInfo node, boolean isPostDescent, Sequence<?> value, XPathContext context) throws XPathException { |
| 143 |
AccumulatorRule target = (AccumulatorRule) rule.getAction(); |
| 144 |
Expression delta = target.getNewValueExpression(); |
| 145 |
XPathContextMajor c2 = context.newCleanContext(); |
| 146 |
final Controller controller = c2.getController(); |
| 147 |
assert controller != null; |
| 148 |
ManualIterator<NodeInfo> initialNode = new ManualIterator<>(node); |
| 149 |
c2.setCurrentIterator(initialNode); |
| 150 |
c2.openStackFrame(target.getStackFrameMap()); |
| 151 |
c2.setLocalVariable(0, value); |
| 152 |
c2.setCurrentComponent(accumulator.getDeclaringComponent()); |
| 153 |
c2.setTemporaryOutputState(StandardNames.XSL_ACCUMULATOR_RULE); |
| 154 |
value = Evaluator.EAGER_SEQUENCE.evaluate(delta, c2); |
| 155 |
|
| 156 |
values.add(new DataPoint(new Visit(node, isPostDescent), value)); |
| 157 |
return value; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public Sequence<?> getValue(NodeInfo node, boolean postDescent) { |
| 175 |
Visit visit = new Visit(node, postDescent); |
| 176 |
return search(0, values.size(), visit); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private Sequence<?> search(int start, int end, Visit sought) { |
| 190 |
|
| 191 |
if (start == end) { |
| 192 |
|
| 193 |
int rel = sought.compareTo(values.get(start).visit); |
| 194 |
if (rel < 0 ) { |
| 195 |
return values.get(start - 1).value; |
| 196 |
} else { |
| 197 |
return values.get(start).value; |
| 198 |
} |
| 199 |
} |
| 200 |
int mid = (start + end) / 2; |
| 201 |
if (sought.compareTo(values.get(mid).visit) <= 0) { |
| 202 |
return search(start, mid, sought); |
| 203 |
} else { |
| 204 |
return search(mid + 1, end, sought); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
private static class Visit implements Comparable<Visit> { |
| 221 |
public NodeInfo node; |
| 222 |
public boolean isPostDescent; |
| 223 |
|
| 224 |
public Visit(NodeInfo node, boolean isPostDescent) { |
| 225 |
this.node = node; |
| 226 |
this.isPostDescent = isPostDescent; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public int compareTo(Visit other) { |
| 237 |
int relation = Navigator.comparePosition(node, other.node); |
| 238 |
switch (relation) { |
| 239 |
case AxisInfo.SELF: |
| 240 |
if (isPostDescent == other.isPostDescent) { |
| 241 |
return 0; |
| 242 |
} else { |
| 243 |
return isPostDescent ? +1 : -1; |
| 244 |
} |
| 245 |
case AxisInfo.PRECEDING: |
| 246 |
return -1; |
| 247 |
case AxisInfo.FOLLOWING: |
| 248 |
return +1; |
| 249 |
case AxisInfo.ANCESTOR: |
| 250 |
return isPostDescent ? +1 : -1; |
| 251 |
case AxisInfo.DESCENDANT: |
| 252 |
return other.isPostDescent ? -1 : +1; |
| 253 |
default: |
| 254 |
throw new IllegalStateException(); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private static class DataPoint { |
| 264 |
public Visit visit; |
| 265 |
public Sequence<?> value; |
| 266 |
|
| 267 |
public DataPoint(Visit visit, Sequence<?> value) { |
| 268 |
this.visit = visit; |
| 269 |
this.value = value; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
} |