KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > methodset > MethodSet


1 package csdl.jblanket.methodset;
2
3 import csdl.jblanket.modifier.MethodCollector;
4 import csdl.jblanket.util.XmlLabel;
5
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.text.ParseException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.TreeSet JavaDoc;
17
18 import org.jdom.Document;
19 import org.jdom.Element;
20 import org.jdom.JDOMException;
21 import org.jdom.input.SAXBuilder;
22 import org.jdom.output.XMLOutputter;
23 import org.jdom.output.Format;
24
25 /**
26  * Implements the MethodSet abstract data type, which provides a container for managing
27  * information about Java methods collected through dynamic analysis (JBlanket).
28  *
29  * @author Philip Johnson, Joy M. Agustin
30  * @version $Id: MethodSet.java,v 1.2 2005/03/07 01:49:08 timshadel Exp $
31  */

32 public class MethodSet {
33
34   /** Holds the individual Method objects. */
35   private Set JavaDoc elements;
36
37   private static Comparator JavaDoc methodInfoComparator =
38     new Comparator JavaDoc() {
39       /**
40        * Compares the two objects (which are MethodInfos).
41        *
42        * @param o1 The first MethodInfo.
43        * @param o2 The second MethodInfo.
44        * @return Typical compare values.
45        */

46       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
47         MethodInfo info1 = (MethodInfo) o1;
48         MethodInfo info2 = (MethodInfo) o2;
49         return info1.getMethodString().compareTo(info2.getMethodString());
50       }
51     };
52
53   /** Creates a new MethodSet. */
54   public MethodSet() {
55     this.elements = new TreeSet JavaDoc(methodInfoComparator);
56   }
57
58   /**
59    * Adds a new Method instance to the MethodSet. If the Method instance already is in
60    * the set, it is not added.
61    *
62    * @param method The Method instance to be added.
63    * @return True if this MethodSet did not already contain the method.
64    */

65   public synchronized boolean add(MethodInfo method) {
66     return this.elements.add(method);
67   }
68
69   /**
70    * Returns an iterator over this MethodSet. The elements are sorted by fully qualified
71    * class name.
72    *
73    * @return An iterator over the Methods in sorted order.
74    */

75   public synchronized Iterator JavaDoc iterator() {
76     return this.elements.iterator();
77   }
78
79   /**
80    * Returns true if this MethodSet contains this Method instance.
81    *
82    * @param method The method to be found.
83    * @return True if found.
84    */

85   public synchronized boolean contains(MethodInfo method) {
86     return this.elements.contains(method);
87   }
88
89   /**
90    * Returns true if this MethodSet has the same set of elements as the passed MethodSet.
91    *
92    * @param methodSet A MethodSet.
93    * @return True iff the two MethodSets have the same elements.
94    */

95   public synchronized boolean equals(MethodSet methodSet) {
96     return this.elements.equals(methodSet.elements);
97   }
98
99   /**
100    * Removes the specified Method from this MethodSet, if it is present.
101    *
102    * @param method The method to be removed.
103    * @return True if it was present originally.
104    */

105   public synchronized boolean remove(MethodInfo method) {
106     return this.elements.remove(method);
107   }
108
109   /**
110    * Returns true if this MethodSet is empty.
111    *
112    * @return True if empty.
113    */

114   public synchronized boolean isEmpty() {
115     return this.elements.isEmpty();
116   }
117
118   /**
119    * Returns the number of elements in this MethodSet (its cardinality).
120    *
121    * @return The cardinality of the MethodSet.
122    */

123   public synchronized int size() {
124     return this.elements.size();
125   }
126
127   /**
128    * Updates this MethodSet with the difference between this MethodSet and the passed
129    * MethodSet.
130    *
131    * @param methodSet The methodSet whose elements are to be removed from this methodSet.
132    * @return This MethodSet modified with the difference between the two.
133    */

