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.Pointer;
20 import org.apache.commons.jxpath.ri.EvalContext;
21 import org.apache.commons.jxpath.ri.compiler.NodeTest;
22 import org.apache.commons.jxpath.ri.model.NodeIterator;
23 import org.apache.commons.jxpath.ri.model.NodePointer;
24
25
26
27
28
29
30
31
32 public class ChildContext extends EvalContext {
33 private NodeTest nodeTest;
34 private boolean startFromParentLocation;
35 private boolean reverse;
36 private NodeIterator iterator;
37
38
39
40
41
42
43
44
45 public ChildContext(EvalContext parentContext, NodeTest nodeTest,
46 boolean startFromParentLocation, boolean reverse) {
47 super(parentContext);
48 this.nodeTest = nodeTest;
49 this.startFromParentLocation = startFromParentLocation;
50 this.reverse = reverse;
51 }
52
53 public NodePointer getCurrentNodePointer() {
54 if (position == 0 && !setPosition(1)) {
55 return null;
56 }
57 return iterator == null ? null : iterator.getNodePointer();
58 }
59
60
61
62
63
64
65
66
67
68 public Pointer getSingleNodePointer() {
69 if (position == 0) {
70 while (nextSet()) {
71 prepare();
72 if (iterator == null) {
73 return null;
74 }
75
76 NodePointer pointer = iterator.getNodePointer();
77 if (pointer != null) {
78 return pointer;
79 }
80 }
81 return null;
82 }
83 return getCurrentNodePointer();
84 }
85
86 public boolean nextNode() {
87 return setPosition(getCurrentPosition() + 1);
88 }
89
90 public void reset() {
91 super.reset();
92 iterator = null;
93 }
94
95 public boolean setPosition(int position) {
96 int oldPosition = getCurrentPosition();
97 super.setPosition(position);
98 if (oldPosition == 0) {
99 prepare();
100 }
101 return iterator == null ? false : iterator.setPosition(position);
102 }
103
104
105
106
107 private void prepare() {
108 NodePointer parent = parentContext.getCurrentNodePointer();
109 if (parent == null) {
110 return;
111 }
112 NodePointer useParent = startFromParentLocation ? parent.getParent() : parent;
113 iterator = useParent.childIterator(nodeTest, reverse,
114 startFromParentLocation ? parent : null);
115 }
116 }