1 22 package org.aspectj.tools.ajdoc; 23 24 import org.aspectj.compiler.base.CompilerErrors; 25 import org.aspectj.compiler.base.ExitRequestException; 26 import org.aspectj.compiler.base.InternalCompilerError; 27 28 import com.sun.javadoc.RootDoc; 29 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 40 public class Ajdoc extends AjdocCompiler { 41 42 private final static int VERBOSE = 1; 43 private final static int WARNINGS = 4; 44 45 private DocletProxy docletProxy; 46 private RootDocMaker rootDocMaker; 47 private String source = null; 48 private String extdirs = null; 49 private String locale = null; 50 private String encoding = null; 51 private String sourcepath = null; 52 private String classpath = null; 53 private String bootclasspath = null; 54 private int verbosity = WARNINGS; 55 private List filenamesAndPackages = new ArrayList (); 56 private List options = new ArrayList (); 57 58 public Ajdoc(String programName) { 59 super(programName); 60 } 61 62 public Ajdoc() { 63 this("ajdoc"); 64 } 65 66 73 public int execute(String [] args) { 74 try { 75 return doc(args) && err.getNumErrors() == 0 ? 0 : 1; 76 } catch (ExitRequestException exit) { 77 return exit.getValue(); 78 } catch (CompilerErrors err) { 79 return err.errors; } catch (InternalCompilerError error) { handleInternalError(error.uncaughtThrowable); 82 error.showWhere(new PrintWriter (System.err)); 83 return -2; 84 } catch (Exception e) { 85 e.printStackTrace(System.err); 86 err.internalError("internal_msg", e); 87 return 1; 88 } 89 } 90 91 92 public void handleInternalError(Throwable uncaughtThrowable) { 93 System.err.println("An internal error occured in Ajdoc invocation of AspectJ-" 94 +getCompiler().getVersion()); 95 System.err.println(uncaughtThrowable.toString()); 96 uncaughtThrowable.printStackTrace(System.err); 97 System.err.println(); 98 } 99 100 107 public boolean doc(String [] args) { 108 109 long start = System.currentTimeMillis(); 110 111 if (args == null) { 112 err.error("internal_error", "Arguments cannot be null"); 113 return false; 114 } 115 116 try { 117 args = expandAndCreateDoclet(args); 118 } catch (FileNotFoundException e) { 119 err.error("file_not_found_exception", e.getMessage()); 120 return false; 121 } catch (IOException e) { 122 err.error("io_exception", e.getMessage()); 123 return false; 124 } 125 126 for (int i = 0; i < args.length;) { 127 String arg = args[i++]; 128 if (arg.equals("-overview")) { 129 set(args, i++); 130 } else if (arg.equals("-public")) { 131 set(arg); 132 if (filter != null) { 133 err.error("argument_already_seen", arg); 134 } else { 135 setFilter(AccessChecker.PUBLIC, arg); 136 } 137 } else if (arg.equals("-protected")) { 138 set(arg); 139 if (filter != null) { 140 err.error("argument_already_seen", arg); 141 } else { 142 setFilter(AccessChecker.PROTECTED, arg); 143 } 144 } else if (arg.equals("-package")) { 145 set(arg); 146 if (filter != null) { 147 err.error("argument_already_seen", arg); 148 } else { 149 setFilter(AccessChecker.PACKAGE, arg); 150 } 151 } else if (arg.equals("-private")) { 152 set(arg); 153 if (filter != null) { 154 err.error("argument_already_seen", arg); 155 } else { 156 setFilter(AccessChecker.PRIVATE, arg); 157 } 158 } else if (arg.equals("-help")) { 159 usage(0); 160 } else if (arg.equals("-sourcepath")) { 161 if (sourcepath != null) { 162 usage("argument_already_seen", arg); 163 } 164 sourcepath = set(args, i++); 165 }else if (arg.equals("-classpath")) { 166 if (classpath != null) { 167 usage("argument_already_seen", arg); 168 } 169 classpath = set(args, i++); 170 }else if (arg.equals("-bootclasspath")) { 171 if (bootclasspath != null) { 172 usage("argument_already_seen", arg); 173 } 174 bootclasspath = set(args, i++); 175 }else if (arg.equals("-source")) { 176 if (source != null) { 177 usage("argument_already_seen", arg); 178 } 179 source = set(args, i++); 180 } else if (arg.equals("-extdirs")) { 181 if (extdirs != null) { 182 usage("argument_already_seen", arg); 183 } 184 extdirs = set(args, i++); 185 } else if (arg.equals("-verbose")) { 186 set(arg); 187 verbosity |= VERBOSE; 188 } else if (arg.equals("-locale")) { 189 if (locale != null) { 190 usage("argument_already_seen", arg); 191 } 192 set(args, i++); 193 } else if (arg.equals("-encoding")) { 194 if (encoding != null) { 195 usage("argument_already_seen", arg); 196 } 197 encoding = set(args, i++); 198 } else if (arg.equals("-compiler")) { 199 err.warning("usage_help", "-compiler option ignored"); 200 } else if (arg.equals("-debug")) { 201 err.warning("usage_help", "-debug option disabled"); 202 } else if (arg.startsWith("-J")) { if (arg.length() == 2) continue; 204 String rest = arg.substring(2); 205 int ieq = rest.indexOf('='); 206 String key, val; 207 if (ieq != -1) { 208 key = rest.substring(0, ieq); 209 val = rest.substring(ieq+1); 210 } else { 211 key = rest; 212 val = ""; 213 } 214 System.setProperty(key, val); 215 } else if (arg.startsWith("-")) { 216 int optionLength = docletProxy.optionLength(arg); 217 if (optionLength < 0) { 218 exit(); 219 } else if (optionLength == 0) { 220 usage("invalid_flag", arg); 221 } else if (optionLength > args.length) { 222 usage("requires_argument", arg, optionLength+""); 223 } else { 224 List iargs = new ArrayList (); 225 iargs.add(arg); 226 for (int j = 0; j < optionLength-1; j++) { 227 iargs.add(args[i++]); 228 } 229 set((String [])iargs.toArray(new String [iargs.size()])); 230 } 231 } else { 232 filenamesAndPackages.add(arg); 233 } 234 } 235 if (locale == null) { 236 locale = ""; 237 } 238 if (sourcepath == null) { 239 sourcepath = "."; 240 } 241 try { 242 if (!docletProxy.validOptions(options, err)) { 243 exit(1); 244 } 245 } catch (IOException e) { 246 e.printStackTrace(System.err); 247 err.internalError("internal_msg", e); 248 return false; 249 } 250 if (filenamesAndPackages.size() < 1) { 251 usage("No_packages_or_classes_specified"); 252 return false; 253 } 254 RootDoc rootDoc = null; 255 boolean good = true; 256 try { 257 rootDoc = makeRootDoc(sourcepath, 258 classpath, 259 bootclasspath, 260 extdirs, 261 verbosity, 262 encoding, 263 locale, 264 source, 265 filenamesAndPackages, 266 options, 267 err, 268 programName, 269 getFilter()); 270 } catch (CannotMakeRootDocException e) { 271 err.error("cant_create_root_doc_ex", "AjdocCompiler", e.getMessage()); 272 exit(1); 273 good = false; 274 } 275 good &= rootDoc != null; 276 if (good) { 277 err.notice("generating_docs"); 278 try { 279 good &= docletProxy.start(rootDoc); 280 } catch (IOException e) { 281 e.printStackTrace(System.err); 282 err.internalError("internal_msg", e); 283 return false; 284 } 285 } 286 if ((verbosity & VERBOSE) != 0) { 287 err.notice("done_in", Long.toString(System.currentTimeMillis()-start)); 288 } 289 return good; 290 } 291 292 293 private void usage(String key, String s0, String s1) { 294 err.error(key, s0, s1); 295 usage(1); 296 } 297 298 private void usage(String key, String s0) { 299 usage(key,s0,""); 300 } 301 302 private void usage(String key) { 303 usage(key,""); 304 } 305 306 private void usage() { 307 err.notice("usage_help", programName); 308 if (docletProxy != null) { docletProxy.optionLength("-help"); } 309 } 310 311 private void usage(int exit) { 312 usage(); 313 exit(exit); 314 } 315 316 protected String [] expandAndCreateDoclet(String [] args) throws IOException { 317 List list = new ArrayList (); 318 docletProxy = DocletProxy.DEFAULT; 319 for (int i = 0; i < args.length;) { 320 String arg = args[i++]; 321 if (arg == null || arg.length() < 1) continue; 322 if (arg.charAt(0) == '@') { 323 expandAtFile(arg.substring(1), list); 324 } else if (arg.equals("-argfile")) { 325 if (check(args, i)) { 326 expandAtFile(args[i++], list); 327 } 328 } else if (arg.equals("-doclet")) { 329 err.warning("usage_help", "-doclet option ignored"); 330 } else if (arg.equals("-docletpath")) { 331 err.warning("usage_help", "-docletpath option ignored"); 332 } else if (arg.equals("-standard")) { 333 docletProxy = DocletProxy.STANDARD; 334 } else { 335 list.add(arg); 336 } 337 } 338 return (String [])list.toArray(new String [list.size()]); 339 } 340 341 private static boolean check(String [] args, int i) { 342 return i >= 0 && i < args.length; 343 } 344 345 private String set(String [] args, int i) { 346 String arg = null; 347 if (check(args, i)) { 348 arg = args[i]; 349 set(args[i-1], arg); 350 } else { 351 err.internalError("internal_error", 352 new ArrayIndexOutOfBoundsException (i)); 353 } 354 return arg; 355 } 356 357 private void set(String opt) { 358 set(new String []{opt}); 359 } 360 361 private void set(String opt, String arg) { 362 set(new String []{opt, arg}); 363 } 364 365 private void set(String [] opts) { 366 options.add(opts); 367 } 368 369 protected void internalCompile(List filenames) { 370 super.internalCompile(filenames); 371 } 372 373 private final void exit() { 374 exit(0); 375 } 376 377 private void exit(int exit) { 378 throw new ExitRequestException(exit); 379 } 380 381 private static String classname(Object o) { 382 return o != null ? o.getClass().getName() : "null"; 383 } 384 } 385 | Popular Tags |