134   public synchronized MethodSet difference(MethodSet methodSet) {
135     MethodSet diffSet = new MethodSet();
136     for (Iterator JavaDoc i = this.iterator(); i.hasNext(); ) {
137       MethodInfo method = (MethodInfo) i.next();
138       if (!methodSet.contains(method)) {
139         diffSet.add(method);
140       }
141     }
142     this.elements = diffSet.elements;
143     return this;
144   }
145
146   /**
147    * Updates this MethodSet with the intersection of this MethodSet and the passed
148    * MethodSet.
149    *
150    * @param methodSet The methodSet whose elements are to be removed from this methodSet.
151    * @return This MethodSet modified with the difference between the two.
152    */

153   public synchronized MethodSet intersection(MethodSet methodSet) {
154     MethodSet intersectSet = new MethodSet();
155     for (Iterator JavaDoc i = this.iterator(); i.hasNext(); ) {
156       MethodInfo method = (MethodInfo) i.next();
157       if (methodSet.contains(method)) {
158         intersectSet.add(method);
159       }
160     }
161     this.elements = intersectSet.elements;
162     return this;
163   }
164
165   /**
166    * Updates this MethodSet with the union of this MethodSet and the passed MethodSet.
167    *
168    * @param methodSet The methodSet whose elements this methodSet is unioned with.
169    * @return This MethodSet modified with the union of the two.
170    */

171   public synchronized MethodSet union(MethodSet methodSet) {
172     MethodSet unionSet = methodSet;
173     for (Iterator JavaDoc i = this.iterator(); i.hasNext(); ) {
174       MethodInfo method = (MethodInfo) i.next();
175       if (!methodSet.contains(method)) {
176         unionSet.add(method);
177       }
178     }
179     this.elements = unionSet.elements;
180     return this;
181   }
182
183   /**
184    * Adds the Method instances found in the passed stream to this MethodSet. The
185    * InputStream is assumed to contain a MethodSet in XML format that conforms to
186    * the MethodSet.dtd specification. The returned time stamp is formatted as
187    * "MMM dd, yyyy HH:mm:ss" where
188    * <p>
189    * MMM - month<br>
190    * dd - day<br>
191    * yyyy - year<br>
192    * HH - hour<br>
193    * mm - minutes<br>
194    * ss - seconds<br>
195    *
196    * @param stream The InputStream from which to construct the MethodSet.
197    * @return time stamp stored in file, null if there is none.
198    * @throws IOException If an error occurs during load.
199    * @throws ParseException If cannot parse time stamp from file.
200    */

201   public synchronized Date JavaDoc load(InputStream JavaDoc stream) throws IOException JavaDoc, ParseException JavaDoc {
202
203     SAXBuilder builder = new SAXBuilder();
204     Document methodSetDocument;
205     try {
206       methodSetDocument = builder.build(stream);
207     }
208     catch (JDOMException e) {
209       throw new IOException JavaDoc("Error during load of MethodSet XML from inputstream.");
210     }
211
212     // load method information
213
Element methodSetElement = methodSetDocument.getRootElement();
214     for (Iterator JavaDoc i = methodSetElement.getChildren().iterator(); i.hasNext(); ) {
215       Element methodElement = (Element) i.next();
216       String JavaDoc className = methodElement.getAttributeValue(XmlLabel.CLASS_ATTRIBUTE.toString());
217       String JavaDoc methodName = methodElement.getAttributeValue(XmlLabel.METHOD_ATTRIBUTE.toString());
218       List JavaDoc parameterTypeList = new ArrayList JavaDoc();
219
220       for (Iterator JavaDoc j = methodElement.getChildren(XmlLabel.PARAMETER.toString()).iterator();
221         j.hasNext(); ) {
222         Element parameter = (Element) j.next();
223         parameterTypeList.add(parameter.getAttributeValue(XmlLabel.TYPE_ATTRIBUTE.toString()));
224       }
225
226       this.elements.add(new MethodInfo(className, methodName, parameterTypeList));
227     }
228
229     // return timeStamp stored in file
230
String JavaDoc timeStamp = methodSetElement.getAttributeValue(XmlLabel.TIMESTAMP_ATTRIBUTE.toString());
231     return MethodCollector.getDateFormat().parse(timeStamp);
232   }
233
234   /**
235    * Writes this MethodSet in XML format to the passed output stream. If
236    * <code>className</code> or <code>timeStamp</code> is null, "class" or "timestamp"
237    * attribute for "MethodSet" in output file is set to "" (empty String). This
238    * behavior can occur during the XML transformation for the JBlanket report.
239    *
240    * @param ostream the OutputStream the MethodSet will be written to.
241    * @param className the name of the class, if one exists.
242    * @param timeStamp the time stamp to include in this file.
243    * @exception IOException if errors during writing.
244    */

