1 package jdiff; 2 3 import com.sun.javadoc.*; 4 5 import java.util.*; 6 import java.io.*; 7 import java.lang.reflect.*; import java.lang.Runtime ; 9 import java.lang.Process ; 10 11 17 public class JDiff extends Doclet { 18 19 25 public static boolean start(RootDoc root) { 26 if (root != null) 27 System.out.println("JDiff: doclet started ..."); 28 JDiff jd = new JDiff(); 29 return jd.startGeneration(root); 30 } 31 32 38 protected boolean startGeneration(RootDoc newRoot) { 39 long startTime = System.currentTimeMillis(); 40 41 if (writeXML) { 44 RootDocToXML.writeXML(newRoot); 45 } 46 47 if (compareAPIs) { 48 String tempOldFileName = oldFileName; 49 if (oldDirectory != null) { 50 tempOldFileName = oldDirectory; 51 if (!tempOldFileName.endsWith(JDiff.DIR_SEP)) { 52 tempOldFileName += JDiff.DIR_SEP; 53 } 54 tempOldFileName += oldFileName; 55 } 56 57 File f = new File(tempOldFileName); 59 if (!f.exists()) { 60 System.out.println("Error: file '" + tempOldFileName + "' does not exist for the old API"); 61 return false; 62 } 63 65 String tempNewFileName = newFileName; 66 if (newDirectory != null) { 67 tempNewFileName = newDirectory; 68 if (!tempNewFileName.endsWith(JDiff.DIR_SEP)) { 69 tempNewFileName += JDiff.DIR_SEP; 70 } 71 tempNewFileName += newFileName; 72 } 73 f = new File(tempNewFileName); 74 if (!f.exists()) { 75 System.out.println("Error: file '" + tempNewFileName + "' does not exist for the new API"); 76 return false; 77 } 78 79 System.out.print("JDiff: reading the old API in from file '" + tempOldFileName + "'..."); 82 API oldAPI = XMLToAPI.readFile(tempOldFileName, false, oldFileName); 84 85 System.out.print("JDiff: reading the new API in from file '" + tempNewFileName + "'..."); 88 API newAPI = XMLToAPI.readFile(tempNewFileName, true, newFileName); 90 91 APIComparator comp = new APIComparator(); 93 94 comp.compareAPIs(oldAPI, newAPI); 95 96 int suffix = oldFileName.lastIndexOf('.'); 100 String commentsFileName = "user_comments_for_" + oldFileName.substring(0, suffix); 101 suffix = newFileName.lastIndexOf('.'); 102 commentsFileName += "_to_" + newFileName.substring(0, suffix) + ".xml"; 103 commentsFileName = commentsFileName.replace(' ', '_'); 104 if (HTMLReportGenerator.outputDir != null) 105 commentsFileName = HTMLReportGenerator.outputDir + DIR_SEP + commentsFileName; 106 System.out.println("JDiff: reading the comments in from file '" + commentsFileName + "'..."); 107 Comments existingComments = Comments.readFile(commentsFileName); 108 if (existingComments == null) 109 System.out.println(" (this will be created)"); 110 111 HTMLReportGenerator reporter = new HTMLReportGenerator(); 113 reporter.generate(comp, existingComments); 114 115 Comments newComments = reporter.getNewComments(); 118 Comments.noteDifferences(existingComments, newComments); 119 120 System.out.println("JDiff: writing the comments out to file '" + commentsFileName + "'..."); 123 Comments.writeFile(commentsFileName, newComments); 124 } 125 126 System.out.print("JDiff: finished (took " + (System.currentTimeMillis() - startTime)/1000 + "s"); 127 if (writeXML) 128 System.out.println(", not including scanning the source files)."); 129 else if (compareAPIs) 130 System.out.println(")."); 131 return true; 132 } 133 134 138 146 public static int optionLength(String option) { 147 return Options.optionLength(option); 148 } 149 150 159 public static boolean validOptions(String [][] options, 160 DocErrorReporter reporter) { 161 return Options.validOptions(options, reporter); 162 } 163 164 169 public static void main(String [] args) { 170 if (args.length == 0) { 171 System.out.println("Looking for a local 'build.xml' configuration file"); 173 } else if (args.length == 1) { 174 if (args[0].compareTo("-help") == 0 || 175 args[0].compareTo("-h") == 0 || 176 args[0].compareTo("?") == 0) { 177 showUsage(); 178 } else if (args[0].compareTo("-version") == 0) { 179 System.out.println("JDiff version: " + JDiff.version); 180 } 181 return; 182 } 183 int rc = runAnt(args); 184 return; 185 } 186 187 190 public static void showUsage() { 191 System.out.println("usage: java jdiff.JDiff [-version] [-buildfile <XML configuration file>]"); 192 System.out.println("If no build file is specified, the local build.xml file is used."); 193 } 194 195 200 public static int runAnt(String [] args) { 201 String className = null; 202 Class c = null; 203 try { 204 className = "org.apache.tools.ant.Main"; 205 c = Class.forName(className); 206 } catch (ClassNotFoundException e1) { 207 System.err.println("Error: ant.jar not found on the classpath"); 208 return -1; 209 } 210 try { 211 Class [] methodArgTypes = new Class [1]; 212 methodArgTypes[0] = args.getClass(); 213 Method mainMethod = c.getMethod("main", methodArgTypes); 214 Object [] methodArgs = new Object [1]; 215 methodArgs[0] = args; 216 Integer res = (Integer )mainMethod.invoke(null, methodArgs); 218 System.gc(); return res.intValue(); 220 } catch (NoSuchMethodException e2) { 221 System.err.println("Error: method \"main\" not found"); 222 e2.printStackTrace(); 223 } catch (IllegalAccessException e4) { 224 System.err.println("Error: class not permitted to be instantiated"); 225 e4.printStackTrace(); 226 } catch (InvocationTargetException e5) { 227 System.err.println("Error: method \"main\" could not be invoked"); 228 e5.printStackTrace(); 229 } catch (Exception e6) { 230 System.err.println("Error: "); 231 e6.printStackTrace(); 232 } 233 System.gc(); return -1; 235 } 236 237 241 static String oldFileName = "old_java.xml"; 242 243 247 static String oldDirectory = null; 248 249 253 static String newFileName = "new_java.xml"; 254 255 259 static String newDirectory = null; 260 261 262 static boolean writeXML = false; 263 264 265 static boolean compareAPIs = false; 266 267 270 static String DIR_SEP = System.getProperty("file.separator"); 271 272 273 static final String jDiffLocation = "http://www.jdiff.org"; 274 275 static final String authorEmail = "doar@pobox.com"; 276 277 278 static final String jDiffDescription = "JDiff is a Javadoc doclet which generates an HTML report of all the packages, classes, constructors, methods, and fields which have been removed, added or changed in any way, including their documentation, when two APIs are compared."; 279 280 static final String jDiffKeywords = "diff, jdiff, javadiff, java diff, java difference, API difference, difference between two APIs, API diff, Javadoc, doclet"; 281 282 283 static final String version = "1.0.10"; 284 285 286 static String javaVersion = System.getProperty("java.version"); 287 288 289 private static boolean trace = false; 290 291 } | Popular Tags |