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