01: package watij.finders;
02:
03: import org.w3c.dom.Element;
04: import org.w3c.dom.Node;
05: import org.w3c.dom.NodeList;
06: import watij.utilities.StringUtils;
07: import watij.utilities.Utils;
08:
09: /**
10: * Created by IntelliJ IDEA.
11: * User:
12: * Date: Apr 20, 2006
13: * Time: 6:40:55 PM
14: * To change this template use File | Settings | File Templates.
15: */
16: public class TextFinder extends BaseFinder implements Symbol {
17:
18: String what;
19:
20: public TextFinder() {
21: }
22:
23: public TextFinder(String what) {
24: this .what = what;
25: }
26:
27: public Finder newFinder(String what) throws Exception {
28: return new TextFinder(what);
29: }
30:
31: public boolean matches(Element element) throws Exception {
32: String elementText = innerText(element);
33: return Utils.isEmpty(what)
34: || (!Utils.isEmpty(elementText) && StringUtils
35: .matchesOrEquals(what, elementText));
36: }
37:
38: private String innerText(Element element) {
39: StringBuffer sBuf = new StringBuffer();
40: decendantInnerText(element, sBuf);
41: return sBuf.toString();
42: }
43:
44: private void decendantInnerText(Node node, StringBuffer sBuf) {
45: NodeList nodeList = node.getChildNodes();
46: int size = nodeList.getLength();
47: for (int i = 0; i < size; i++) {
48: Node child = nodeList.item(i);
49: if (child != null && child.getNodeType() == Node.TEXT_NODE) {
50: sBuf.append(child.getNodeValue());
51: decendantInnerText(child, sBuf);
52: }
53: }
54: }
55: }
|