Debian Codegraph @

net/sf/saxon/expr/accum/PathMappedAccumulatorData.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.om.AxisInfo;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.pattern.AnyNodeTest;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.iter.AxisIterator;
16 import net.sf.saxon.tree.tiny.TinyNodeImpl;
17 import net.sf.saxon.tree.util.Navigator;
18
19 import java.util.Stack;
20
21 /**
22 * Accumulator data for a tree that is obtained by mapping the nodes in this tree
23 * to nodes in some other tree (specifically, the subtree from which this tree was
24 * originally copied with copy-accumulators=yes) and getting the accumulator value
25 * from the corresponding node in the other tree.
26 */
27 public class PathMappedAccumulatorData implements IAccumulatorData {
28
29 private IAccumulatorData originalData;
30 private NodeInfo origin;
31
32 PathMappedAccumulatorData(IAccumulatorData original, NodeInfo origin) {
33 this.originalData = original;
34 this.origin = origin;
35 }
36
37 @Override
38 public Accumulator getAccumulator() {
39 return null;
40 }
41
42 @Override
43 public Sequence<?> getValue(NodeInfo node, boolean postDescent) throws XPathException {
44 return originalData.getValue(map(node), postDescent);
45 }
46
47 private NodeInfo map(NodeInfo node) {
48 if (origin instanceof TinyNodeImpl && node instanceof TinyNodeImpl) {
49 // Fast algorithm where both trees are TinyTrees: map the node numbers
50 int nodeNrInSubtree = ((TinyNodeImpl)node).getNodeNumber();
51 return ((TinyNodeImpl)origin).getTree().getNode(nodeNrInSubtree + ((TinyNodeImpl)origin).getNodeNumber());
52 } else {
53 // General algorithm: get the node with a corresponding path in terms of sibling position.
54 Stack<Integer> path = new Stack<Integer>();
55 NodeInfo ancestor = node;
56 while (ancestor != null) {
57 path.push(Navigator.getSiblingPosition(ancestor, AnyNodeTest.getInstance(), Integer.MAX_VALUE));
58 ancestor = ancestor.getParent();
59 }
60 NodeInfo target = origin;
61 while (!path.isEmpty()) {
62 int pos = path.pop();
63 AxisIterator kids = target.iterateAxis(AxisInfo.CHILD);
64 while (pos-- > 0) {
65 target = kids.next();
66 assert (target != null);
67 }
68 }
69 return target;
70 }
71 }
72
73
74 }
75