01: package watij.finders;
02:
03: import org.w3c.dom.Attr;
04: import org.w3c.dom.Element;
05: import watij.utilities.StringUtils;
06: import watij.utilities.Utils;
07: import watij.utilities.Debug;
08:
09: public class AttributeFinder extends BaseFinder implements Symbol {
10:
11: String name, what;
12:
13: public AttributeFinder(String name) {
14: this .name = name;
15: }
16:
17: public AttributeFinder(String name, String what) {
18: this .name = name;
19: this .what = what;
20: }
21:
22: public Finder newFinder(String what) throws Exception {
23: return new AttributeFinder(name, what);
24: }
25:
26: public boolean matches(Element element) throws Exception {
27: Debug.getInstance().println(
28: "element tag =" + element.getTagName());
29: Debug.getInstance().println("name=" + name + " what=" + what);
30: return matches(element.getAttributeNode(name), what);
31: }
32:
33: protected boolean matches(Attr attr, String what) {
34: Debug
35: .getInstance()
36: .println(
37: "Begin AttributeFinder.matches(Attr attr, String what)");
38: Debug.getInstance().println("what=" + what);
39: Debug.getInstance().println("attr=" + attr);
40: if (attr == null) {
41: Debug.getInstance().println("returning false");
42: return false;
43: }
44: Debug.getInstance().println(
45: "attr name = " + attr.getName() + " attr value = "
46: + attr.getValue());
47: if (matchesNameWithValueEmpty(attr.getName(), what)) {
48: Debug.getInstance().println("returning true");
49: return true;
50: }
51: if (matchesNameAndValue(attr.getName(), attr.getValue(), what)) {
52: Debug.getInstance().println("returning true");
53: return true;
54: }
55: Debug.getInstance().println("returning false");
56: return false;
57: }
58:
59: protected boolean matchesNameWithValueEmpty(String attrName,
60: String what) {
61: return Utils.isEmpty(what) && attrName.equals(name);
62: }
63:
64: protected boolean matchesNameAndValue(String attrName,
65: String attrValue, String what) {
66: return attrName.equals(name)
67: && StringUtils.matchesOrEquals(what, attrValue);
68: }
69: }
|