| 1 package csdl.jblanket.methodset; 2 3 import csdl.jblanket.modifier.MethodCollector; 4 import csdl.jblanket.util.XmlLabel; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 import java.text.ParseException ; 10 import java.util.ArrayList ; 11 import java.util.Comparator ; 12 import java.util.Date ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Set ; 16 import java.util.TreeSet ; 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 32 public class MethodSet { 33 34 35 private Set elements; 36 37 private static Comparator methodInfoComparator = 38 new Comparator () { 39 46 public int compare(Object o1, Object o2) { 47 MethodInfo info1 = (MethodInfo) o1; 48 MethodInfo info2 = (MethodInfo) o2; 49 return info1.getMethodString().compareTo(info2.getMethodString()); 50 } 51 }; 52 53 54 public MethodSet() { 55 this.elements = new TreeSet (methodInfoComparator); 56 } 57 58 65 public synchronized boolean add(MethodInfo method) { 66 return this.elements.add(method); 67 } 68 69 75 public synchronized Iterator iterator() { 76 return this.elements.iterator(); 77 } 78 79 85 public synchronized boolean contains(MethodInfo method) { 86 return this.elements.contains(method); 87 } 88 89 95 public synchronized boolean equals(MethodSet methodSet) { 96 return this.elements.equals(methodSet.elements); 97 } 98 99 105 public synchronized boolean remove(MethodInfo method) { 106 return this.elements.remove(method); 107 } 108 109 114 public synchronized boolean isEmpty() { 115 return this.elements.isEmpty(); 116 } 117 118 123 public synchronized int size() { 124 return this.elements.size(); 125 } 126 127 134 public synchronized MethodSet difference(MethodSet methodSet) { 135 MethodSet diffSet = new MethodSet(); 136 for (Iterator 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 153 public synchronized MethodSet intersection(MethodSet methodSet) { 154 MethodSet intersectSet = new MethodSet(); 155 for (Iterator 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 171 public synchronized MethodSet union(MethodSet methodSet) { 172 MethodSet unionSet = methodSet; 173 for (Iterator 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 201 public synchronized Date load(InputStream stream) throws IOException , ParseException { 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 ("Error during load of MethodSet XML from inputstream."); 210 } 211 212 Element methodSetElement = methodSetDocument.getRootElement(); 214 for (Iterator i = methodSetElement.getChildren().iterator(); i.hasNext(); ) { 215 Element methodElement = (Element) i.next(); 216 String className = methodElement.getAttributeValue(XmlLabel.CLASS_ATTRIBUTE.toString()); 217 String methodName = methodElement.getAttributeValue(XmlLabel.METHOD_ATTRIBUTE.toString()); 218 List parameterTypeList = new ArrayList (); 219 220 for (Iterator 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 String timeStamp = methodSetElement.getAttributeValue(XmlLabel.TIMESTAMP_ATTRIBUTE.toString()); 231 return MethodCollector.getDateFormat().parse(timeStamp); 232 } 233 234 245 public synchronized void store(OutputStream ostream, String className, Date timeStamp) 246 throws IOException { 247 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 248 Document methodSetDocument = new Document(createRoot(className, timeStamp)); 249 outputter.output(methodSetDocument, ostream); 250 } 251 252 259 public synchronized Element createRoot(String className, Date timeStamp) { 260 261 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 (this.elements.size())).toString()); 272 273 if (timeStamp != null) { 274 String 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 for (Iterator i = this.elements.iterator(); i.hasNext(); ) { 283 284 MethodInfo method = (MethodInfo) i.next(); 285 286 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 for (Iterator j = method.getParameterTypeList().iterator(); j.hasNext(); ) { 294 String parameterType = (String ) 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 308 public synchronized String toString() { 309 return "[MethodSet " + this.elements + "]"; 310 } 311 } 312 | Popular Tags |