1 package jdiff; 2 3 import java.io.*; 4 import java.util.*; 5 6 7 import org.xml.sax.Attributes ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.SAXParseException ; 10 import org.xml.sax.XMLReader ; 11 import org.xml.sax.InputSource ; 12 import org.xml.sax.helpers.*; 13 14 22 public class XMLToAPI { 23 24 25 private static API api_ = null; 26 27 28 private XMLToAPI() { 29 } 30 31 41 public static API readFile(String filename, boolean createGlobalComments, 42 String apiName) { 43 api_ = new API(); 45 api_.name_ = apiName; try { 47 XMLReader parser = null; 48 DefaultHandler handler = new APIHandler(api_, createGlobalComments); 49 try { 50 String parserName = System.getProperty("org.xml.sax.driver"); 51 if (parserName == null) { 52 parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); 53 } else { 54 parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); 57 } 58 } catch (SAXException saxe) { 59 System.out.println("SAXException: " + saxe); 60 saxe.printStackTrace(); 61 System.exit(1); 62 } 63 if (validateXML) { 64 parser.setFeature("http://xml.org/sax/features/namespaces", true); 65 parser.setFeature("http://xml.org/sax/features/validation", true); 66 parser.setFeature("http://apache.org/xml/features/validation/schema", true); 67 } 68 69 parser.setContentHandler(handler); 70 parser.setErrorHandler(handler); 71 parser.parse(new InputSource (new FileInputStream(new File(filename)))); 72 } catch(org.xml.sax.SAXNotRecognizedException snre) { 73 System.out.println("SAX Parser does not recognize feature: " + snre); 74 snre.printStackTrace(); 75 System.exit(1); 76 } catch(org.xml.sax.SAXNotSupportedException snse) { 77 System.out.println("SAX Parser feature is not supported: " + snse); 78 snse.printStackTrace(); 79 System.exit(1); 80 } catch(org.xml.sax.SAXException saxe) { 81 System.out.println("SAX Exception parsing file '" + filename + "' : " + saxe); 82 saxe.printStackTrace(); 83 System.exit(1); 84 } catch(java.io.IOException ioe) { 85 System.out.println("IOException parsing file '" + filename + "' : " + ioe); 86 ioe.printStackTrace(); 87 System.exit(1); 88 } 89 90 addInheritedElements(); 92 return api_; 93 } 95 98 public static void addInheritedElements() { 99 Iterator iter = api_.packages_.iterator(); 100 while (iter.hasNext()) { 101 PackageAPI pkg = (PackageAPI)(iter.next()); 102 Iterator iter2 = pkg.classes_.iterator(); 103 while (iter2.hasNext()) { 104 ClassAPI cls = (ClassAPI)(iter2.next()); 105 if (cls.extends_ != null) { 107 ClassAPI parent = (ClassAPI)api_.classes_.get(cls.extends_); 108 if (parent != null) 109 addInheritedElements(cls, parent, cls.extends_); 110 } 111 if (cls.implements_.size() != 0) { 112 Iterator iter3 = cls.implements_.iterator(); 113 while (iter3.hasNext()) { 114 String implName = (String )(iter3.next()); 115 ClassAPI parent = (ClassAPI)api_.classes_.get(implName); 116 if (parent != null) 117 addInheritedElements(cls, parent, implName); 118 } 119 } 120 } } } 123 124 136 public static void addInheritedElements(ClassAPI child, ClassAPI parent, 137 String fqParentName) { 138 if (parent.methods_.size() != 0) { 139 Iterator iter = parent.methods_.iterator(); 140 while (iter.hasNext()) { 141 MethodAPI m = (MethodAPI)(iter.next()); 142 boolean overridden = false; 144 Iterator iter2 = child.methods_.iterator(); 145 while (iter2.hasNext()) { 146 MethodAPI localM = (MethodAPI)(iter2.next()); 147 if (localM.name_.compareTo(m.name_) == 0 && 148 localM.getSignature().compareTo(m.getSignature()) == 0) 149 overridden = true; 150 } 151 if (!overridden && m.inheritedFrom_ == null && 152 m.modifiers_.visibility != null && 153 m.modifiers_.visibility.compareTo("private") != 0) { 154 MethodAPI m2 = new MethodAPI(m); 155 m2.inheritedFrom_ = fqParentName; 156 child.methods_.add(m2); 157 } 158 } 159 } 160 if (parent.fields_.size() != 0) { 161 Iterator iter = parent.fields_.iterator(); 162 while (iter.hasNext()) { 163 FieldAPI f = (FieldAPI)(iter.next()); 164 if (child.fields_.indexOf(f) == -1 && 165 f.inheritedFrom_ == null && 166 f.modifiers_.visibility != null && 167 f.modifiers_.visibility.compareTo("private") != 0) { 168 FieldAPI f2 = new FieldAPI(f); 169 f2.inheritedFrom_ = fqParentName; 170 child.fields_.add(f2); 171 } 172 } 173 } 174 175 if (parent.extends_ != null) { 177 ClassAPI parent2 = (ClassAPI)api_.classes_.get(parent.extends_); 178 if (parent2 != null) 179 addInheritedElements(child, parent2, parent.extends_); 180 } 181 if (parent.implements_.size() != 0) { 182 Iterator iter3 = parent.implements_.iterator(); 183 while (iter3.hasNext()) { 184 String implName = (String )(iter3.next()); 185 ClassAPI parent2 = (ClassAPI)api_.classes_.get(implName); 186 if (parent2 != null) 187 addInheritedElements(child, parent2, implName); 188 } 189 } 190 } 191 192 196 201 public static void nameAPI(String name) { 202 if (name == null) { 203 System.out.println("Error: no API identifier found in the XML file '" + api_.name_ + "'"); 204 System.exit(3); 205 } 206 String filename2 = name.replace(' ','_'); 209 filename2 += ".xml"; 210 if (filename2.compareTo(api_.name_) != 0) { 211 System.out.println("Warning: API identifier in the XML file (" + 212 name + ") differs from the name of the file '" + 213 api_.name_ + "'"); 214 } 215 api_.name_ = name; 216 } 217 218 223 public static void addPackage(String name) { 224 api_.currPkg_ = new PackageAPI(name); 225 api_.packages_.add(api_.currPkg_); 226 } 227 228 235 public static void addClass(String name, String parent, 236 boolean isAbstract, 237 Modifiers modifiers) { 238 api_.currClass_ = new ClassAPI(name, parent, false, isAbstract, modifiers); 239 api_.currPkg_.classes_.add(api_.currClass_); 240 String fqName = api_.currPkg_.name_ + "." + name; 241 ClassAPI caOld = (ClassAPI)api_.classes_.put(fqName, api_.currClass_); 242 if (caOld != null) { 243 System.out.println("Warning: duplicate class : " + fqName + " found. Using the first instance only."); 244 } 245 } 246 247 255 public static void addInterface(String name, String parent, 256 boolean isAbstract, 257 Modifiers modifiers) { 258 api_.currClass_ = new ClassAPI(name, parent, true, isAbstract, modifiers); 259 api_.currPkg_.classes_.add(api_.currClass_); 260 } 261 262 268 public static void addImplements(String name) { 269 api_.currClass_.implements_.add(name); 270 } 271 272 279 public static void addCtor(String type, Modifiers modifiers) { 280 String t = type; 281 if (t == null) 282 t = "void"; 283 api_.currCtor_ = new ConstructorAPI(t, modifiers); 284 api_.currClass_.ctors_.add(api_.currCtor_); 285 } 286 287 294 public static void addMethod(String name, String returnType, 295 boolean isAbstract, boolean isNative, 296 boolean isSynchronized, Modifiers modifiers) { 297 String rt = returnType; 298 if (rt == null) 299 rt = "void"; 300 api_.currMethod_ = new MethodAPI(name, rt, isAbstract, isNative, 301 isSynchronized, modifiers); 302 api_.currClass_.methods_.add(api_.currMethod_); 303 } 304 305 312 public static void addField(String name, String type, boolean isTransient, 313 boolean isVolatile, String value, Modifiers modifiers) { 314 String t = type; 315 if (t == null) 316 t = "void"; 317 api_.currField_ = new FieldAPI(name, t, isTransient, isVolatile, value, modifiers); 318 api_.currClass_.fields_.add(api_.currField_); 319 } 320 321 329 public static void addParam(String name, String type) { 330 String t = type; 331 if (t == null) 332 t = "void"; 333 ParamAPI paramAPI = new ParamAPI(name, t); 334 api_.currMethod_.params_.add(paramAPI); 335 } 336 337 346 public static void addException(String name, String type, String currElement) { 347 String exceptionId = type; 348 if (type == null || !showExceptionTypes) 349 exceptionId = name; 350 if (currElement.compareTo("method") == 0) { 351 if (api_.currMethod_.exceptions_.compareTo("no exceptions") == 0) 352 api_.currMethod_.exceptions_ = exceptionId; 353 else 354 api_.currMethod_.exceptions_ += ", " + exceptionId; 355 } else { 356 if (api_.currCtor_.exceptions_.compareTo("no exceptions") == 0) 357 api_.currCtor_.exceptions_ = exceptionId; 358 else 359 api_.currCtor_.exceptions_ += ", " + exceptionId; 360 } 361 } 362 363 368 public static boolean validateXML = false; 369 370 376 private static boolean showExceptionTypes = true; 377 } 378 | Popular Tags |