KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > TextAndElementWalker


1 package net.sourceforge.jwebunit;
2
3 import org.w3c.dom.Node JavaDoc;
4 import org.w3c.dom.NodeList JavaDoc;
5 import org.w3c.dom.Element JavaDoc;
6 import org.w3c.dom.CharacterData JavaDoc;
7
8 import java.util.ArrayList JavaDoc;
9
10 /**
11  * Walks over a DOM subtree to associate labels with some kind of elements.
12  */

13 class TextAndElementWalker {
14     private StringBuffer JavaDoc currentText;
15     private ArrayList JavaDoc texts;
16     private ArrayList JavaDoc nodes;
17     private String JavaDoc[] tags;
18
19     public TextAndElementWalker(Element JavaDoc root, String JavaDoc[] tags) {
20         currentText = new StringBuffer JavaDoc();
21         texts = new ArrayList JavaDoc();
22         nodes = new ArrayList JavaDoc();
23         this.tags = tags;
24         walk(root);
25     }
26
27     private void walk(Node JavaDoc node) {
28         if (node instanceof CharacterData JavaDoc) {
29             currentText.append(((CharacterData JavaDoc) node).getData());
30         } else if (node instanceof Element JavaDoc) {
31             String JavaDoc tag = node.getNodeName();
32             if (matches(tag)) {
33                 rememberElement((Element JavaDoc) node);
34             } else {
35                 NodeList JavaDoc children = node.getChildNodes();
36                 for (int i = 0; i < children.getLength(); i++) {
37                     walk(children.item(i));
38                 }
39             }
40         }
41     }
42
43     private boolean matches(String JavaDoc tag) {
44         for (int i = 0; i < tags.length; i++) {
45             if (tag.equalsIgnoreCase(tags[i])) {
46                 return true;
47             }
48         }
49         return false;
50     }
51
52     private void rememberElement(Element JavaDoc node) {
53         texts.add(currentText.toString());
54         nodes.add(node);
55         currentText = new StringBuffer JavaDoc();
56     }
57
58     public Element JavaDoc getElementAfterText(String JavaDoc label) {
59         int index = indexOfElementAfterText(label);
60         return index == -1 ? null : (Element JavaDoc) nodes.get(index);
61     }
62
63     private int indexOfElementAfterText(String JavaDoc label) {
64         for (int i = 0; i < nodes.size(); i++) {
65             if (((String JavaDoc) texts.get(i)).indexOf(label) != -1) {
66                 return i;
67             }
68         }
69         return -1;
70     }
71
72     public Element JavaDoc getElementWithTextAfterText(String JavaDoc linkText, String JavaDoc labelText) {
73         int index = indexOfElementAfterText(labelText);
74         if (index != -1) {
75             for (int i = index; i < nodes.size(); i++) {
76                 Element JavaDoc node = (Element JavaDoc) nodes.get(i);
77                 if (HttpUnitDialog.nodeContainsText(node, linkText)) {
78                     return node;
79                 }
80             }
81         }
82         return null;
83     }
84
85 }
86
Popular Tags