001: package uk.org.ponder.saxalizer;
002:
003: /**
004: * A SAXAccessMethodSpec identifies a mapping from an XML tag name to a Java
005: * method name that is capable of either setting or getting an XML-serialised
006: * subobject (the <it>target object </it). If it is a get method, the signature
007: * of the method must be <code>
008: * <clazz> <methodname>() </code>. If
009: * it is a set method, the signature of the method must be <code>void
010: * <methodname>(<clazz>)</code>.
011: * <p>
012: * Thus we must be clear that <code>clazz</code> is the type returned or taken
013: * from the access method, i.e. the type of the subobject and NOT the type of
014: * the object that the method is a member of.
015: * <p>
016: * The value "*" is allowed for the parameter <code>clazz</code>, which
017: * indicates that the XML tagname will be interpreted as a fully-qualified Java
018: * classname. This class must exist and be a subclass of the actual argument
019: * type of the method identified.
020: */
021:
022: public class SAXAccessMethodSpec implements SAXalizable,
023: SAXalizableAttrs {
024: public static final int GET_METHOD = 0;
025: public static final int SET_METHOD = 1;
026: public static final int FIELD_METHOD = 2;
027: public static final String ACCESS_METHOD = "method";
028: public static final String ACCESS_FIELD = "field";
029: public static final String ACCESS_IGNORE = "ignore";
030: public static final String XML_TAG = "tag";
031: public static final String XML_ATTRIBUTE = "attribute";
032: public static final String XML_BODY = "body";
033:
034: public static final String DEFAULT_ACCESS = ACCESS_METHOD;
035: public static final String DEFAULT_XML_FORM = XML_TAG;
036: // either the tag or attribute name, depending on the value of xmlform.
037: public String xmlname;
038: // if either getmethodname or setmethodname is set, fieldname will not be
039: // set. At least one out of the following three will be.
040: public String getmethodname;
041: public String setmethodname;
042: public String fieldname;
043: // QQQQQ this field need not exist, although the ACCESS strings do.
044: public String accesstype = DEFAULT_ACCESS;
045: public String xmlform = DEFAULT_XML_FORM;
046: public Class clazz;
047:
048: /**
049: * @param tag The tagname that will be used when the target object is
050: * serialised, or "*" if the tag is not known.
051: * @param methodname The method to be called in order to deliver or retrieve
052: * the target object.
053: * @param clazz The class (or a superclass) of the target object
054: */
055: public SAXAccessMethodSpec(String tag, String methodname,
056: Class clazz) {
057: this .xmlname = tag;
058: this .getmethodname = methodname;
059: this .clazz = clazz;
060: }
061:
062: public SAXAccessMethodSpec(String tag, String methodorfieldname,
063: Class clazz, String accesstype) {
064: this .xmlname = tag;
065: this .accesstype = accesstype;
066: if (accesstype.equals(ACCESS_METHOD)) {
067: this .getmethodname = methodorfieldname;
068: } else if (accesstype.equals(ACCESS_FIELD)) {
069: this .fieldname = methodorfieldname;
070: }
071: this .clazz = clazz;
072: }
073:
074: public SAXAccessMethodSpec() {
075:
076: }
077:
078: public boolean isDuplicate(SAXAccessMethodSpec newentry) {
079: if (newentry.setmethodname != null
080: && newentry.setmethodname.equals(setmethodname))
081: return true;
082: if (newentry.getmethodname != null
083: && newentry.getmethodname.equals(getmethodname))
084: return true;
085: if (newentry.fieldname != null
086: && newentry.fieldname.equals(fieldname))
087: return true;
088: return false;
089: }
090:
091: /**
092: * This is a utility method, currently called by MethodAnalyser, for SAMS
093: * returned from getSAXQqqMethod() in order to swap the name supplied for
094: * "methodname" which defaults to referring to a get method into a set method.
095: *
096: * @param toconvert
097: */
098: public static void convertToSetSpec(SAXAccessMethodSpec[] toconvert) {
099: for (int i = 0; i < toconvert.length; ++i) {
100: // Bail out if the swapping has already been performed!!
101: if (toconvert[i].getmethodname != null) {
102: toconvert[i].setmethodname = toconvert[i].getmethodname;
103: toconvert[i].getmethodname = null;
104: }
105: }
106: }
107:
108: /**
109: * This is a utility method, currently called by MethodAnalyser, for SAMS
110: * returned from getSAXQqqAttrMethod() in order to convert swap the name
111: * supplied for "tagname" which defaults to referring to a tag method into an
112: * attribute.
113: *
114: * @param toconvert
115: */
116: public static void convertToAttrSpec(SAXAccessMethodSpec[] toconvert) {
117: for (int i = 0; i < toconvert.length; ++i) {
118: toconvert[i].xmlform = XML_ATTRIBUTE;
119: }
120: }
121:
122: // Yes, SAXAccessMethodSpecs are themselves SAXalizable.
123: public SAXAccessMethodSpec[] getSAXSetMethods() {
124: return new SAXAccessMethodSpec[] {
125: new SAXAccessMethodSpec("getmethod", "getmethodname",
126: String.class, ACCESS_FIELD),
127: new SAXAccessMethodSpec("setmethod", "setmethodname",
128: String.class, ACCESS_FIELD),
129: new SAXAccessMethodSpec("fieldname", "fieldname",
130: String.class, ACCESS_FIELD),
131: new SAXAccessMethodSpec("javaclass", "clazz",
132: Class.class, ACCESS_FIELD) };
133: }
134:
135: public SAXAccessMethodSpec[] getSAXSetAttrMethods() {
136: return new SAXAccessMethodSpec[] {
137: new SAXAccessMethodSpec("xmlname", "xmlname",
138: String.class, ACCESS_FIELD),
139: new SAXAccessMethodSpec("access-type", "accesstype",
140: String.class, ACCESS_FIELD),
141: new SAXAccessMethodSpec("xml-form", "xmlform",
142: String.class, ACCESS_FIELD) };
143: }
144:
145: public String toString() {
146: return "SAXAccessMethodSpec for " + xmlform + " " + xmlname
147: + ", getmethod: " + getmethodname + " setmethod: "
148: + setmethodname + " fieldname: " + fieldname;
149: }
150: }
|