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

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