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

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