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 org.apache.commons.jxpath.ri.EvalContext;
20 import org.apache.commons.jxpath.ri.compiler.NodeTest;
21 import org.apache.commons.jxpath.ri.model.NodePointer;
22
23
24
25
26
27
28
29 public class AncestorContext extends EvalContext {
30 private NodeTest nodeTest;
31 private boolean setStarted = false;
32 private NodePointer currentNodePointer;
33 private boolean includeSelf;
34
35
36
37
38
39
40
41
42 public AncestorContext(
43 EvalContext parentContext,
44 boolean includeSelf,
45 NodeTest nodeTest) {
46 super(parentContext);
47 this.includeSelf = includeSelf;
48 this.nodeTest = nodeTest;
49 }
50
51 public NodePointer getCurrentNodePointer() {
52 return currentNodePointer;
53 }
54
55 public int getDocumentOrder() {
56 return -1;
57 }
58
59 public void reset() {
60 super.reset();
61 setStarted = false;
62 }
63
64 public boolean setPosition(int position) {
65 if (position < getCurrentPosition()) {
66 reset();
67 }
68
69 while (getCurrentPosition() < position) {
70 if (!nextNode()) {
71 return false;
72 }
73 }
74 return true;
75 }
76
77 public boolean nextNode() {
78 if (!setStarted) {
79 setStarted = true;
80 currentNodePointer = parentContext.getCurrentNodePointer();
81 if (includeSelf && currentNodePointer.testNode(nodeTest)) {
82 position++;
83 return true;
84 }
85 }
86
87 while (true) {
88 currentNodePointer = currentNodePointer.getImmediateParentPointer();
89
90 if (currentNodePointer == null) {
91 return false;
92 }
93
94 if (currentNodePointer.testNode(nodeTest)) {
95 position++;
96 return true;
97 }
98 }
99 }
100 }