You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileFilter;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.FileReader;
  21. import java.io.FilenameFilter;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Hashtable;
  26. import java.util.Iterator;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.StringTokenizer;
  30. import java.util.Vector;
  31. import org.aspectj.asm.AsmManager;
  32. import org.aspectj.bridge.IMessage;
  33. import org.aspectj.bridge.Version;
  34. import org.aspectj.util.FileUtil;
  35. import org.aspectj.util.LangUtil;
  36. /**
  37. * This is an old implementation of ajdoc that does not use an OO style. However, it does the job, and should serve to evolve a
  38. * lightweight ajdoc implementation until we can make a properly extended javadoc implementation.
  39. *
  40. * @author Mik Kersten
  41. */
  42. public class Main implements Config {
  43. private static final String FAIL_MESSAGE = "> compile failed, exiting ajdoc";
  44. /** Command line options. */
  45. static Vector<String> options;
  46. /** Options to pass to ajc. */
  47. static Vector<String> ajcOptions;
  48. /** All of the files to be processed by ajdoc. */
  49. static Vector<String> filenames;
  50. /** List of files to pass to javadoc. */
  51. static Vector<String> fileList;
  52. /** List of packages to pass to javadoc. */
  53. static Vector<String> packageList;
  54. /** Default to package visiblity. */
  55. static String docModifier = "package";
  56. static Vector<String> sourcepath;
  57. static boolean verboseMode = false;
  58. static boolean packageMode = false;
  59. static boolean authorStandardDocletSwitch = false;
  60. static boolean versionStandardDocletSwitch = false;
  61. static File rootDir = null;
  62. static Hashtable declIDTable = new Hashtable();
  63. static String docDir = ".";
  64. private static boolean deleteTempFilesOnExit = true;
  65. private static boolean aborted = false;
  66. private static IMessage[] errors;
  67. private static boolean shownAjdocUsageMessage = false;
  68. // creating a local variable to enable us to create the ajdocworkingdir
  69. // in a local sandbox during testing
  70. private static String outputWorkingDir = Config.WORKING_DIR;
  71. public static void clearState() {
  72. options = new Vector<String>();
  73. ajcOptions = new Vector<String>();
  74. filenames = new Vector<String>();
  75. fileList = new Vector<String>();
  76. packageList = new Vector<String>();
  77. docModifier = "package";
  78. sourcepath = new Vector<String>();
  79. verboseMode = false;
  80. packageMode = false;
  81. rootDir = null;
  82. declIDTable = new Hashtable();
  83. docDir = ".";
  84. aborted = false;
  85. deleteTempFilesOnExit = true;
  86. }
  87. public static void main(String[] args) {
  88. clearState();
  89. if (!JavadocRunner.has14ToolsAvailable()) {
  90. System.err.println("ajdoc requires a JDK 1.4 or later tools jar - exiting");
  91. aborted = true;
  92. return;
  93. }
  94. // STEP 1: parse the command line and do other global setup
  95. sourcepath.addElement("."); // add the current directory to the classapth
  96. parseCommandLine(args);
  97. rootDir = getRootDir();
  98. File[] inputFiles = new File[filenames.size()];
  99. File[] signatureFiles = new File[filenames.size()];
  100. try {
  101. // create the workingdir if it doesn't exist
  102. if (!(new File(outputWorkingDir).isDirectory())) {
  103. File dir = new File(outputWorkingDir);
  104. dir.mkdir();
  105. if (deleteTempFilesOnExit)
  106. dir.deleteOnExit();
  107. }
  108. for (int i = 0; i < filenames.size(); i++) {
  109. inputFiles[i] = new File(filenames.elementAt(i));
  110. }
  111. // PHASE 0: call ajc
  112. AsmManager model = callAjc(inputFiles);
  113. if (CompilerWrapper.hasErrors()) {
  114. System.out.println(FAIL_MESSAGE);
  115. aborted = true;
  116. errors = CompilerWrapper.getErrors();
  117. return;
  118. }
  119. for (int ii = 0; ii < filenames.size(); ii++) {
  120. signatureFiles[ii] = createSignatureFile(model, inputFiles[ii]);
  121. }
  122. // PHASE 1: generate Signature files (Java with DeclIDs and no bodies).
  123. System.out.println("> Building signature files...");
  124. try {
  125. StubFileGenerator.doFiles(model, declIDTable, inputFiles, signatureFiles);
  126. // Copy package.html and related files over
  127. packageHTML(model, inputFiles);
  128. } catch (DocException d) {
  129. System.err.println(d.getMessage());
  130. return;
  131. }
  132. // PHASE 2: let Javadoc generate HTML (with DeclIDs)
  133. callJavadoc(signatureFiles);
  134. // PHASE 3: add AspectDoc specific stuff to the HTML (and remove the DeclIDS).
  135. decorateHtmlFiles(model, inputFiles);
  136. System.out.println("> Finished.");
  137. } catch (Throwable e) {
  138. handleInternalError(e);
  139. exit(-2);
  140. }
  141. }
  142. /**
  143. * Method to copy over any package.html files that may be part of the documentation so that javadoc generates the
  144. * package-summary properly.
  145. */
  146. private static void packageHTML(AsmManager model, File[] inputFiles) throws IOException {
  147. ArrayList<String> dirList = new ArrayList<String>();
  148. for (File inputFile : inputFiles) {
  149. String packageName = StructureUtil.getPackageDeclarationFromFile(model, inputFile);
  150. // Only copy the package.html file once.
  151. if (dirList.contains(packageName))
  152. continue;
  153. // Check to see if there exist a package.html file for this package.
  154. String dir = inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().lastIndexOf(File.separator));
  155. File input = new File(dir + Config.DIR_SEP_CHAR + "package.html");
  156. File inDir = new File(dir + Config.DIR_SEP_CHAR + "doc-files");
  157. // If it does not exist lets go to the next package.
  158. if (!input.exists()) {
  159. dirList.add(packageName);
  160. continue;
  161. }
  162. String filename = "";
  163. String docFiles = "";
  164. if (packageName != null) {
  165. String pathName = outputWorkingDir + File.separator + packageName.replace('.', File.separatorChar);
  166. File packageDir = new File(pathName);
  167. if (!packageDir.exists()) {
  168. dirList.add(packageName);
  169. continue;
  170. }
  171. packageName = packageName.replace('.', '/'); // !!!
  172. filename = outputWorkingDir + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + "package.html";
  173. docFiles = rootDir.getAbsolutePath() + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + "doc-files";
  174. } else {
  175. filename = outputWorkingDir + Config.DIR_SEP_CHAR + "package.html";
  176. docFiles = rootDir.getAbsolutePath() + Config.DIR_SEP_CHAR + "doc-files";
  177. }
  178. File output = new File(filename);
  179. FileUtil.copyFile(input, output);// Copy package.html
  180. // javadoc doesn't do anything with the doc-files folder so
  181. // we'll just copy it directly to the document location.
  182. if (!inDir.exists())
  183. continue;
  184. File outDir = new File(docFiles);
  185. System.out.println("> Copying folder " + outDir);
  186. FileUtil.copyFile(inDir, outDir);// Copy doc-files folder if it exist
  187. }
  188. }
  189. private static AsmManager callAjc(File[] inputFiles) {
  190. ajcOptions.addElement("-noExit");
  191. ajcOptions.addElement("-XjavadocsInModel"); // TODO: wrong option to force model gen
  192. ajcOptions.addElement("-d");
  193. ajcOptions.addElement(rootDir.getAbsolutePath());
  194. String[] argsToCompiler = new String[ajcOptions.size() + inputFiles.length];
  195. int i = 0;
  196. for (; i < ajcOptions.size(); i++) {
  197. argsToCompiler[i] = ajcOptions.elementAt(i);
  198. }
  199. for (File inputFile : inputFiles) {
  200. argsToCompiler[i] = inputFile.getAbsolutePath();
  201. // System.out.println(">> file to ajc: " + inputFiles[j].getAbsolutePath());
  202. i++;
  203. }
  204. System.out.println("> Calling ajc...");
  205. return CompilerWrapper.executeMain(argsToCompiler);
  206. }
  207. private static void callJavadoc(File[] signatureFiles) throws IOException {
  208. System.out.println("> Calling javadoc...");
  209. String[] javadocargs = null;
  210. List<String> files = new ArrayList<String>();
  211. if (packageMode) {
  212. int numExtraArgs = 2;
  213. if (authorStandardDocletSwitch)
  214. numExtraArgs++;
  215. if (versionStandardDocletSwitch)
  216. numExtraArgs++;
  217. javadocargs = new String[numExtraArgs + options.size() + packageList.size() + fileList.size()];
  218. javadocargs[0] = "-sourcepath";
  219. javadocargs[1] = outputWorkingDir;
  220. int argIndex = 2;
  221. if (authorStandardDocletSwitch) {
  222. javadocargs[argIndex] = "-author";
  223. argIndex++;
  224. }
  225. if (versionStandardDocletSwitch) {
  226. javadocargs[argIndex] = "-version";
  227. }
  228. // javadocargs[1] = getSourcepathAsString();
  229. for (int k = 0; k < options.size(); k++) {
  230. javadocargs[numExtraArgs + k] = options.elementAt(k);
  231. }
  232. for (int k = 0; k < packageList.size(); k++) {
  233. javadocargs[numExtraArgs + options.size() + k] = packageList.elementAt(k);
  234. }
  235. for (int k = 0; k < fileList.size(); k++) {
  236. javadocargs[numExtraArgs + options.size() + packageList.size() + k] = fileList.elementAt(k);
  237. }
  238. if (LangUtil.is19VMOrGreater()) {
  239. options = new Vector<String>();
  240. for (String a: javadocargs) {
  241. options.add(a);
  242. }
  243. }
  244. } else {
  245. javadocargs = new String[options.size() + signatureFiles.length];
  246. for (int k = 0; k < options.size(); k++) {
  247. javadocargs[k] = options.elementAt(k);
  248. }
  249. for (int k = 0; k < signatureFiles.length; k++) {
  250. javadocargs[options.size() + k] = StructureUtil.translateAjPathName(signatureFiles[k].getCanonicalPath());
  251. }
  252. for (File signatureFile : signatureFiles) {
  253. files.add(StructureUtil.translateAjPathName(signatureFile.getCanonicalPath()));
  254. }
  255. }
  256. if (LangUtil.is19VMOrGreater()) {
  257. JavadocRunner.callJavadocViaToolProvider(options, files);
  258. } else {
  259. JavadocRunner.callJavadoc(javadocargs);
  260. }
  261. }
  262. /**
  263. * We start with the known HTML files (the ones that correspond directly to the input files.) As we go along, we may learn that
  264. * Javadoc split one .java file into multiple .html files to handle inner classes or local classes. The html file decorator
  265. * picks that up.
  266. */
  267. private static void decorateHtmlFiles(AsmManager model, File[] inputFiles) throws IOException {
  268. System.out.println("> Decorating html files...");
  269. HtmlDecorator.decorateHTMLFromInputFiles(model, declIDTable, rootDir, inputFiles, docModifier);
  270. System.out.println("> Removing generated tags...");
  271. removeDeclIDsFromFile("index-all.html", true);
  272. removeDeclIDsFromFile("serialized-form.html", true);
  273. if (packageList.size() > 0) {
  274. for (int p = 0; p < packageList.size(); p++) {
  275. removeDeclIDsFromFile(packageList.elementAt(p).replace('.', '/') + Config.DIR_SEP_CHAR
  276. + "package-summary.html", true);
  277. }
  278. } else {
  279. File[] files = rootDir.listFiles();
  280. if (files == null) {
  281. System.err.println("Destination directory is not a directory: " + rootDir.toString());
  282. return;
  283. }
  284. files = FileUtil.listFiles(rootDir, new FileFilter() {
  285. @Override
  286. public boolean accept(File f) {
  287. return f.getName().equals("package-summary.html");
  288. }
  289. });
  290. for (File file : files) {
  291. removeDeclIDsFromFile(file.getAbsolutePath(), false);
  292. }
  293. }
  294. }
  295. private static void removeDeclIDsFromFile(String filename, boolean relativePath) {
  296. // Remove the decl ids from "index-all.html"
  297. File indexFile;
  298. if (relativePath) {
  299. indexFile = new File(docDir + Config.DIR_SEP_CHAR + filename);
  300. } else {
  301. indexFile = new File(filename);
  302. }
  303. try {
  304. if (indexFile.exists()) {
  305. BufferedReader indexFileReader = new BufferedReader(new FileReader(indexFile));
  306. // StringBuffer greatly reduces the time it takes to remove generated tags
  307. StringBuffer indexFileBuffer = new StringBuffer((int) indexFile.length());
  308. String line = indexFileReader.readLine();
  309. while (line != null) {
  310. int indexStart = line.indexOf(Config.DECL_ID_STRING);
  311. int indexEnd = line.indexOf(Config.DECL_ID_TERMINATOR);
  312. if (indexStart != -1 && indexEnd != -1) {
  313. line = line.substring(0, indexStart) + line.substring(indexEnd + Config.DECL_ID_TERMINATOR.length());
  314. }
  315. indexFileBuffer.append(line);
  316. line = indexFileReader.readLine();
  317. }
  318. FileOutputStream fos = new FileOutputStream(indexFile);
  319. fos.write(indexFileBuffer.toString().getBytes());
  320. indexFileReader.close();
  321. fos.close();
  322. }
  323. } catch (IOException ioe) {
  324. // be siltent
  325. }
  326. }
  327. static Vector<String> getSourcePath() {
  328. Vector<String> sourcePath = new Vector<String>();
  329. boolean found = false;
  330. for (int i = 0; i < options.size(); i++) {
  331. String currOption = options.elementAt(i);
  332. if (found && !currOption.startsWith("-")) {
  333. sourcePath.add(currOption);
  334. }
  335. if (currOption.equals("-sourcepath")) {
  336. found = true;
  337. }
  338. }
  339. return sourcePath;
  340. }
  341. static File getRootDir() {
  342. File rootDir = new File(".");
  343. for (int i = 0; i < options.size(); i++) {
  344. if (options.elementAt(i).equals("-d")) {
  345. rootDir = new File(options.elementAt(i + 1));
  346. if (!rootDir.exists()) {
  347. rootDir.mkdir();
  348. // System.out.println( "Destination directory not found: " +
  349. // (String)options.elementAt(i+1) );
  350. // System.exit( -1 );
  351. }
  352. }
  353. }
  354. return rootDir;
  355. }
  356. static File createSignatureFile(AsmManager model, File inputFile) throws IOException {
  357. String packageName = StructureUtil.getPackageDeclarationFromFile(model, inputFile);
  358. String filename = "";
  359. if (packageName != null) {
  360. String pathName = outputWorkingDir + '/' + packageName.replace('.', '/');
  361. File packageDir = new File(pathName);
  362. if (!packageDir.exists()) {
  363. packageDir.mkdirs();
  364. if (deleteTempFilesOnExit)
  365. packageDir.deleteOnExit();
  366. }
  367. // verifyPackageDirExists(packageName, null);
  368. packageName = packageName.replace('.', '/'); // !!!
  369. filename = outputWorkingDir + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + inputFile.getName();
  370. } else {
  371. filename = outputWorkingDir + Config.DIR_SEP_CHAR + inputFile.getName();
  372. }
  373. File signatureFile = new File(filename);
  374. if (deleteTempFilesOnExit)
  375. signatureFile.deleteOnExit();
  376. return signatureFile;
  377. }
  378. // static void verifyPackageDirExists( String packageName, String offset ) {
  379. // System.err.println(">>> name: " + packageName + ", offset: " + offset);
  380. // if ( packageName.indexOf( "." ) != -1 ) {
  381. // File tempFile = new File("c:/aspectj-test/d1/d2/d3");
  382. // tempFile.mkdirs();
  383. // String currPkgDir = packageName.substring( 0, packageName.indexOf( "." ) );
  384. // String remainingPkg = packageName.substring( packageName.indexOf( "." )+1 );
  385. // String filePath = null;
  386. // if ( offset != null ) {
  387. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR +
  388. // offset + Config.DIR_SEP_CHAR + currPkgDir ;
  389. // }
  390. // else {
  391. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + currPkgDir;
  392. // }
  393. // File packageDir = new File( filePath );
  394. // if ( !packageDir.exists() ) {
  395. // packageDir.mkdir();
  396. // if (deleteTempFilesOnExit) packageDir.deleteOnExit();
  397. // }
  398. // if ( remainingPkg != "" ) {
  399. // verifyPackageDirExists( remainingPkg, currPkgDir );
  400. // }
  401. // }
  402. // else {
  403. // String filePath = null;
  404. // if ( offset != null ) {
  405. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + offset + Config.DIR_SEP_CHAR + packageName;
  406. // }
  407. // else {
  408. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + packageName;
  409. // }
  410. // File packageDir = new File( filePath );
  411. // if ( !packageDir.exists() ) {
  412. // packageDir.mkdir();
  413. // if (deleteTempFilesOnExit) packageDir.deleteOnExit();
  414. // }
  415. // }
  416. // }
  417. /**
  418. * Can read Eclipse-generated single-line arg
  419. */
  420. static void parseCommandLine(String[] args) {
  421. if (args.length == 0) {
  422. displayHelpAndExit(null);
  423. } else if (args.length == 1 && args[0].startsWith("@")) {
  424. String argFile = args[0].substring(1);
  425. System.out.println("> Using arg file: " + argFile);
  426. BufferedReader br;
  427. try {
  428. br = new BufferedReader(new FileReader(argFile));
  429. String line = "";
  430. line = br.readLine();
  431. StringTokenizer st = new StringTokenizer(line, " ");
  432. List<String> argList = new ArrayList<String>();
  433. while (st.hasMoreElements()) {
  434. argList.add(st.nextToken());
  435. }
  436. // System.err.println(argList);
  437. args = new String[argList.size()];
  438. int counter = 0;
  439. for (String s : argList) {
  440. args[counter] = s;
  441. counter++;
  442. }
  443. } catch (FileNotFoundException e) {
  444. System.err.println("> could not read arg file: " + argFile);
  445. e.printStackTrace();
  446. } catch (IOException ioe) {
  447. System.err.println("> could not read arg file: " + argFile);
  448. ioe.printStackTrace();
  449. }
  450. }
  451. List<String> vargs = new LinkedList<String>(Arrays.asList(args));
  452. vargs.add("-Xset:minimalModel=false");
  453. parseArgs(vargs, new File(".")); // !!!
  454. if (filenames.size() == 0) {
  455. displayHelpAndExit("ajdoc: No packages or classes specified");
  456. }
  457. }
  458. static void setSourcepath(String arg) {
  459. sourcepath.clear();
  460. arg = arg + File.pathSeparator; // makes things easier for ourselves
  461. StringTokenizer tokenizer = new StringTokenizer(arg, File.pathSeparator);
  462. while (tokenizer.hasMoreElements()) {
  463. sourcepath.addElement(tokenizer.nextToken());
  464. }
  465. }
  466. static String getSourcepathAsString() {
  467. String cPath = "";
  468. for (int i = 0; i < sourcepath.size(); i++) {
  469. cPath += sourcepath.elementAt(i) + Config.DIR_SEP_CHAR + outputWorkingDir;
  470. if (i != sourcepath.size() - 1) {
  471. cPath += File.pathSeparator;
  472. }
  473. }
  474. return cPath;
  475. }
  476. static void parseArgs(List vargs, File currentWorkingDir) {
  477. boolean addNextAsOption = false;
  478. boolean addNextAsArgFile = false;
  479. boolean addNextToAJCOptions = false;
  480. boolean addNextAsDocDir = false;
  481. boolean addNextAsClasspath = false;
  482. boolean ignoreArg = false; // used for discrepancy betwen class/sourcepath in ajc/javadoc
  483. boolean addNextAsSourcePath = false;
  484. if (vargs.size() == 0) {
  485. displayHelpAndExit(null);
  486. }
  487. for (Object varg : vargs) {
  488. String arg = (String) varg;
  489. ignoreArg = false;
  490. if (addNextAsDocDir) {
  491. docDir = arg;
  492. addNextAsDocDir = false;
  493. }
  494. if (addNextAsClasspath) {
  495. addNextAsClasspath = false;
  496. }
  497. if (addNextAsSourcePath) {
  498. setSourcepath(arg);
  499. addNextAsSourcePath = false;
  500. ignoreArg = true;
  501. }
  502. if (arg.startsWith("@")) {
  503. expandAtSignFile(arg.substring(1), currentWorkingDir);
  504. } else if (arg.equals("-argfile")) {
  505. addNextAsArgFile = true;
  506. } else if (addNextAsArgFile) {
  507. expandAtSignFile(arg, currentWorkingDir);
  508. addNextAsArgFile = false;
  509. } else if (arg.equals("-d")) {
  510. addNextAsOption = true;
  511. options.addElement(arg);
  512. addNextAsDocDir = true;
  513. } else if (arg.equals("-bootclasspath")) {
  514. addNextAsOption = true;
  515. addNextToAJCOptions = true;
  516. options.addElement(arg);
  517. ajcOptions.addElement(arg);
  518. } else if (arg.equals("-source")) {
  519. addNextAsOption = true;
  520. addNextToAJCOptions = true;
  521. addNextAsClasspath = true;
  522. options.addElement(arg);
  523. ajcOptions.addElement(arg);
  524. } else if (arg.equals("-classpath")) {
  525. addNextAsOption = true;
  526. addNextToAJCOptions = true;
  527. addNextAsClasspath = true;
  528. options.addElement(arg);
  529. ajcOptions.addElement(arg);
  530. } else if (arg.equals("-encoding")) {
  531. addNextAsOption = true;
  532. addNextToAJCOptions = false;
  533. options.addElement(arg);
  534. } else if (arg.equals("-docencoding")) {
  535. addNextAsOption = true;
  536. addNextToAJCOptions = false;
  537. options.addElement(arg);
  538. } else if (arg.equals("-charset")) {
  539. addNextAsOption = true;
  540. addNextToAJCOptions = false;
  541. options.addElement(arg);
  542. } else if (arg.equals("-sourcepath")) {
  543. addNextAsSourcePath = true;
  544. // options.addElement( arg );
  545. // ajcOptions.addElement( arg );
  546. } else if (arg.equals("-link")) {
  547. addNextAsOption = true;
  548. options.addElement(arg);
  549. } else if (arg.equals("-bottom")) {
  550. addNextAsOption = true;
  551. options.addElement(arg);
  552. } else if (arg.equals("-windowtitle")) {
  553. addNextAsOption = true;
  554. options.addElement(arg);
  555. } else if (arg.equals("-XajdocDebug")) {
  556. deleteTempFilesOnExit = false;
  557. } else if (arg.equals("-use")) {
  558. System.out.println("> Ignoring unsupported option: -use");
  559. } else if (arg.equals("-splitindex")) {
  560. // passed to javadoc
  561. } else if (arg.startsWith("-") || addNextAsOption || addNextToAJCOptions) {
  562. if (arg.equals("-private")) {
  563. docModifier = "private";
  564. } else if (arg.equals("-package")) {
  565. docModifier = "package";
  566. } else if (arg.equals("-protected")) {
  567. docModifier = "protected";
  568. } else if (arg.equals("-public")) {
  569. docModifier = "public";
  570. } else if (arg.equals("-verbose")) {
  571. verboseMode = true;
  572. } else if (arg.equals("-author")) {
  573. authorStandardDocletSwitch = true;
  574. } else if (arg.equals("-version")) {
  575. versionStandardDocletSwitch = true;
  576. } else if (arg.equals("-v")) {
  577. System.out.println(getVersion());
  578. exit(0);
  579. } else if (arg.equals("-help")) {
  580. displayHelpAndExit(null);
  581. } else if (arg.equals("-doclet") || arg.equals("-docletpath")) {
  582. System.out.println("The doclet and docletpath options are not currently supported \n"
  583. + "since ajdoc makes assumptions about the behavior of the standard \n"
  584. + "doclet. If you would find this option useful please email us at: \n"
  585. + " \n"
  586. + " aspectj-dev@eclipse.org \n"
  587. + " \n");
  588. exit(0);
  589. } else if (arg.equals("-nonavbar") || arg.equals("-noindex")) {
  590. // pass through
  591. // System.err.println("> ignoring unsupported option: " + arg);
  592. } else if (addNextToAJCOptions || addNextAsOption) {
  593. // deal with these two options together even though effectively
  594. // just falling through if addNextAsOption is true. Otherwise
  595. // will have to ensure check "addNextToAJCOptions" before
  596. // "addNextAsOption" so as the options are added to the
  597. // correct lists.
  598. if (addNextToAJCOptions) {
  599. ajcOptions.addElement(arg);
  600. if (!arg.startsWith("-")) {
  601. addNextToAJCOptions = false;
  602. }
  603. if (!addNextAsOption) {
  604. continue;
  605. }
  606. }
  607. } else if (arg.startsWith("-")) {
  608. ajcOptions.addElement(arg);
  609. addNextToAJCOptions = true;
  610. continue;
  611. } else {
  612. System.err.println("> unrecognized argument: " + arg);
  613. displayHelpAndExit(null);
  614. }
  615. options.addElement(arg);
  616. addNextAsOption = false;
  617. } else {
  618. // check if this is a file or a package
  619. // System.err.println(">>>>>>>> " + );
  620. // String entryName = arg.substring(arg.lastIndexOf(File.separator)+1);
  621. if (FileUtil.hasSourceSuffix(arg) || arg.endsWith(".lst") && arg != null) {
  622. File f = new File(arg);
  623. if (f.isAbsolute()) {
  624. filenames.addElement(arg);
  625. } else {
  626. filenames.addElement(currentWorkingDir + Config.DIR_SEP_CHAR + arg);
  627. }
  628. fileList.addElement(arg);
  629. }
  630. // PACKAGE MODE STUFF
  631. else if (!ignoreArg) {
  632. packageMode = true;
  633. packageList.addElement(arg);
  634. arg = arg.replace('.', '/'); // !!!
  635. // do this for every item in the classpath
  636. for (int c = 0; c < sourcepath.size(); c++) {
  637. String path = sourcepath.elementAt(c) + Config.DIR_SEP_CHAR + arg;
  638. File pkg = new File(path);
  639. if (pkg.isDirectory()) {
  640. String[] files = pkg.list(new FilenameFilter() {
  641. @Override
  642. public boolean accept(File dir, String name) {
  643. int index1 = name.lastIndexOf(".");
  644. int index2 = name.length();
  645. if ((index1 >= 0 && index2 >= 0)
  646. && (name.substring(index1, index2).equals(".java") || name.substring(index1, index2)
  647. .equals(".aj"))) {
  648. return true;
  649. } else {
  650. return false;
  651. }
  652. }
  653. });
  654. for (String file : files) {
  655. filenames.addElement(sourcepath.elementAt(c) + Config.DIR_SEP_CHAR + arg
  656. + Config.DIR_SEP_CHAR + file);
  657. }
  658. } else if (c == sourcepath.size()) { // last element on classpath
  659. System.out.println("ajdoc: No package, class, or source file " + "found named " + arg + ".");
  660. } else {
  661. // didn't find it on that element of the classpath but that's ok
  662. }
  663. }
  664. }
  665. }
  666. }
  667. // set the default visibility as an option to javadoc option
  668. if (!options.contains("-private") && !options.contains("-package") && !options.contains("-protected")
  669. && !options.contains("-public")) {
  670. options.addElement("-package");
  671. }
  672. }
  673. static void expandAtSignFile(String filename, File currentWorkingDir) {
  674. List<String> result = new LinkedList<String>();
  675. File atFile = qualifiedFile(filename, currentWorkingDir);
  676. String atFileParent = atFile.getParent();
  677. File myWorkingDir = null;
  678. if (atFileParent != null)
  679. myWorkingDir = new File(atFileParent);
  680. try {
  681. BufferedReader stream = new BufferedReader(new FileReader(atFile));
  682. String line = null;
  683. while ((line = stream.readLine()) != null) {
  684. // strip out any comments of the form # to end of line
  685. int commentStart = line.indexOf("//");
  686. if (commentStart != -1) {
  687. line = line.substring(0, commentStart);
  688. }
  689. // remove extra whitespace that might have crept in
  690. line = line.trim();
  691. // ignore blank lines
  692. if (line.length() == 0)
  693. continue;
  694. result.add(line);
  695. }
  696. stream.close();
  697. } catch (IOException e) {
  698. System.err.println("Error while reading the @ file " + atFile.getPath() + ".\n" + e);
  699. System.exit(-1);
  700. }
  701. parseArgs(result, myWorkingDir);
  702. }
  703. static File qualifiedFile(String name, File currentWorkingDir) {
  704. name = name.replace('/', File.separatorChar);
  705. File file = new File(name);
  706. if (!file.isAbsolute() && currentWorkingDir != null) {
  707. file = new File(currentWorkingDir, name);
  708. }
  709. return file;
  710. }
  711. static void displayHelpAndExit(String message) {
  712. shownAjdocUsageMessage = true;
  713. if (message != null) {
  714. System.err.println(message);
  715. System.err.println();
  716. System.err.println(Config.USAGE);
  717. } else {
  718. System.out.println(Config.USAGE);
  719. exit(0);
  720. }
  721. }
  722. static protected void exit(int value) {
  723. System.out.flush();
  724. System.err.flush();
  725. System.exit(value);
  726. }
  727. /* This section of code handles errors that occur during compilation */
  728. static final String internalErrorMessage = " \n"
  729. + "If this has not already been logged as a bug raised please raise \n"
  730. + "a new AspectJ bug at https://bugs.eclipse.org/bugs including the \n"
  731. + "text below. To make the bug a priority, please also include a test\n"
  732. + "program that can reproduce this problem.\n ";
  733. static public void handleInternalError(Throwable uncaughtThrowable) {
  734. System.err.println("An internal error occured in ajdoc");
  735. System.err.println(internalErrorMessage);
  736. System.err.println(uncaughtThrowable.toString());
  737. uncaughtThrowable.printStackTrace();
  738. System.err.println();
  739. }
  740. static String getVersion() {
  741. return "ajdoc version " + Version.getText();
  742. }
  743. public static boolean hasAborted() {
  744. return aborted;
  745. }
  746. public static IMessage[] getErrors() {
  747. return errors;
  748. }
  749. public static boolean hasShownAjdocUsageMessage() {
  750. return shownAjdocUsageMessage;
  751. }
  752. /**
  753. * Sets the output working dir to be <fullyQualifiedOutputDir>\ajdocworkingdir Useful in testing to redirect the ajdocworkingdir
  754. * to the sandbox
  755. */
  756. public static void setOutputWorkingDir(String fullyQulifiedOutputDir) {
  757. if (fullyQulifiedOutputDir == null) {
  758. resetOutputWorkingDir();
  759. } else {
  760. outputWorkingDir = fullyQulifiedOutputDir + File.separatorChar + Config.WORKING_DIR;
  761. }
  762. }
  763. /**
  764. * Resets the output working dir to be the default which is <the current directory>\ajdocworkingdir
  765. */
  766. public static void resetOutputWorkingDir() {
  767. outputWorkingDir = Config.WORKING_DIR;
  768. }
  769. }