|
1 |
| package org.objectledge.web.test; |
|
2 |
| |
|
3 |
| import java.util.ArrayList; |
|
4 |
| |
|
5 |
| import org.w3c.dom.CharacterData; |
|
6 |
| import org.w3c.dom.Element; |
|
7 |
| import org.w3c.dom.Node; |
|
8 |
| import org.w3c.dom.NodeList; |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| public class DOMTreeWalker |
|
17 |
| { |
|
18 |
| private Element root; |
|
19 |
| |
|
20 |
| private ArrayList<Object> elements; |
|
21 |
| |
|
22 |
| private int currentIndex; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
0
| public DOMTreeWalker(Element root)
|
|
30 |
| { |
|
31 |
0
| this.root = root;
|
|
32 |
0
| this.elements = new ArrayList<Object>();
|
|
33 |
0
| this.currentIndex = 0;
|
|
34 |
0
| loadList();
|
|
35 |
| } |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
0
| public void reset()
|
|
41 |
| { |
|
42 |
0
| currentIndex = 0;
|
|
43 |
| } |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
0
| public void loadList()
|
|
49 |
| { |
|
50 |
0
| loadNode(root);
|
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
0
| private void loadNode(Node node)
|
|
59 |
| { |
|
60 |
0
| if(node instanceof CharacterData)
|
|
61 |
| { |
|
62 |
0
| String data = ((CharacterData)node).getData();
|
|
63 |
0
| elements.add(data);
|
|
64 |
| } |
|
65 |
0
| if(node instanceof Element)
|
|
66 |
| { |
|
67 |
0
| elements.add(node);
|
|
68 |
0
| NodeList children = node.getChildNodes();
|
|
69 |
0
| for (int i = 0; i < children.getLength(); i++)
|
|
70 |
| { |
|
71 |
0
| loadNode(children.item(i));
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| public Element findElementAfterText(String text, String tagName)
|
|
85 |
| { |
|
86 |
0
| for(; currentIndex < elements.size(); currentIndex++)
|
|
87 |
| { |
|
88 |
0
| Object ob = elements.get(currentIndex);
|
|
89 |
0
| if(ob instanceof String)
|
|
90 |
| { |
|
91 |
0
| if(((String)ob).contains(text))
|
|
92 |
| { |
|
93 |
0
| break;
|
|
94 |
| } |
|
95 |
| } |
|
96 |
| } |
|
97 |
0
| for(; currentIndex < elements.size(); currentIndex++)
|
|
98 |
| { |
|
99 |
0
| Object ob = elements.get(currentIndex);
|
|
100 |
0
| if(ob instanceof Element)
|
|
101 |
| { |
|
102 |
0
| if(tagName != null)
|
|
103 |
| { |
|
104 |
0
| String tag = ((Element)ob).getNodeName();
|
|
105 |
0
| if(tagName.equals(tag.toLowerCase()))
|
|
106 |
| { |
|
107 |
0
| return (Element)ob;
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| else |
|
111 |
| { |
|
112 |
0
| return (Element)ob;
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| } |
|
116 |
0
| return null;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
0
| public Element findElementWithText(String text, String tagName)
|
|
127 |
| { |
|
128 |
0
| Element matchedElement = null;
|
|
129 |
0
| Element currentElement = null;
|
|
130 |
| |
|
131 |
0
| for(; currentIndex < elements.size(); currentIndex++)
|
|
132 |
| { |
|
133 |
0
| Object ob = elements.get(currentIndex);
|
|
134 |
0
| if(ob instanceof Element)
|
|
135 |
| { |
|
136 |
0
| if(tagName != null)
|
|
137 |
| { |
|
138 |
0
| String tag = ((Element)ob).getNodeName();
|
|
139 |
0
| if(tagName.equals(tag.toLowerCase()))
|
|
140 |
| { |
|
141 |
0
| currentElement = (Element)ob;
|
|
142 |
| } |
|
143 |
| } |
|
144 |
| else |
|
145 |
| { |
|
146 |
0
| currentElement = (Element)ob;
|
|
147 |
| } |
|
148 |
| } |
|
149 |
0
| if(ob instanceof String)
|
|
150 |
| { |
|
151 |
0
| if(((String)ob).contains(text))
|
|
152 |
| { |
|
153 |
0
| matchedElement = currentElement;
|
|
154 |
| } |
|
155 |
| } |
|
156 |
| } |
|
157 |
0
| return matchedElement;
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
0
| public String getNextText()
|
|
166 |
| { |
|
167 |
0
| return getNextText(0);
|
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
0
| public String getNextText(int skip)
|
|
178 |
| { |
|
179 |
0
| currentIndex = currentIndex + skip + 1;
|
|
180 |
0
| for(; currentIndex < elements.size(); currentIndex++)
|
|
181 |
| { |
|
182 |
0
| Object ob = elements.get(currentIndex);
|
|
183 |
0
| if(ob instanceof String)
|
|
184 |
| { |
|
185 |
0
| return (String)ob;
|
|
186 |
| } |
|
187 |
| } |
|
188 |
0
| return null;
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
0
| public Element getNextElement()
|
|
197 |
| { |
|
198 |
0
| return getNextElement(0);
|
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
0
| public Element getNextElement(int skip)
|
|
208 |
| { |
|
209 |
0
| return getNextElement(skip, null);
|
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
0
| public Element getNextElement(int skip, String tagName)
|
|
220 |
| { |
|
221 |
0
| currentIndex = currentIndex + skip + 1;
|
|
222 |
0
| for(; currentIndex < elements.size(); currentIndex++)
|
|
223 |
| { |
|
224 |
0
| Object ob = elements.get(currentIndex);
|
|
225 |
0
| if(ob instanceof Element)
|
|
226 |
| { |
|
227 |
0
| if(tagName != null)
|
|
228 |
| { |
|
229 |
0
| String tag = ((Element)ob).getNodeName();
|
|
230 |
0
| if(tagName.equals(tag.toLowerCase()))
|
|
231 |
| { |
|
232 |
0
| return (Element)ob;
|
|
233 |
| } |
|
234 |
| } |
|
235 |
| else |
|
236 |
| { |
|
237 |
0
| return (Element)ob;
|
|
238 |
| } |
|
239 |
| } |
|
240 |
| } |
|
241 |
0
| return null;
|
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
0
| public void gotoElement(Element element)
|
|
250 |
| { |
|
251 |
0
| for(currentIndex = 0; currentIndex < elements.size(); currentIndex++)
|
|
252 |
| { |
|
253 |
0
| Object node = elements.get(currentIndex);
|
|
254 |
0
| if(node instanceof Element)
|
|
255 |
| { |
|
256 |
0
| Element el = (Element)node;
|
|
257 |
0
| if(el.getNodeName().equals(element.getNodeName()))
|
|
258 |
| { |
|
259 |
0
| if(el.equals(element))
|
|
260 |
| { |
|
261 |
0
| break;
|
|
262 |
| } |
|
263 |
| } |
|
264 |
| } |
|
265 |
| } |
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
0
| public int countTags(String tagName)
|
|
275 |
| { |
|
276 |
0
| int i = 0;
|
|
277 |
0
| int counter = 0;
|
|
278 |
0
| for(; i < elements.size(); i++)
|
|
279 |
| { |
|
280 |
0
| Object ob = elements.get(i);
|
|
281 |
0
| if(ob instanceof Element)
|
|
282 |
| { |
|
283 |
0
| if(tagName != null)
|
|
284 |
| { |
|
285 |
0
| String tag = ((Element)ob).getNodeName();
|
|
286 |
0
| if(tagName.equals(tag.toLowerCase()))
|
|
287 |
| { |
|
288 |
0
| counter++;
|
|
289 |
| } |
|
290 |
| } |
|
291 |
| else |
|
292 |
| { |
|
293 |
0
| counter++;
|
|
294 |
| } |
|
295 |
| } |
|
296 |
| } |
|
297 |
0
| return counter;
|
|
298 |
| } |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
0
| public static int countElements(Node node, String tagName)
|
|
308 |
| { |
|
309 |
0
| int counter = 0;
|
|
310 |
0
| NodeList list = node.getChildNodes();
|
|
311 |
0
| for(int i = 0; i< list.getLength(); i++)
|
|
312 |
| { |
|
313 |
0
| Node child = list.item(i);
|
|
314 |
0
| if(child instanceof Element)
|
|
315 |
| { |
|
316 |
0
| if(tagName != null)
|
|
317 |
| { |
|
318 |
0
| String tag = ((Element)child).getNodeName();
|
|
319 |
0
| if(tagName.equals(tag.toLowerCase()))
|
|
320 |
| { |
|
321 |
0
| counter++;
|
|
322 |
| } |
|
323 |
| } |
|
324 |
| else |
|
325 |
| { |
|
326 |
0
| counter++;
|
|
327 |
| } |
|
328 |
| } |
|
329 |
| } |
|
330 |
0
| return counter;
|
|
331 |
| } |
|
332 |
| } |