001: /*
002: * SimpleKey.java
003: *
004: * Created on January 31, 2004, 6:28 PM
005: */
006:
007: package org.netbeans.actions.simple;
008:
009: import java.lang.reflect.Method;
010: import java.util.Map;
011: import org.xml.sax.SAXException;
012:
013: /**
014: *
015: * @author Tim Boudreau
016: */
017: public class SimpleKey {
018: private String value;
019: private String method;
020: private String clazz;
021: private boolean mustContain;
022: private String mustMatch;
023:
024: /** Creates a new instance of SimpleKey */
025: public SimpleKey(String value, String method, String clazz,
026: boolean mustContain, String mustMatch) throws SAXException {
027: this .value = value;
028: this .method = method;
029: this .clazz = clazz;
030: this .mustContain = mustContain;
031: this .mustMatch = mustMatch;
032: if ((clazz == null) != (method == null)) {
033: throw new SAXException(
034: "Key must define both a class and a "
035: + "method to call on it if it defines one. Class: "
036: + clazz + " method " + method + " name "
037: + value);
038: }
039: if (clazz != null && mustMatch != null) {
040: throw new SAXException(
041: "A key may define a method to call, or a value to match from the map, not both. Key:"
042: + value);
043: }
044: System.err.println("SimpleKey: " + value + " mustContain "
045: + mustContain + " mustMatch " + mustMatch + " class "
046: + clazz + " method " + method);
047: }
048:
049: public boolean mustTest() {
050: return clazz != null || mustMatch != null;
051: }
052:
053: public boolean isSimpleTest() {
054: return clazz == null;
055: }
056:
057: public Object getValue() {
058: return value;
059: }
060:
061: public boolean isMustContain() {
062: return mustContain;
063: }
064:
065: public boolean test(Map m) {
066: // System.err.println("TEST CONSTRAINT " + value);
067: String s = (String) m.get(value);
068: boolean result = s != null;
069: if (!mustContain) {
070: result = !result;
071: } else if (result) {
072: if (mustMatch != null) {
073: // System.err.println("TESTING " + value + " MUSTMATCH " + mustMatch + " value is " + s);
074: result = s.equals(mustMatch);
075: }
076: }
077: // System.err.println(" SimpleKey test " + value + " result=" + result);
078: if (result && clazz != null) {
079: result = invokeMethod(m, method, clazz);
080: // System.err.println(" Invoked method - result " + result);
081: }
082: return result;
083: }
084:
085: public String toString() {
086: return value;
087: }
088:
089: /** Asymmetric impl of equals & hashcode, but works for a quick demo */
090: public boolean equals(Object o) {
091: if (o instanceof SimpleKey) {
092: SimpleKey oth = (SimpleKey) o;
093: return oth.method == method && oth.clazz == clazz
094: && oth.mustContain == mustContain
095: && oth.mustMatch == mustMatch && oth.value == value;
096: } else if (o instanceof String) {
097: return ((String) o).equals(value);
098: } else {
099: return false;
100: }
101: }
102:
103: public int hashCode() {
104: return value.hashCode();
105: }
106:
107: private boolean invokeMethod(Map m, String method, String clazz) {
108: if (theClass == null) {
109: try {
110: theClass = Class.forName(clazz);
111: } catch (Exception e) {
112: e.printStackTrace();
113: return false;
114: }
115: }
116: Object o = m.get(value);
117: if (o == null) {
118: return false;
119: }
120: if (theMethod == null) {
121: try {
122: theMethod = theClass.getDeclaredMethod(method, null);
123: } catch (Exception e) {
124: e.printStackTrace();
125: return false;
126: }
127:
128: if (theMethod.getReturnType() != Boolean.class
129: && theMethod.getReturnType() != Boolean.TYPE) {
130: throw new IllegalArgumentException("Method " + method
131: + " on " + clazz
132: + " must return boolean or Boolean and "
133: + "take no arguments"); //NOI18N
134: }
135: }
136: Boolean result = Boolean.FALSE;
137: try {
138: result = (Boolean) theMethod.invoke(o, null);
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142: return result.booleanValue();
143: }
144:
145: private Class theClass = null;
146: private Method theMethod = null;
147: }
|