001: package csdl.jblanket.report.element;
002:
003: import java.io.PrintWriter;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: /**
009: * Provides a wrapper for the 'Method' element in an XML file. An instance of a MethodElement
010: * represents elements of a method's type signature.
011: * <p>
012: * An example of a method as a MethodElement with no parameters:
013: * <pre>
014: * <Method name="length" package="java.lang.String"/>
015: * </pre>
016: * <p>
017: * An example of a method as a MethodElement with parameters:
018: * <pre>
019: * <Method name="indexOf" package="java.lang.String">
020: * <Parameter type="java.lang.String"/>
021: * <Parameter type="int"/>
022: * </Method>
023: * </pre>
024: *
025: * @author Joy M. Agustin
026: * @version $Id: MethodElement.java,v 1.1 2004/11/07 00:32:39 timshadel Exp $
027: */
028: public class MethodElement implements Comparable {
029:
030: /** Name of the class method belongs to */
031: private String className;
032: /** Name of the method represented */
033: private String methodName;
034: /** Parameters of the method represented */
035: private List parameters;
036:
037: /**
038: * Constructs a new MethodElement object.
039: *
040: * @param className the name of the class <code>methodName</code> belongs to.
041: * @param methodName the name of the method represented by this MethodElement.
042: */
043: public MethodElement(String className, String methodName) {
044:
045: this .className = className;
046: this .methodName = methodName;
047: this .parameters = new ArrayList();
048: }
049:
050: /**
051: * Returns the name of the class of this MethodElement object.
052: *
053: * @return the name of the class of this MethodElement object.
054: */
055: public String getClassName() {
056: return this .className;
057: }
058:
059: /**
060: * Returns the name of the method of this MethodElement object.
061: *
062: * @return the name of the method of this MethodElement object.
063: */
064: public String getMethodName() {
065: return this .methodName;
066: }
067:
068: /**
069: * Adds a parameter to this MethodElement object.
070: *
071: * @param parameter the parameter to add.
072: */
073: public void addParameter(ParameterElement parameter) {
074: this .parameters.add(parameter);
075: }
076:
077: /**
078: * Returns a String representation of this MethodElement as found in an aggregate XML file.
079: *
080: * @return a String representation of this MethodElement.
081: */
082: public String toString() {
083:
084: StringBuffer buffer = new StringBuffer();
085: buffer.append(" <Method class=\"" + this .className + "\"");
086: buffer.append(" method=\"" + this .methodName + "\"");
087:
088: if (this .parameters.isEmpty()) {
089: buffer.append("/>");
090: } else {
091: buffer.append(">\n");
092: for (Iterator i = this .parameters.iterator(); i.hasNext();) {
093: buffer.append("" + i.next() + "\n");
094: }
095: buffer.append(" </Method>");
096: }
097:
098: return buffer.toString();
099: }
100:
101: /**
102: * Writes this MethodElement to writer.
103: *
104: * @param writer points to an output file.
105: */
106: public void write(PrintWriter writer) {
107:
108: writer.write(" <Method class=\"" + this .className + "\"");
109: writer.write(" method=\"" + this .methodName + "\"");
110:
111: if (this .parameters.isEmpty()) {
112: writer.write("/>");
113: } else {
114: writer.write(">\n");
115: for (Iterator i = this .parameters.iterator(); i.hasNext();) {
116: writer.write("" + i.next() + "\n");
117: }
118: writer.write(" </Method>");
119: }
120: }
121:
122: /**
123: * Compares this MethodElement object to <code>obj</code>.
124: *
125: * @param obj the MethodElement object to compare to.
126: * @return negative integer if this MethodElement object comes before <code>obj</code>, zero
127: * if this MethodElement object is equal to <code>obj</code>, or positive integer if
128: * <code>obj</code> comes before this MethodElement object.
129: */
130: public int compareTo(Object obj) {
131:
132: if (this .className.compareTo(((MethodElement) obj)
133: .getClassName()) != 0) {
134: return this .className.compareTo(((MethodElement) obj)
135: .getClassName());
136: }
137:
138: if (this .methodName.compareTo(((MethodElement) obj)
139: .getMethodName()) != 0) {
140: return this .methodName.compareTo(((MethodElement) obj)
141: .getMethodName());
142: } else {
143: StringBuffer this String = new StringBuffer();
144: StringBuffer objString = new StringBuffer();
145:
146: for (Iterator i = this .parameters.iterator(); i.hasNext();) {
147: this String.append(((ParameterElement) i.next())
148: .getType());
149: }
150:
151: for (Iterator i = ((MethodElement) obj).parameters
152: .iterator(); i.hasNext();) {
153: objString.append(((ParameterElement) i.next())
154: .getType());
155: }
156:
157: return thisString.toString()
158: .compareTo(objString.toString());
159: }
160: }
161: }
|