245   public synchronized void store(OutputStream JavaDoc ostream, String JavaDoc className, Date JavaDoc timeStamp)
246      throws IOException JavaDoc {
247     XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
248     Document methodSetDocument = new Document(createRoot(className, timeStamp));
249     outputter.output(methodSetDocument, ostream);
250   }
251
252   /**
253    * Creates a Document object from this MethodSet.
254    *
255    * @param className the name of the class, if one exists.
256    * @param timeStamp the time stamp to include in this file.
257    * @return the the new Document.
258    */

259   public synchronized Element createRoot(String JavaDoc className, Date JavaDoc timeStamp) {
260
261     // create the root element in the document
262
Element methodSetRoot = new Element(XmlLabel.METHODSET.toString());
263
264     if (className != null) {
265       methodSetRoot.setAttribute(XmlLabel.NAME_ATTRIBUTE.toString(), className);
266     }
267     else {
268       methodSetRoot.setAttribute(XmlLabel.NAME_ATTRIBUTE.toString(), "");
269     }
270     methodSetRoot.setAttribute(XmlLabel.SIZE_ATTRIBUTE.toString(),
271                                (new Integer JavaDoc(this.elements.size())).toString());
272
273     if (timeStamp != null) {
274       String JavaDoc timeStampString = MethodCollector.getDateFormat().format(timeStamp);
275       methodSetRoot.setAttribute(XmlLabel.TIMESTAMP_ATTRIBUTE.toString(), timeStampString);
276     }
277     else {
278       methodSetRoot.setAttribute(XmlLabel.TIMESTAMP_ATTRIBUTE.toString(), "");
279     }
280
281     // process each element
282
for (Iterator JavaDoc i = this.elements.iterator(); i.hasNext(); ) {
283
284       MethodInfo method = (MethodInfo) i.next();
285
286       // add the method element in XML file
287
Element methodElement = new Element(XmlLabel.METHOD.toString());
288       methodSetRoot.addContent(methodElement);
289       methodElement.setAttribute(XmlLabel.CLASS_ATTRIBUTE.toString(), method.getClassName());
290       methodElement.setAttribute(XmlLabel.METHOD_ATTRIBUTE.toString(), method.getMethodName());
291
292       // process each parameter type
293
for (Iterator JavaDoc j = method.getParameterTypeList().iterator(); j.hasNext(); ) {
294         String JavaDoc parameterType = (String JavaDoc) j.next();
295         Element parameterElement = new Element(XmlLabel.PARAMETER.toString());
296         methodElement.addContent(parameterElement);
297         parameterElement.setAttribute(XmlLabel.TYPE_ATTRIBUTE.toString(), parameterType);
298       }
299     }
300     return methodSetRoot;
301   }
302
303   /**
304    * Provides a readable representation of the MethodSet.
305    *
306    * @return The MethodSet as a string.
307    */

308   public synchronized String JavaDoc toString() {
309     return "[MethodSet " + this.elements + "]";
310   }
311 }
312
Popular Tags