1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.axes;
18
19 import java.util.Stack;
20
21 import org.apache.commons.jxpath.ri.EvalContext;
22 import org.apache.commons.jxpath.ri.compiler.NodeTest;
23 import org.apache.commons.jxpath.ri.model.NodeIterator;
24 import org.apache.commons.jxpath.ri.model.NodePointer;
25
26
27
28
29
30
31
32 public class PrecedingOrFollowingContext extends EvalContext {
33 private NodeTest nodeTest;
34 private boolean setStarted = false;
35 private Stack stack = null;
36 private NodePointer currentNodePointer;
37 private NodePointer currentRootLocation;
38 private boolean reverse;
39
40
41
42
43
44
45
46 public PrecedingOrFollowingContext(
47 EvalContext parentContext,
48 NodeTest nodeTest,
49 boolean reverse) {
50 super(parentContext);
51 this.nodeTest = nodeTest;
52 this.reverse = reverse;
53 }
54
55 public NodePointer getCurrentNodePointer() {
56 return currentNodePointer;
57 }
58
59 public int getDocumentOrder() {
60 return reverse ? -1 : 1;
61 }
62
63 public void reset() {
64 super.reset();
65 setStarted = false;
66 }
67
68 public boolean setPosition(int position) {
69 if (position < this.position) {
70 reset();
71 }
72
73 while (this.position < position) {
74 if (!nextNode()) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 public boolean nextNode() {
82 if (!setStarted) {
83 setStarted = true;
84 if (stack == null) {
85 stack = new Stack();
86 }
87 else {
88 stack.clear();
89 }
90 currentRootLocation = parentContext.getCurrentNodePointer();
91 NodePointer parent = currentRootLocation.getParent();
92 if (parent != null) {
93
94 stack.push(
95 parent.childIterator(null, reverse, currentRootLocation));
96 }
97 }
98
99 while (true) {
100 if (stack.isEmpty()) {
101 currentRootLocation = currentRootLocation.getParent();
102
103 if (currentRootLocation == null
104 || currentRootLocation.isRoot()) {
105 break;
106 }
107
108 NodePointer parent = currentRootLocation.getParent();
109 if (parent != null) {
110 stack.push(
111 parent.childIterator(
112 null,
113 reverse,
114 currentRootLocation));
115 }
116 }
117
118 while (!stack.isEmpty()) {
119 if (!reverse) {
120 NodeIterator it = (NodeIterator) stack.peek();
121 if (it.setPosition(it.getPosition() + 1)) {
122 currentNodePointer = it.getNodePointer();
123 if (!currentNodePointer.isLeaf()) {
124 stack.push(
125 currentNodePointer.childIterator(
126 null,
127 reverse,
128 null));
129 }
130 if (currentNodePointer.testNode(nodeTest)) {
131 super.setPosition(getCurrentPosition() + 1);
132 return true;
133 }
134 }
135 else {
136
137
138 stack.pop();
139 }
140 }
141 else {
142 NodeIterator it = (NodeIterator) stack.peek();
143 if (it.setPosition(it.getPosition() + 1)) {
144 currentNodePointer = it.getNodePointer();
145 if (!currentNodePointer.isLeaf()) {
146 stack.push(
147 currentNodePointer.childIterator(
148 null,
149 reverse,
150 null));
151 }
152 else if (currentNodePointer.testNode(nodeTest)) {
153 super.setPosition(getCurrentPosition() + 1);
154 return true;
155 }
156 }
157 else {
158 stack.pop();
159 if (!stack.isEmpty()) {
160 it = (NodeIterator) stack.peek();
161 currentNodePointer = it.getNodePointer();
162 if (currentNodePointer.testNode(nodeTest)) {
163 super.setPosition(getCurrentPosition() + 1);
164 return true;
165 }
166 }
167 }
168 }
169 }
170 }
171 return false;
172 }
173 }