1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.model.jdom;
19
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.commons.jxpath.ri.compiler.NodeTest;
24 import org.apache.commons.jxpath.ri.model.NodeIterator;
25 import org.apache.commons.jxpath.ri.model.NodePointer;
26 import org.jdom.Document;
27 import org.jdom.Element;
28
29
30
31
32 public class JDOMNodeIterator implements NodeIterator {
33
34 private final NodePointer parent;
35 private final NodeTest nodeTest;
36 private final boolean reverse;
37 private int position;
38 private int index;
39 private List children;
40 private Object child;
41
42
43
44
45
46
47
48
49
50 public JDOMNodeIterator(final NodePointer parent, final NodeTest nodeTest, final boolean reverse, final NodePointer startWith) {
51 this.parent = parent;
52 if (startWith != null) {
53 this.child = startWith.getNode();
54 }
55
56 final Object node = parent.getNode();
57 if (node instanceof Document) {
58 this.children = ((Document) node).getContent();
59 } else if (node instanceof Element) {
60 this.children = ((Element) node).getContent();
61 } else {
62 this.children = Collections.EMPTY_LIST;
63 }
64 this.nodeTest = nodeTest;
65 this.reverse = reverse;
66 }
67
68 @Override
69 public NodePointer getNodePointer() {
70 if (child == null) {
71 if (!setPosition(1)) {
72 return null;
73 }
74 position = 0;
75 }
76 return new JDOMNodePointer(parent, child);
77 }
78
79 @Override
80 public int getPosition() {
81 return position;
82 }
83
84
85
86
87
88
89 private boolean next() {
90 position++;
91 if (!reverse) {
92 if (position == 1) {
93 index = 0;
94 if (child != null) {
95 index = children.indexOf(child) + 1;
96 }
97 } else {
98 index++;
99 }
100 for (; index < children.size(); index++) {
101 child = children.get(index);
102 if (testChild()) {
103 return true;
104 }
105 }
106 return false;
107 }
108 if (position == 1) {
109 index = children.size() - 1;
110 if (child != null) {
111 index = children.indexOf(child) - 1;
112 }
113 } else {
114 index--;
115 }
116 for (; index >= 0; index--) {
117 child = children.get(index);
118 if (testChild()) {
119 return true;
120 }
121 }
122 return false;
123 }
124
125
126
127
128
129
130
131 private boolean previous() {
132 position--;
133 if (!reverse) {
134 while (--index >= 0) {
135 child = children.get(index);
136 if (testChild()) {
137 return true;
138 }
139 }
140 } else {
141 for (; index < children.size(); index++) {
142 child = children.get(index);
143 if (testChild()) {
144 return true;
145 }
146 }
147 }
148 return false;
149 }
150
151 @Override
152 public boolean setPosition(final int position) {
153 while (this.position < position) {
154 if (!next()) {
155 return false;
156 }
157 }
158 while (this.position > position) {
159 if (!previous()) {
160 return false;
161 }
162 }
163 return true;
164 }
165
166
167
168
169
170
171 private boolean testChild() {
172 return JDOMNodePointer.testNode(parent, child, nodeTest);
173 }
174 }