1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.FileWriter;
  20. import java.io.FilenameFilter;
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import java.io.BufferedReader;
  24. import java.io.FileReader;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.util.Enumeration;
  28. import java.util.Locale;
  29. import java.util.StringTokenizer;
  30. import java.util.Vector;
  31. import org.apache.tools.ant.BuildException;
  32. import org.apache.tools.ant.DirectoryScanner;
  33. import org.apache.tools.ant.Project;
  34. import org.apache.tools.ant.ProjectComponent;
  35. import org.apache.tools.ant.Task;
  36. import org.apache.tools.ant.types.Commandline;
  37. import org.apache.tools.ant.types.DirSet;
  38. import org.apache.tools.ant.types.EnumeratedAttribute;
  39. import org.apache.tools.ant.types.FileSet;
  40. import org.apache.tools.ant.types.Path;
  41. import org.apache.tools.ant.types.PatternSet;
  42. import org.apache.tools.ant.types.Reference;
  43. import org.apache.tools.ant.util.FileUtils;
  44. import org.apache.tools.ant.util.JavaEnvUtils;
  45. /**
  46. * Generates Javadoc documentation for a collection
  47. * of source code.
  48. *
  49. * <P>Current known limitations are:
  50. *
  51. * <P><UL>
  52. * <LI>patterns must be of the form "xxx.*", every other pattern doesn't
  53. * work.
  54. * <LI>there is no control on arguments sanity since they are left
  55. * to the javadoc implementation.
  56. * <LI>argument J in javadoc1 is not supported (what is that for anyway?)
  57. * </UL>
  58. *
  59. * <P>If no <CODE>doclet</CODE> is set, then the <CODE>version</CODE> and
  60. * <CODE>author</CODE> are by default <CODE>"yes"</CODE>.
  61. *
  62. * <P>Note: This task is run on another VM because the Javadoc code calls
  63. * <CODE>System.exit()</CODE> which would break Ant functionality.
  64. *
  65. *
  66. * @since Ant 1.1
  67. *
  68. * @ant.task category="java"
  69. */
  70. public class Javadoc extends Task {
  71. /**
  72. * Inner class used to manage doclet parameters.
  73. */
  74. public class DocletParam {
  75. /** The parameter name */
  76. private String name;
  77. /** The parameter value */
  78. private String value;
  79. /**
  80. * Set the name of the parameter.
  81. *
  82. * @param name the name of the doclet parameter
  83. */
  84. public void setName(String name) {
  85. this.name = name;
  86. }
  87. /**
  88. * Get the parameter name.
  89. *
  90. * @return the parameter's name.
  91. */
  92. public String getName() {
  93. return name;
  94. }
  95. /**
  96. * Set the parameter value.
  97. *
  98. * Note that only string values are supported. No resolution of file
  99. * paths is performed.
  100. *
  101. * @param value the parameter value.
  102. */
  103. public void setValue(String value) {
  104. this.value = value;
  105. }
  106. /**
  107. * Get the parameter value.
  108. *
  109. * @return the parameter value.
  110. */
  111. public String getValue() {
  112. return value;
  113. }
  114. }
  115. /**
  116. * A project aware class used for Javadoc extensions which take a name
  117. * and a path such as doclet and taglet arguments.
  118. *
  119. */
  120. public static class ExtensionInfo extends ProjectComponent {
  121. /** The name of the extension */
  122. private String name;
  123. /** The optional path to use to load the extension */
  124. private Path path;
  125. /**
  126. * Set the name of the extension
  127. *
  128. * @param name the extension's name.
  129. */
  130. public void setName(String name) {
  131. this.name = name;
  132. }
  133. /**
  134. * Get the name of the extension.
  135. *
  136. * @return the extension's name.
  137. */
  138. public String getName() {
  139. return name;
  140. }
  141. /**
  142. * Set the path to use when loading the component.
  143. *
  144. * @param path a Path instance containing the classpath to use.
  145. */
  146. public void setPath(Path path) {
  147. if (this.path == null) {
  148. this.path = path;
  149. } else {
  150. this.path.append(path);
  151. }
  152. }
  153. /**
  154. * Get the extension's path.
  155. *
  156. * @return the path to be used to load the extension.
  157. * May be <code>null</code>
  158. */
  159. public Path getPath() {
  160. return path;
  161. }
  162. /**
  163. * Create an empty nested path to be configured by Ant with the
  164. * classpath for the extension.
  165. *
  166. * @return a new Path instance to be configured.
  167. */
  168. public Path createPath() {
  169. if (path == null) {
  170. path = new Path(getProject());
  171. }
  172. return path.createPath();
  173. }
  174. /**
  175. * Adds a reference to a CLASSPATH defined elsewhere.
  176. *
  177. * @param r the reference containing the path.
  178. */
  179. public void setPathRef(Reference r) {
  180. createPath().setRefid(r);
  181. }
  182. }
  183. /**
  184. * This class stores info about doclets.
  185. *
  186. */
  187. public class DocletInfo extends ExtensionInfo {
  188. /** Collection of doclet parameters. */
  189. private Vector params = new Vector();
  190. /**
  191. * Create a doclet parameter to be configured by Ant.
  192. *
  193. * @return a new DocletParam instance to be configured.
  194. */
  195. public DocletParam createParam() {
  196. DocletParam param = new DocletParam();
  197. params.addElement(param);
  198. return param;
  199. }
  200. /**
  201. * Get the doclet's parameters.
  202. *
  203. * @return an Enumeration of DocletParam instances.
  204. */
  205. public Enumeration getParams() {
  206. return params.elements();
  207. }
  208. }
  209. /**
  210. * Used to track info about the packages to be javadoc'd
  211. */
  212. public static class PackageName {
  213. /** The package name */
  214. private String name;
  215. /**
  216. * Set the name of the package
  217. *
  218. * @param name the package name.
  219. */
  220. public void setName(String name) {
  221. this.name = name.trim();
  222. }
  223. /**
  224. * Get the package name.
  225. *
  226. * @return the package's name.
  227. */
  228. public String getName() {
  229. return name;
  230. }
  231. /**
  232. * @see java.lang.Object#toString
  233. */
  234. public String toString() {
  235. return getName();
  236. }
  237. }
  238. /**
  239. * This class is used to manage the source files to be processed.
  240. */
  241. public static class SourceFile {
  242. /** The source file */
  243. private File file;
  244. /**
  245. * Default constructor
  246. */
  247. public SourceFile() {
  248. }
  249. /**
  250. * Constructor specifying the source file directly
  251. *
  252. * @param file the source file
  253. */
  254. public SourceFile(File file) {
  255. this.file = file;
  256. }
  257. /**
  258. * Set the source file.
  259. *
  260. * @param file the source file.
  261. */
  262. public void setFile(File file) {
  263. this.file = file;
  264. }
  265. /**
  266. * Get the source file.
  267. *
  268. * @return the source file.
  269. */
  270. public File getFile() {
  271. return file;
  272. }
  273. }
  274. /**
  275. * An HTML element in the javadoc.
  276. *
  277. * This class is used for those javadoc elements which contain HTML such as
  278. * footers, headers, etc.
  279. */
  280. public static class Html {
  281. /** The text for the element */
  282. private StringBuffer text = new StringBuffer();
  283. /**
  284. * Add text to the element.
  285. *
  286. * @param t the text to be added.
  287. */
  288. public void addText(String t) {
  289. text.append(t);
  290. }
  291. /**
  292. * Get the current text for the element.
  293. *
  294. * @return the current text.
  295. */
  296. public String getText() {
  297. return text.substring(0);
  298. }
  299. }
  300. /**
  301. * EnumeratedAttribute implementation supporting the javadoc scoping
  302. * values.
  303. */
  304. public static class AccessType extends EnumeratedAttribute {
  305. /**
  306. * @see EnumeratedAttribute#getValues().
  307. */
  308. public String[] getValues() {
  309. // Protected first so if any GUI tool offers a default
  310. // based on enum #0, it will be right.
  311. return new String[] {"protected", "public", "package", "private"};
  312. }
  313. }
  314. /** The command line built to execute Javadoc. */
  315. private Commandline cmd = new Commandline();
  316. /** Flag which indicates if javadoc from JDK 1.1 is to be used. */
  317. private static boolean javadoc1 =
  318. JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1);
  319. /** Flag which indicates if javadoc from JDK 1.4 is available */
  320. private static boolean javadoc4 =
  321. !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)
  322. && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
  323. && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3);
  324. /**
  325. * Utility method to add an argument to the command line conditionally
  326. * based on the given flag.
  327. *
  328. * @param b the flag which controls if the argument is added.
  329. * @param arg the argument value.
  330. */
  331. private void addArgIf(boolean b, String arg) {
  332. if (b) {
  333. cmd.createArgument().setValue(arg);
  334. }
  335. }
  336. /**
  337. * Utility method to add a non JDK1.1 javadoc argument.
  338. *
  339. * @param key the argument name.
  340. * @param value the argument value.
  341. */
  342. private void add12ArgIfNotEmpty(String key, String value) {
  343. if (!javadoc1) {
  344. if (value != null && value.length() != 0) {
  345. cmd.createArgument().setValue(key);
  346. cmd.createArgument().setValue(value);
  347. } else {
  348. log("Warning: Leaving out empty argument '" + key + "'",
  349. Project.MSG_WARN);
  350. }
  351. }
  352. }
  353. /**
  354. * Utility method to add a non-JDK1.1 argument to the command line
  355. * conditionally based on the given flag.
  356. *
  357. * @param b the flag which controls if the argument is added.
  358. * @param arg the argument value.
  359. */
  360. private void add12ArgIf(boolean b, String arg) {
  361. if (!javadoc1 && b) {
  362. cmd.createArgument().setValue(arg);
  363. }
  364. }
  365. /**
  366. * Flag which indicates if the task should fail if there is a
  367. * javadoc error.
  368. */
  369. private boolean failOnError = false;
  370. private Path sourcePath = null;
  371. private File destDir = null;
  372. private Vector sourceFiles = new Vector();
  373. private Vector packageNames = new Vector();
  374. private Vector excludePackageNames = new Vector(1);
  375. private boolean author = true;
  376. private boolean version = true;
  377. private DocletInfo doclet = null;
  378. private Path classpath = null;
  379. private Path bootclasspath = null;
  380. private String group = null;
  381. private String packageList = null;
  382. private Vector links = new Vector();
  383. private Vector groups = new Vector();
  384. private Vector tags = new Vector();
  385. private boolean useDefaultExcludes = true;
  386. private Html doctitle = null;
  387. private Html header = null;
  388. private Html footer = null;
  389. private Html bottom = null;
  390. private boolean useExternalFile = false;
  391. private FileUtils fileUtils = FileUtils.newFileUtils();
  392. private String source = null;
  393. private boolean linksource = false;
  394. private boolean breakiterator = false;
  395. private String noqualifier;
  396. private Vector fileSets = new Vector();
  397. private Vector packageSets = new Vector();
  398. /**
  399. * Work around command line length limit by using an external file
  400. * for the sourcefiles.
  401. *
  402. * @param b true if an external file is to be used.
  403. */
  404. public void setUseExternalFile(boolean b) {
  405. if (!javadoc1) {
  406. useExternalFile = b;
  407. }
  408. }
  409. /**
  410. * Sets whether default exclusions should be used or not.
  411. *
  412. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  413. * should be used, "false"|"off"|"no" when they
  414. * shouldn't be used.
  415. */
  416. public void setDefaultexcludes(boolean useDefaultExcludes) {
  417. this.useDefaultExcludes = useDefaultExcludes;
  418. }
  419. /**
  420. * Set the maximum memory to be used by the javadoc process
  421. *
  422. * @param max a string indicating the maximum memory according to the
  423. * JVM conventions (e.g. 128m is 128 Megabytes)
  424. */
  425. public void setMaxmemory(String max) {
  426. if (javadoc1) {
  427. cmd.createArgument().setValue("-J-mx" + max);
  428. } else {
  429. cmd.createArgument().setValue("-J-Xmx" + max);
  430. }
  431. }
  432. /**
  433. * Set an additional parameter on the command line
  434. *
  435. * @param add the additional command line parameter for the javadoc task.
  436. */
  437. public void setAdditionalparam(String add) {
  438. cmd.createArgument().setLine(add);
  439. }
  440. /**
  441. * Adds a command-line argument.
  442. * @since Ant 1.6
  443. */
  444. public Commandline.Argument createArg() {
  445. return cmd.createArgument();
  446. }
  447. /**
  448. * Specify where to find source file
  449. *
  450. * @param src a Path instance containing the various source directories.
  451. */
  452. public void setSourcepath(Path src) {
  453. if (sourcePath == null) {
  454. sourcePath = src;
  455. } else {
  456. sourcePath.append(src);
  457. }
  458. }
  459. /**
  460. * Create a path to be configured with the locations of the source
  461. * files.
  462. *
  463. * @return a new Path instance to be configured by the Ant core.
  464. */
  465. public Path createSourcepath() {
  466. if (sourcePath == null) {
  467. sourcePath = new Path(getProject());
  468. }
  469. return sourcePath.createPath();
  470. }
  471. /**
  472. * Adds a reference to a CLASSPATH defined elsewhere.
  473. *
  474. * @param r the reference containing the source path definition.
  475. */
  476. public void setSourcepathRef(Reference r) {
  477. createSourcepath().setRefid(r);
  478. }
  479. /**
  480. * Set the directory where the Javadoc output will be generated.
  481. *
  482. * @param dir the destination directory.
  483. */
  484. public void setDestdir(File dir) {
  485. destDir = dir;
  486. cmd.createArgument().setValue("-d");
  487. cmd.createArgument().setFile(destDir);
  488. }
  489. /**
  490. * Set the list of source files to process.
  491. *
  492. * @param src a comma separated list of source files.
  493. */
  494. public void setSourcefiles(String src) {
  495. StringTokenizer tok = new StringTokenizer(src, ",");
  496. while (tok.hasMoreTokens()) {
  497. String f = tok.nextToken();
  498. SourceFile sf = new SourceFile();
  499. sf.setFile(getProject().resolveFile(f.trim()));
  500. addSource(sf);
  501. }
  502. }
  503. /**
  504. * Add a single source file.
  505. *
  506. * @param sf the source file to be processed.
  507. */
  508. public void addSource(SourceFile sf) {
  509. sourceFiles.addElement(sf);
  510. }
  511. /**
  512. * Set the package names to be processed.
  513. *
  514. * @param packages a comma separated list of packages specs
  515. * (may be wildcarded).
  516. *
  517. * @see #addPackage for wildcard information.
  518. */
  519. public void setPackagenames(String packages) {
  520. StringTokenizer tok = new StringTokenizer(packages, ",");
  521. while (tok.hasMoreTokens()) {
  522. String p = tok.nextToken();
  523. PackageName pn = new PackageName();
  524. pn.setName(p);
  525. addPackage(pn);
  526. }
  527. }
  528. /**
  529. * Add a single package to be processed.
  530. *
  531. * If the package name ends with ".*" the Javadoc task
  532. * will find and process all subpackages.
  533. *
  534. * @param pn the package name, possibly wildcarded.
  535. */
  536. public void addPackage(PackageName pn) {
  537. packageNames.addElement(pn);
  538. }
  539. /**
  540. * Set the list of packages to be excluded.
  541. *
  542. * @param packages a comma separated list of packages to be excluded.
  543. * This may not include wildcards.
  544. */
  545. public void setExcludePackageNames(String packages) {
  546. StringTokenizer tok = new StringTokenizer(packages, ",");
  547. while (tok.hasMoreTokens()) {
  548. String p = tok.nextToken();
  549. PackageName pn = new PackageName();
  550. pn.setName(p);
  551. addExcludePackage(pn);
  552. }
  553. }
  554. /**
  555. * Add a package to be excluded from the javadoc run.
  556. *
  557. * @param pn the name of the package (wildcards are not permitted).
  558. */
  559. public void addExcludePackage(PackageName pn) {
  560. excludePackageNames.addElement(pn);
  561. }
  562. /**
  563. * Specify the file containing the overview to be included in the generated
  564. * documentation.
  565. *
  566. * @param f the file containing the overview.
  567. */
  568. public void setOverview(File f) {
  569. if (!javadoc1) {
  570. cmd.createArgument().setValue("-overview");
  571. cmd.createArgument().setFile(f);
  572. }
  573. }
  574. /**
  575. * Indicate whether only public classes and members are to be included in
  576. * the scope processed
  577. *
  578. * @param b true if scope is to be public.
  579. */
  580. public void setPublic(boolean b) {
  581. addArgIf(b, "-public");
  582. }
  583. /**
  584. * Indicate whether only protected and public classes and members are to
  585. * be included in the scope processed
  586. *
  587. * @param b true if scope is to be protected.
  588. */
  589. public void setProtected(boolean b) {
  590. addArgIf(b, "-protected");
  591. }
  592. /**
  593. * Indicate whether only package, protected and public classes and
  594. * members are to be included in the scope processed
  595. *
  596. * @param b true if scope is to be package level.
  597. */
  598. public void setPackage(boolean b) {
  599. addArgIf(b, "-package");
  600. }
  601. /**
  602. * Indicate whether all classes and
  603. * members are to be included in the scope processed
  604. *
  605. * @param b true if scope is to be private level.
  606. */
  607. public void setPrivate(boolean b) {
  608. addArgIf(b, "-private");
  609. }
  610. /**
  611. * Set the scope to be processed. This is an alternative to the
  612. * use of the setPublic, setPrivate, etc methods. It gives better build
  613. * file control over what scope is processed.
  614. *
  615. * @param at the scope to be processed.
  616. */
  617. public void setAccess(AccessType at) {
  618. cmd.createArgument().setValue("-" + at.getValue());
  619. }
  620. /**
  621. * Set the class that starts the doclet used in generating the
  622. * documentation.
  623. *
  624. * @param docletName the name of the doclet class.
  625. */
  626. public void setDoclet(String docletName) {
  627. if (doclet == null) {
  628. doclet = new DocletInfo();
  629. doclet.setProject(getProject());
  630. }
  631. doclet.setName(docletName);
  632. }
  633. /**
  634. * Set the classpath used to find the doclet class.
  635. *
  636. * @param docletPath the doclet classpath.
  637. */
  638. public void setDocletPath(Path docletPath) {
  639. if (doclet == null) {
  640. doclet = new DocletInfo();
  641. doclet.setProject(getProject());
  642. }
  643. doclet.setPath(docletPath);
  644. }
  645. /**
  646. * Set the classpath used to find the doclet class by reference.
  647. *
  648. * @param r the reference to the Path instance to use as the doclet
  649. * classpath.
  650. */
  651. public void setDocletPathRef(Reference r) {
  652. if (doclet == null) {
  653. doclet = new DocletInfo();
  654. doclet.setProject(getProject());
  655. }
  656. doclet.createPath().setRefid(r);
  657. }
  658. /**
  659. * Create a doclet to be used in the documentation generation.
  660. *
  661. * @return a new DocletInfo instance to be configured.
  662. */
  663. public DocletInfo createDoclet() {
  664. doclet = new DocletInfo();
  665. return doclet;
  666. }
  667. /**
  668. * Add a taglet
  669. *
  670. * @param tagletInfo information about the taglet.
  671. */
  672. public void addTaglet(ExtensionInfo tagletInfo) {
  673. tags.addElement(tagletInfo);
  674. }
  675. /**
  676. * Indicate whether Javadoc should produce old style (JDK 1.1)
  677. * documentation.
  678. *
  679. * This is not supported by JDK 1.1 and has been phased out in JDK 1.4
  680. *
  681. * @param b if true attempt to generate old style documentation.
  682. */
  683. public void setOld(boolean b) {
  684. if (b) {
  685. if (javadoc1) {
  686. log("Javadoc 1.1 doesn't support the -1.1 switch",
  687. Project.MSG_WARN);
  688. } else if (javadoc4) {
  689. log("Javadoc 1.4 doesn't support the -1.1 switch anymore",
  690. Project.MSG_WARN);
  691. } else {
  692. cmd.createArgument().setValue("-1.1");
  693. }
  694. }
  695. }
  696. /**
  697. * Set the classpath to be used for this javadoc run.
  698. *
  699. * @param path an Ant Path object containing the compilation
  700. * classpath.
  701. */
  702. public void setClasspath(Path path) {
  703. if (classpath == null) {
  704. classpath = path;
  705. } else {
  706. classpath.append(path);
  707. }
  708. }
  709. /**
  710. * Create a Path to be configured with the classpath to use
  711. *
  712. * @return a new Path instance to be configured with the classpath.
  713. */
  714. public Path createClasspath() {
  715. if (classpath == null) {
  716. classpath = new Path(getProject());
  717. }
  718. return classpath.createPath();
  719. }
  720. /**
  721. * Adds a reference to a CLASSPATH defined elsewhere.
  722. *
  723. * @param r the reference to an instance defining the classpath.
  724. */
  725. public void setClasspathRef(Reference r) {
  726. createClasspath().setRefid(r);
  727. }
  728. /**
  729. * Set the boot classpath to use.
  730. *
  731. * @param path the boot classpath.
  732. */
  733. public void setBootclasspath(Path path) {
  734. if (bootclasspath == null) {
  735. bootclasspath = path;
  736. } else {
  737. bootclasspath.append(path);
  738. }
  739. }
  740. /**
  741. * Create a Path to be configured with the boot classpath
  742. *
  743. * @return a new Path instance to be configured with the boot classpath.
  744. */
  745. public Path createBootclasspath() {
  746. if (bootclasspath == null) {
  747. bootclasspath = new Path(getProject());
  748. }
  749. return bootclasspath.createPath();
  750. }
  751. /**
  752. * Adds a reference to a CLASSPATH defined elsewhere.
  753. *
  754. * @param r the reference to an instance defining the bootclasspath.
  755. */
  756. public void setBootClasspathRef(Reference r) {
  757. createBootclasspath().setRefid(r);
  758. }
  759. /**
  760. * Set the location of the extensions directories.
  761. *
  762. * @param path the string version of the path.
  763. * @deprecated Use the {@link #setExtdirs(Path)} version.
  764. */
  765. public void setExtdirs(String path) {
  766. if (!javadoc1) {
  767. cmd.createArgument().setValue("-extdirs");
  768. cmd.createArgument().setValue(path);
  769. }
  770. }
  771. /**
  772. * Set the location of the extensions directories.
  773. *
  774. * @param path a path containing the extension directories.
  775. */
  776. public void setExtdirs(Path path) {
  777. if (!javadoc1) {
  778. cmd.createArgument().setValue("-extdirs");
  779. cmd.createArgument().setPath(path);
  780. }
  781. }
  782. /**
  783. * Run javadoc in verbose mode
  784. *
  785. * @param b true if operation is to be verbose.
  786. */
  787. public void setVerbose(boolean b) {
  788. add12ArgIf(b, "-verbose");
  789. }
  790. /**
  791. * Set the local to use in documentation generation.
  792. *
  793. * @param locale the locale to use.
  794. */
  795. public void setLocale(String locale) {
  796. if (!javadoc1) {
  797. // createArgument(true) is necessary to make sure, -locale
  798. // is the first argument (required in 1.3+).
  799. cmd.createArgument(true).setValue(locale);
  800. cmd.createArgument(true).setValue("-locale");
  801. }
  802. }
  803. /**
  804. * Set the encoding name of the source files,
  805. *
  806. * @param enc the name of the encoding for the source files.
  807. */
  808. public void setEncoding(String enc) {
  809. cmd.createArgument().setValue("-encoding");
  810. cmd.createArgument().setValue(enc);
  811. }
  812. /**
  813. * Include the version tag in the generated documentation.
  814. *
  815. * @param b true if the version tag should be included.
  816. */
  817. public void setVersion(boolean b) {
  818. this.version = b;
  819. }
  820. /**
  821. * Generate the "use" page for each package.
  822. *
  823. * @param b true if the use page should be generated.
  824. */
  825. public void setUse(boolean b) {
  826. add12ArgIf(b, "-use");
  827. }
  828. /**
  829. * Include the author tag in the generated documentation.
  830. *
  831. * @param b true if the author tag should be included.
  832. */
  833. public void setAuthor(boolean b) {
  834. author = b;
  835. }
  836. /**
  837. * Generate a split index
  838. *
  839. * @param b true if the index should be split into a file per letter.
  840. */
  841. public void setSplitindex(boolean b) {
  842. add12ArgIf(b, "-splitindex");
  843. }
  844. /**
  845. * Set the title to be placed in the HTML <title> tag of the
  846. * generated documentation.
  847. *
  848. * @param title the window title to use.
  849. */
  850. public void setWindowtitle(String title) {
  851. add12ArgIfNotEmpty("-windowtitle", title);
  852. }
  853. /**
  854. * Set the title of the generated overview page.
  855. *
  856. * @param doctitle the Document title.
  857. */
  858. public void setDoctitle(String doctitle) {
  859. Html h = new Html();
  860. h.addText(doctitle);
  861. addDoctitle(h);
  862. }
  863. /**
  864. * Add a document title to use for the overview page.
  865. *
  866. * @param text the HTML element containing the document title.
  867. */
  868. public void addDoctitle(Html text) {
  869. if (!javadoc1) {
  870. doctitle = text;
  871. }
  872. }
  873. /**
  874. * Set the header text to be placed at the top of each output file.
  875. *
  876. * @param header the header text
  877. */
  878. public void setHeader(String header) {
  879. Html h = new Html();
  880. h.addText(header);
  881. addHeader(h);
  882. }
  883. /**
  884. * Set the header text to be placed at the top of each output file.
  885. *
  886. * @param text the header text
  887. */
  888. public void addHeader(Html text) {
  889. if (!javadoc1) {
  890. header = text;
  891. }
  892. }
  893. /**
  894. * Set the footer text to be placed at the bottom of each output file.
  895. *
  896. * @param footer the footer text.
  897. */
  898. public void setFooter(String footer) {
  899. Html h = new Html();
  900. h.addText(footer);
  901. addFooter(h);
  902. }
  903. /**
  904. * Set the footer text to be placed at the bottom of each output file.
  905. *
  906. * @param text the footer text.
  907. */
  908. public void addFooter(Html text) {
  909. if (!javadoc1) {
  910. footer = text;
  911. }
  912. }
  913. /**
  914. * Set the text to be placed at the bottom of each output file.
  915. *
  916. * @param bottom the bottom text.
  917. */
  918. public void setBottom(String bottom) {
  919. Html h = new Html();
  920. h.addText(bottom);
  921. addBottom(h);
  922. }
  923. /**
  924. * Set the text to be placed at the bottom of each output file.
  925. *
  926. * @param text the bottom text.
  927. */
  928. public void addBottom(Html text) {
  929. if (!javadoc1) {
  930. bottom = text;
  931. }
  932. }
  933. /**
  934. * Link to docs at "url" using package list at "url2"
  935. * - separate the URLs by using a space character.
  936. *
  937. * @param src the offline link specification (url and package list)
  938. */
  939. public void setLinkoffline(String src) {
  940. if (!javadoc1) {
  941. LinkArgument le = createLink();
  942. le.setOffline(true);
  943. String linkOfflineError = "The linkoffline attribute must include"
  944. + " a URL and a package-list file location separated by a"
  945. + " space";
  946. if (src.trim().length() == 0) {
  947. throw new BuildException(linkOfflineError);
  948. }
  949. StringTokenizer tok = new StringTokenizer(src, " ", false);
  950. le.setHref(tok.nextToken());
  951. if (!tok.hasMoreTokens()) {
  952. throw new BuildException(linkOfflineError);
  953. }
  954. le.setPackagelistLoc(getProject().resolveFile(tok.nextToken()));
  955. }
  956. }
  957. /**
  958. * Group specified packages together in overview page.
  959. *
  960. * @param src the group packages - a command separated list of group specs,
  961. * each one being a group name and package specification separated
  962. * by a space.
  963. */
  964. public void setGroup(String src) {
  965. group = src;
  966. }
  967. /**
  968. * Create links to javadoc output at the given URL.
  969. */
  970. public void setLink(String src) {
  971. if (!javadoc1) {
  972. createLink().setHref(src);
  973. }
  974. }
  975. /**
  976. * Control deprecation infromation
  977. *
  978. * @param b If true, do not include deprecated information.
  979. */
  980. public void setNodeprecated(boolean b) {
  981. addArgIf(b, "-nodeprecated");
  982. }
  983. /**
  984. * Control deprecated list generation
  985. *
  986. * @param b if true, do not generate deprecated list.
  987. */
  988. public void setNodeprecatedlist(boolean b) {
  989. add12ArgIf(b, "-nodeprecatedlist");
  990. }
  991. /**
  992. * Control class tree generation.
  993. *
  994. * @param b if true, do not generate class hierarchy.
  995. */
  996. public void setNotree(boolean b) {
  997. addArgIf(b, "-notree");
  998. }
  999. /**
  1000. * Control generation of index.
  1001. *
  1002. * @param b if true, do not generate index.
  1003. */
  1004. public void setNoindex(boolean b) {
  1005. addArgIf(b, "-noindex");
  1006. }
  1007. /**
  1008. * Control generation of help link.
  1009. *
  1010. * @param b if true, do not generate help link
  1011. */
  1012. public void setNohelp(boolean b) {
  1013. add12ArgIf(b, "-nohelp");
  1014. }
  1015. /**
  1016. * Control generation of the navigation bar.
  1017. *
  1018. * @param b if true, do not generate navigation bar.
  1019. */
  1020. public void setNonavbar(boolean b) {
  1021. add12ArgIf(b, "-nonavbar");
  1022. }
  1023. /**
  1024. * Control warnings about serial tag.
  1025. *
  1026. * @param b if true, generate warning about the serial tag.
  1027. */
  1028. public void setSerialwarn(boolean b) {
  1029. add12ArgIf(b, "-serialwarn");
  1030. }
  1031. /**
  1032. * Specifies the CSS stylesheet file to use.
  1033. *
  1034. * @param f the file with the CSS to use.
  1035. */
  1036. public void setStylesheetfile(File f) {
  1037. if (!javadoc1) {
  1038. cmd.createArgument().setValue("-stylesheetfile");
  1039. cmd.createArgument().setFile(f);
  1040. }
  1041. }
  1042. /**
  1043. * Specifies the HTML help file to use.
  1044. *
  1045. * @param f the file containing help content.
  1046. */
  1047. public void setHelpfile(File f) {
  1048. if (!javadoc1) {
  1049. cmd.createArgument().setValue("-helpfile");
  1050. cmd.createArgument().setFile(f);
  1051. }
  1052. }
  1053. /**
  1054. * Output file encoding name.
  1055. *
  1056. * @param enc name of the encoding to use.
  1057. */
  1058. public void setDocencoding(String enc) {
  1059. cmd.createArgument().setValue("-docencoding");
  1060. cmd.createArgument().setValue(enc);
  1061. }
  1062. /**
  1063. * The name of a file containing the packages to process.
  1064. *
  1065. * @param src the file containing the package list.
  1066. */
  1067. public void setPackageList(String src) {
  1068. if (!javadoc1) {
  1069. packageList = src;
  1070. }
  1071. }
  1072. /**
  1073. * Create link to javadoc output at the given URL.
  1074. *
  1075. * @return link argument to configure
  1076. */
  1077. public LinkArgument createLink() {
  1078. LinkArgument la = new LinkArgument();
  1079. links.addElement(la);
  1080. return la;
  1081. }
  1082. /**
  1083. * Represents a link triplet (href, whether link is offline, location of the
  1084. * package list if off line)
  1085. */
  1086. public class LinkArgument {
  1087. private String href;
  1088. private boolean offline = false;
  1089. private File packagelistLoc;
  1090. public LinkArgument() {
  1091. }
  1092. public void setHref(String hr) {
  1093. href = hr;
  1094. }
  1095. public String getHref() {
  1096. return href;
  1097. }
  1098. public void setPackagelistLoc(File src) {
  1099. packagelistLoc = src;
  1100. }
  1101. public File getPackagelistLoc() {
  1102. return packagelistLoc;
  1103. }
  1104. public void setOffline(boolean offline) {
  1105. this.offline = offline;
  1106. }
  1107. public boolean isLinkOffline() {
  1108. return offline;
  1109. }
  1110. }
  1111. /**
  1112. * Creates and adds a -tag argument. This is used to specify
  1113. * custom tags. This argument is only available for JavaDoc 1.4,
  1114. * and will generate a verbose message (and then be ignored)
  1115. * when run on Java versions below 1.4.
  1116. */
  1117. public TagArgument createTag() {
  1118. if (!javadoc4) {
  1119. log ("-tag option not supported on JavaDoc < 1.4",
  1120. Project.MSG_VERBOSE);
  1121. }
  1122. TagArgument ta = new TagArgument();
  1123. tags.addElement (ta);
  1124. return ta;
  1125. }
  1126. /**
  1127. * Scope element verbose names. (Defined here as fields
  1128. * cannot be static in inner classes.) The first letter
  1129. * from each element is used to build up the scope string.
  1130. */
  1131. static final String[] SCOPE_ELEMENTS = {
  1132. "overview", "packages", "types", "constructors",
  1133. "methods", "fields"
  1134. };
  1135. /**
  1136. * Class representing a -tag argument.
  1137. */
  1138. public class TagArgument extends FileSet {
  1139. /** Name of the tag. */
  1140. private String name = null;
  1141. /** Description of the tag to place in the JavaDocs. */
  1142. private String description = null;
  1143. /** Whether or not the tag is enabled. */
  1144. private boolean enabled = true;
  1145. /**
  1146. * Scope string of the tag. This will form the middle
  1147. * argument of the -tag parameter when the tag is enabled
  1148. * (with an X prepended for and is parsed from human-readable form.
  1149. */
  1150. private String scope = "a";
  1151. /** Sole constructor. */
  1152. public TagArgument () {
  1153. }
  1154. /**
  1155. * Sets the name of the tag.
  1156. *
  1157. * @param name The name of the tag.
  1158. * Must not be <code>null</code> or empty.
  1159. */
  1160. public void setName (String name) {
  1161. this.name = name;
  1162. }
  1163. /**
  1164. * Sets the description of the tag. This is what appears in
  1165. * the JavaDoc.
  1166. *
  1167. * @param description The description of the tag.
  1168. * Must not be <code>null</code> or empty.
  1169. */
  1170. public void setDescription (String description) {
  1171. this.description = description;
  1172. }
  1173. /**
  1174. * Sets the scope of the tag. This is in comma-separated
  1175. * form, with each element being one of "all" (the default),
  1176. * "overview", "packages", "types", "constructors", "methods",
  1177. * "fields". The elements are treated in a case-insensitive
  1178. * manner.
  1179. *
  1180. * @param verboseScope The scope of the tag.
  1181. * Must not be <code>null</code>,
  1182. * should not be empty.
  1183. *
  1184. * @exception BuildException if all is specified along with
  1185. * other elements, if any elements are repeated, if no
  1186. * elements are specified, or if any unrecognised elements are
  1187. * specified.
  1188. */
  1189. public void setScope (String verboseScope) throws BuildException {
  1190. verboseScope = verboseScope.toLowerCase(Locale.US);
  1191. boolean[] elements = new boolean[SCOPE_ELEMENTS.length];
  1192. boolean gotAll = false;
  1193. boolean gotNotAll = false;
  1194. // Go through the tokens one at a time, updating the
  1195. // elements array and issuing warnings where appropriate.
  1196. StringTokenizer tok = new StringTokenizer (verboseScope, ",");
  1197. while (tok.hasMoreTokens()) {
  1198. String next = tok.nextToken().trim();
  1199. if (next.equals("all")) {
  1200. if (gotAll) {
  1201. getProject().log ("Repeated tag scope element: all",
  1202. Project.MSG_VERBOSE);
  1203. }
  1204. gotAll = true;
  1205. } else {
  1206. int i;
  1207. for (i = 0; i < SCOPE_ELEMENTS.length; i++) {
  1208. if (next.equals (SCOPE_ELEMENTS[i])) {
  1209. break;
  1210. }
  1211. }
  1212. if (i == SCOPE_ELEMENTS.length) {
  1213. throw new BuildException ("Unrecognised scope element: "
  1214. + next);
  1215. } else {
  1216. if (elements[i]) {
  1217. getProject().log ("Repeated tag scope element: "
  1218. + next, Project.MSG_VERBOSE);
  1219. }
  1220. elements[i] = true;
  1221. gotNotAll = true;
  1222. }
  1223. }
  1224. }
  1225. if (gotNotAll && gotAll) {
  1226. throw new BuildException ("Mixture of \"all\" and other scope "
  1227. + "elements in tag parameter.");
  1228. }
  1229. if (!gotNotAll && !gotAll) {
  1230. throw new BuildException ("No scope elements specified in tag "
  1231. + "parameter.");
  1232. }
  1233. if (gotAll) {
  1234. this.scope = "a";
  1235. } else {
  1236. StringBuffer buff = new StringBuffer (elements.length);
  1237. for (int i = 0; i < elements.length; i++) {
  1238. if (elements[i]) {
  1239. buff.append (SCOPE_ELEMENTS[i].charAt(0));
  1240. }
  1241. }
  1242. this.scope = buff.toString();
  1243. }
  1244. }
  1245. /**
  1246. * Sets whether or not the tag is enabled.
  1247. *
  1248. * @param enabled Whether or not this tag is enabled.
  1249. */
  1250. public void setEnabled (boolean enabled) {
  1251. this.enabled = enabled;
  1252. }
  1253. /**
  1254. * Returns the -tag parameter this argument represented.
  1255. *
  1256. * @exception BuildException if either the name or description
  1257. * is <code>null</code> or empty.
  1258. */
  1259. public String getParameter () throws BuildException {
  1260. if (name == null || name.equals("")) {
  1261. throw new BuildException ("No name specified for custom tag.");
  1262. }
  1263. if (description != null) {
  1264. return name + ":" + (enabled ? "" : "X")
  1265. + scope + ":" + description;
  1266. } else {
  1267. return name;
  1268. }
  1269. }
  1270. }
  1271. /**
  1272. * Separates packages on the overview page into whatever
  1273. * groups you specify, one group per table.
  1274. */
  1275. public GroupArgument createGroup() {
  1276. GroupArgument ga = new GroupArgument();
  1277. groups.addElement(ga);
  1278. return ga;
  1279. }
  1280. public class GroupArgument {
  1281. private Html title;
  1282. private Vector packages = new Vector();
  1283. public GroupArgument() {
  1284. }
  1285. public void setTitle(String src) {
  1286. Html h = new Html();
  1287. h.addText(src);
  1288. addTitle(h);
  1289. }
  1290. public void addTitle(Html text) {
  1291. title = text;
  1292. }
  1293. public String getTitle() {
  1294. return title != null ? title.getText() : null;
  1295. }
  1296. public void setPackages(String src) {
  1297. StringTokenizer tok = new StringTokenizer(src, ",");
  1298. while (tok.hasMoreTokens()) {
  1299. String p = tok.nextToken();
  1300. PackageName pn = new PackageName();
  1301. pn.setName(p);
  1302. addPackage(pn);
  1303. }
  1304. }
  1305. public void addPackage(PackageName pn) {
  1306. packages.addElement(pn);
  1307. }
  1308. public String getPackages() {
  1309. StringBuffer p = new StringBuffer();
  1310. for (int i = 0; i < packages.size(); i++) {
  1311. if (i > 0) {
  1312. p.append(":");
  1313. }
  1314. p.append(packages.elementAt(i).toString());
  1315. }
  1316. return p.toString();
  1317. }
  1318. }
  1319. /**
  1320. * Charset for cross-platform viewing of generated documentation.
  1321. */
  1322. public void setCharset(String src) {
  1323. this.add12ArgIfNotEmpty("-charset", src);
  1324. }
  1325. /**
  1326. * Should the build process fail if javadoc fails (as indicated by
  1327. * a non zero return code)?
  1328. *
  1329. * <p>Default is false.</p>
  1330. */
  1331. public void setFailonerror(boolean b) {
  1332. failOnError = b;
  1333. }
  1334. /**
  1335. * Enables the -source switch, will be ignored if javadoc is not
  1336. * the 1.4 version.
  1337. *
  1338. * @since Ant 1.5
  1339. */
  1340. public void setSource(String source) {
  1341. if (!javadoc4) {
  1342. log ("-source option not supported on JavaDoc < 1.4",
  1343. Project.MSG_VERBOSE);
  1344. }
  1345. this.source = source;
  1346. }
  1347. /**
  1348. * Adds a packageset.
  1349. *
  1350. * <p>All included directories will be translated into package
  1351. * names be converting the directory separator into dots.</p>
  1352. *
  1353. * @since 1.5
  1354. */
  1355. public void addPackageset(DirSet packageSet) {
  1356. packageSets.addElement(packageSet);
  1357. }
  1358. /**
  1359. * Adds a fileset.
  1360. *
  1361. * <p>All included files will be added as sourcefiles. The task
  1362. * will automatically add
  1363. * <code>includes="**/*.java"</code> to the
  1364. * fileset.</p>
  1365. *
  1366. * @since 1.5
  1367. */
  1368. public void addFileset(FileSet fs) {
  1369. fileSets.addElement(fs);
  1370. }
  1371. /**
  1372. * Enables the -linksource switch, will be ignored if javadoc is not
  1373. * the 1.4 version. Default is false
  1374. *
  1375. * @since Ant 1.6
  1376. */
  1377. public void setLinksource(boolean b) {
  1378. if (!javadoc4) {
  1379. log ("-linksource option not supported on JavaDoc < 1.4",
  1380. Project.MSG_VERBOSE);
  1381. }
  1382. this.linksource = b;
  1383. }
  1384. /**
  1385. * Enables the -linksource switch, will be ignored if javadoc is not
  1386. * the 1.4 version. Default is false
  1387. *
  1388. * @since Ant 1.6
  1389. */
  1390. public void setBreakiterator(boolean b) {
  1391. if (!javadoc4) {
  1392. log ("-breakiterator option not supported on JavaDoc < 1.4",
  1393. Project.MSG_VERBOSE);
  1394. }
  1395. this.breakiterator = b;
  1396. }
  1397. /**
  1398. * Enables the -noqualifier switch, will be ignored if javadoc is not
  1399. * the 1.4 version.
  1400. *
  1401. * @since Ant 1.6
  1402. */
  1403. public void setNoqualifier(String noqualifier) {
  1404. if (!javadoc4) {
  1405. log ("-noqualifier option not supported on JavaDoc < 1.4",
  1406. Project.MSG_VERBOSE);
  1407. }
  1408. this.noqualifier = noqualifier;
  1409. }
  1410. public void execute() throws BuildException {
  1411. if ("javadoc2".equals(getTaskType())) {
  1412. log("!! javadoc2 is deprecated. Use javadoc instead. !!");
  1413. }
  1414. Vector packagesToDoc = new Vector();
  1415. Path sourceDirs = new Path(getProject());
  1416. if (packageList != null && sourcePath == null) {
  1417. String msg = "sourcePath attribute must be set when "
  1418. + "specifying packagelist.";
  1419. throw new BuildException(msg);
  1420. }
  1421. if (sourcePath != null) {
  1422. sourceDirs.addExisting(sourcePath);
  1423. }
  1424. parsePackages(packagesToDoc, sourceDirs);
  1425. if (packagesToDoc.size() != 0 && sourceDirs.size() == 0) {
  1426. String msg = "sourcePath attribute must be set when "
  1427. + "specifying package names.";
  1428. throw new BuildException(msg);
  1429. }
  1430. Vector sourceFilesToDoc = (Vector) sourceFiles.clone();
  1431. addFileSets(sourceFilesToDoc);
  1432. if (packageList == null && packagesToDoc.size() == 0
  1433. && sourceFilesToDoc.size() == 0) {
  1434. throw new BuildException("No source files and no packages have "
  1435. + "been specified.");
  1436. }
  1437. log("Generating Javadoc", Project.MSG_INFO);
  1438. Commandline toExecute = (Commandline) cmd.clone();
  1439. toExecute.setExecutable(JavaEnvUtils.getJdkExecutable("javadoc"));
  1440. // ------------------------------------------ general javadoc arguments
  1441. if (doctitle != null) {
  1442. toExecute.createArgument().setValue("-doctitle");
  1443. toExecute.createArgument().setValue(expand(doctitle.getText()));
  1444. }
  1445. if (header != null) {
  1446. toExecute.createArgument().setValue("-header");
  1447. toExecute.createArgument().setValue(expand(header.getText()));
  1448. }
  1449. if (footer != null) {
  1450. toExecute.createArgument().setValue("-footer");
  1451. toExecute.createArgument().setValue(expand(footer.getText()));
  1452. }
  1453. if (bottom != null) {
  1454. toExecute.createArgument().setValue("-bottom");
  1455. toExecute.createArgument().setValue(expand(bottom.getText()));
  1456. }
  1457. if (classpath == null) {
  1458. classpath = (new Path(getProject())).concatSystemClasspath("last");
  1459. } else {
  1460. classpath = classpath.concatSystemClasspath("ignore");
  1461. }
  1462. if (!javadoc1) {
  1463. if (classpath.size() > 0) {
  1464. toExecute.createArgument().setValue("-classpath");
  1465. toExecute.createArgument().setPath(classpath);
  1466. }
  1467. if (sourceDirs.size() > 0) {
  1468. toExecute.createArgument().setValue("-sourcepath");
  1469. toExecute.createArgument().setPath(sourceDirs);
  1470. }
  1471. } else {
  1472. sourceDirs.append(classpath);
  1473. if (sourceDirs.size() > 0) {
  1474. toExecute.createArgument().setValue("-classpath");
  1475. toExecute.createArgument().setPath(sourceDirs);
  1476. }
  1477. }
  1478. if (version && doclet == null) {
  1479. toExecute.createArgument().setValue("-version");
  1480. }
  1481. if (author && doclet == null) {
  1482. toExecute.createArgument().setValue("-author");
  1483. }
  1484. if (javadoc1 || doclet == null) {
  1485. if (destDir == null) {
  1486. String msg = "destDir attribute must be set!";
  1487. throw new BuildException(msg);
  1488. }
  1489. }
  1490. // ---------------------------- javadoc2 arguments for default doclet
  1491. if (!javadoc1) {
  1492. if (doclet != null) {
  1493. if (doclet.getName() == null) {
  1494. throw new BuildException("The doclet name must be "
  1495. + "specified.", getLocation());
  1496. } else {
  1497. toExecute.createArgument().setValue("-doclet");
  1498. toExecute.createArgument().setValue(doclet.getName());
  1499. if (doclet.getPath() != null) {
  1500. Path docletPath
  1501. = doclet.getPath().concatSystemClasspath("ignore");
  1502. if (docletPath.size() != 0) {
  1503. toExecute.createArgument().setValue("-docletpath");
  1504. toExecute.createArgument().setPath(docletPath);
  1505. }
  1506. }
  1507. for (Enumeration e = doclet.getParams();
  1508. e.hasMoreElements();) {
  1509. DocletParam param = (DocletParam) e.nextElement();
  1510. if (param.getName() == null) {
  1511. throw new BuildException("Doclet parameters must "
  1512. + "have a name");
  1513. }
  1514. toExecute.createArgument().setValue(param.getName());
  1515. if (param.getValue() != null) {
  1516. toExecute.createArgument()
  1517. .setValue(param.getValue());
  1518. }
  1519. }
  1520. }
  1521. }
  1522. if (bootclasspath != null && bootclasspath.size() > 0) {
  1523. toExecute.createArgument().setValue("-bootclasspath");
  1524. toExecute.createArgument().setPath(bootclasspath);
  1525. }
  1526. // add the links arguments
  1527. if (links.size() != 0) {
  1528. for (Enumeration e = links.elements(); e.hasMoreElements();) {
  1529. LinkArgument la = (LinkArgument) e.nextElement();
  1530. if (la.getHref() == null || la.getHref().length() == 0) {
  1531. log("No href was given for the link - skipping",
  1532. Project.MSG_VERBOSE);
  1533. continue;
  1534. } else {
  1535. // is the href a valid URL
  1536. try {
  1537. URL base = new URL("file://.");
  1538. new URL(base, la.getHref());
  1539. } catch (MalformedURLException mue) {
  1540. // ok - just skip
  1541. log("Link href \"" + la.getHref()
  1542. + "\" is not a valid url - skipping link",
  1543. Project.MSG_WARN);
  1544. continue;
  1545. }
  1546. }
  1547. if (la.isLinkOffline()) {
  1548. File packageListLocation = la.getPackagelistLoc();
  1549. if (packageListLocation == null) {
  1550. throw new BuildException("The package list "
  1551. + " location for link " + la.getHref()
  1552. + " must be provided because the link is "
  1553. + "offline");
  1554. }
  1555. File packageListFile =
  1556. new File(packageListLocation, "package-list");
  1557. if (packageListFile.exists()) {
  1558. try {
  1559. String packageListURL =
  1560. fileUtils.getFileURL(packageListLocation)
  1561. .toExternalForm();
  1562. toExecute.createArgument()
  1563. .setValue("-linkoffline");
  1564. toExecute.createArgument()
  1565. .setValue(la.getHref());
  1566. toExecute.createArgument()
  1567. .setValue(packageListURL);
  1568. } catch (MalformedURLException ex) {
  1569. log("Warning: Package list location was "
  1570. + "invalid " + packageListLocation,
  1571. Project.MSG_WARN);
  1572. }
  1573. } else {
  1574. log("Warning: No package list was found at "
  1575. + packageListLocation, Project.MSG_VERBOSE);
  1576. }
  1577. } else {
  1578. toExecute.createArgument().setValue("-link");
  1579. toExecute.createArgument().setValue(la.getHref());
  1580. }
  1581. }
  1582. }
  1583. // add the single group arguments
  1584. // Javadoc 1.2 rules:
  1585. // Multiple -group args allowed.
  1586. // Each arg includes 3 strings: -group [name] [packagelist].
  1587. // Elements in [packagelist] are colon-delimited.
  1588. // An element in [packagelist] may end with the * wildcard.
  1589. // Ant javadoc task rules for group attribute:
  1590. // Args are comma-delimited.
  1591. // Each arg is 2 space-delimited strings.
  1592. // E.g., group="XSLT_Packages org.apache.xalan.xslt*,
  1593. // XPath_Packages org.apache.xalan.xpath*"
  1594. if (group != null) {
  1595. StringTokenizer tok = new StringTokenizer(group, ",", false);
  1596. while (tok.hasMoreTokens()) {
  1597. String grp = tok.nextToken().trim();
  1598. int space = grp.indexOf(" ");
  1599. if (space > 0) {
  1600. String name = grp.substring(0, space);
  1601. String pkgList = grp.substring(space + 1);
  1602. toExecute.createArgument().setValue("-group");
  1603. toExecute.createArgument().setValue(name);
  1604. toExecute.createArgument().setValue(pkgList);
  1605. }
  1606. }
  1607. }
  1608. // add the group arguments
  1609. if (groups.size() != 0) {
  1610. for (Enumeration e = groups.elements(); e.hasMoreElements();) {
  1611. GroupArgument ga = (GroupArgument) e.nextElement();
  1612. String title = ga.getTitle();
  1613. String packages = ga.getPackages();
  1614. if (title == null || packages == null) {
  1615. throw new BuildException("The title and packages must "
  1616. + "be specified for group "
  1617. + "elements.");
  1618. }
  1619. toExecute.createArgument().setValue("-group");
  1620. toExecute.createArgument().setValue(expand(title));
  1621. toExecute.createArgument().setValue(packages);
  1622. }
  1623. }
  1624. // JavaDoc 1.4 parameters
  1625. if (javadoc4) {
  1626. for (Enumeration e = tags.elements(); e.hasMoreElements();) {
  1627. Object element = e.nextElement();
  1628. if (element instanceof TagArgument) {
  1629. TagArgument ta = (TagArgument) element;
  1630. File tagDir = ta.getDir(getProject());
  1631. if (tagDir == null) {
  1632. // The tag element is not used as a fileset,
  1633. // but specifies the tag directly.
  1634. toExecute.createArgument().setValue ("-tag");
  1635. toExecute.createArgument().setValue (ta.getParameter());
  1636. } else {
  1637. // The tag element is used as a fileset. Parse all the files and
  1638. // create -tag arguments.
  1639. DirectoryScanner tagDefScanner = ta.getDirectoryScanner(getProject());
  1640. String[] files = tagDefScanner.getIncludedFiles();
  1641. for (int i = 0; i < files.length; i++) {
  1642. File tagDefFile = new File(tagDir, files[i]);
  1643. try {
  1644. BufferedReader in
  1645. = new BufferedReader(new FileReader(tagDefFile));
  1646. String line = null;
  1647. while ((line = in.readLine()) != null) {
  1648. toExecute.createArgument().setValue ("-tag");
  1649. toExecute.createArgument().setValue (line);
  1650. }
  1651. in.close();
  1652. } catch (IOException ioe) {
  1653. throw new BuildException("Couldn't read "
  1654. + " tag file from "
  1655. + tagDefFile.getAbsolutePath(), ioe);
  1656. }
  1657. }
  1658. }
  1659. } else {
  1660. ExtensionInfo tagletInfo = (ExtensionInfo) element;
  1661. toExecute.createArgument().setValue("-taglet");
  1662. toExecute.createArgument().setValue(tagletInfo
  1663. .getName());
  1664. if (tagletInfo.getPath() != null) {
  1665. Path tagletPath = tagletInfo.getPath()
  1666. .concatSystemClasspath("ignore");
  1667. if (tagletPath.size() != 0) {
  1668. toExecute.createArgument()
  1669. .setValue("-tagletpath");
  1670. toExecute.createArgument().setPath(tagletPath);
  1671. }
  1672. }
  1673. }
  1674. }
  1675. if (source != null) {
  1676. toExecute.createArgument().setValue("-source");
  1677. toExecute.createArgument().setValue(source);
  1678. }
  1679. if (linksource && doclet == null) {
  1680. toExecute.createArgument().setValue("-linksource");
  1681. }
  1682. if (breakiterator && doclet == null) {
  1683. toExecute.createArgument().setValue("-breakiterator");
  1684. }
  1685. if (noqualifier != null && doclet == null) {
  1686. toExecute.createArgument().setValue("-noqualifier");
  1687. toExecute.createArgument().setValue(noqualifier);
  1688. }
  1689. }
  1690. }
  1691. File tmpList = null;
  1692. PrintWriter srcListWriter = null;
  1693. try {
  1694. /**
  1695. * Write sourcefiles and package names to a temporary file
  1696. * if requested.
  1697. */
  1698. if (useExternalFile) {
  1699. if (tmpList == null) {
  1700. tmpList = fileUtils.createTempFile("javadoc", "", null);
  1701. tmpList.deleteOnExit();
  1702. toExecute.createArgument()
  1703. .setValue("@" + tmpList.getAbsolutePath());
  1704. }
  1705. srcListWriter = new PrintWriter(
  1706. new FileWriter(tmpList.getAbsolutePath(),
  1707. true));
  1708. }
  1709. Enumeration e = packagesToDoc.elements();
  1710. while (e.hasMoreElements()) {
  1711. String packageName = (String) e.nextElement();
  1712. if (useExternalFile) {
  1713. srcListWriter.println(packageName);
  1714. } else {
  1715. toExecute.createArgument().setValue(packageName);
  1716. }
  1717. }
  1718. e = sourceFilesToDoc.elements();
  1719. while (e.hasMoreElements()) {
  1720. SourceFile sf = (SourceFile) e.nextElement();
  1721. String sourceFileName = sf.getFile().getAbsolutePath();
  1722. if (useExternalFile) {
  1723. if (javadoc4 && sourceFileName.indexOf(" ") > -1) {
  1724. srcListWriter.println("\"" + sourceFileName + "\"");
  1725. } else {
  1726. srcListWriter.println(sourceFileName);
  1727. }
  1728. } else {
  1729. toExecute.createArgument().setValue(sourceFileName);
  1730. }
  1731. }
  1732. } catch (IOException e) {
  1733. tmpList.delete();
  1734. throw new BuildException("Error creating temporary file",
  1735. e, getLocation());
  1736. } finally {
  1737. if (srcListWriter != null) {
  1738. srcListWriter.close();
  1739. }
  1740. }
  1741. if (packageList != null) {
  1742. toExecute.createArgument().setValue("@" + packageList);
  1743. }
  1744. log(toExecute.describeCommand(), Project.MSG_VERBOSE);
  1745. log("Javadoc execution", Project.MSG_INFO);
  1746. JavadocOutputStream out = new JavadocOutputStream(Project.MSG_INFO);
  1747. JavadocOutputStream err = new JavadocOutputStream(Project.MSG_WARN);
  1748. Execute exe = new Execute(new PumpStreamHandler(out, err));
  1749. exe.setAntRun(getProject());
  1750. /*
  1751. * No reason to change the working directory as all filenames and
  1752. * path components have been resolved already.
  1753. *
  1754. * Avoid problems with command line length in some environments.
  1755. */
  1756. exe.setWorkingDirectory(null);
  1757. try {
  1758. exe.setCommandline(toExecute.getCommandline());
  1759. int ret = exe.execute();
  1760. if (ret != 0 && failOnError) {
  1761. throw new BuildException("Javadoc returned " + ret, getLocation());
  1762. }
  1763. } catch (IOException e) {
  1764. throw new BuildException("Javadoc failed: " + e, e, getLocation());
  1765. } finally {
  1766. if (tmpList != null) {
  1767. tmpList.delete();
  1768. tmpList = null;
  1769. }
  1770. out.logFlush();
  1771. err.logFlush();
  1772. try {
  1773. out.close();
  1774. err.close();
  1775. } catch (IOException e) {
  1776. // ignore
  1777. }
  1778. }
  1779. }
  1780. /**
  1781. * Add the files matched by the nested filesets to the Vector as
  1782. * SourceFile instances.
  1783. *
  1784. * @since 1.5
  1785. */
  1786. private void addFileSets(Vector sf) {
  1787. Enumeration e = fileSets.elements();
  1788. while (e.hasMoreElements()) {
  1789. FileSet fs = (FileSet) e.nextElement();
  1790. if (!fs.hasPatterns() && !fs.hasSelectors()) {
  1791. fs = (FileSet) fs.clone();
  1792. fs.createInclude().setName("**/*.java");
  1793. }
  1794. File baseDir = fs.getDir(getProject());
  1795. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  1796. String[] files = ds.getIncludedFiles();
  1797. for (int i = 0; i < files.length; i++) {
  1798. sf.addElement(new SourceFile(new File(baseDir, files[i])));
  1799. }
  1800. }
  1801. }
  1802. /**
  1803. * Add the directories matched by the nested dirsets to the Vector
  1804. * and the base directories of the dirsets to the Path. It also
  1805. * handles the packages and excludepackages attributes and
  1806. * elements.
  1807. *
  1808. * @since 1.5
  1809. */
  1810. private void parsePackages(Vector pn, Path sp) {
  1811. Vector addedPackages = new Vector();
  1812. Vector dirSets = (Vector) packageSets.clone();
  1813. // for each sourcePath entry, add a directoryset with includes
  1814. // taken from packagenames attribute and nested package
  1815. // elements and excludes taken from excludepackages attribute
  1816. // and nested excludepackage elements
  1817. if (sourcePath != null && packageNames.size() > 0) {
  1818. PatternSet ps = new PatternSet();
  1819. Enumeration e = packageNames.elements();
  1820. while (e.hasMoreElements()) {
  1821. PackageName p = (PackageName) e.nextElement();
  1822. String pkg = p.getName().replace('.', '/');
  1823. if (pkg.endsWith("*")) {
  1824. pkg += "*";
  1825. }
  1826. ps.createInclude().setName(pkg);
  1827. }
  1828. e = excludePackageNames.elements();
  1829. while (e.hasMoreElements()) {
  1830. PackageName p = (PackageName) e.nextElement();
  1831. String pkg = p.getName().replace('.', '/');
  1832. if (pkg.endsWith("*")) {
  1833. pkg += "*";
  1834. }
  1835. ps.createExclude().setName(pkg);
  1836. }
  1837. String[] pathElements = sourcePath.list();
  1838. for (int i = 0; i < pathElements.length; i++) {
  1839. DirSet ds = new DirSet();
  1840. ds.setDefaultexcludes(useDefaultExcludes);
  1841. ds.setDir(new File(pathElements[i]));
  1842. ds.createPatternSet().addConfiguredPatternset(ps);
  1843. dirSets.addElement(ds);
  1844. }
  1845. }
  1846. Enumeration e = dirSets.elements();
  1847. while (e.hasMoreElements()) {
  1848. DirSet ds = (DirSet) e.nextElement();
  1849. File baseDir = ds.getDir(getProject());
  1850. log("scanning " + baseDir + " for packages.", Project.MSG_DEBUG);
  1851. DirectoryScanner dsc = ds.getDirectoryScanner(getProject());
  1852. String[] dirs = dsc.getIncludedDirectories();
  1853. boolean containsPackages = false;
  1854. for (int i = 0; i < dirs.length; i++) {
  1855. // are there any java files in this directory?
  1856. File pd = new File(baseDir, dirs[i]);
  1857. String[] files = pd.list(new FilenameFilter () {
  1858. public boolean accept(File dir1, String name) {
  1859. if (name.endsWith(".java")) {
  1860. return true;
  1861. }
  1862. return false; // ignore dirs
  1863. }
  1864. });
  1865. if (files.length > 0) {
  1866. containsPackages = true;
  1867. String packageName =
  1868. dirs[i].replace(File.separatorChar, '.');
  1869. if (!addedPackages.contains(packageName)) {
  1870. addedPackages.addElement(packageName);
  1871. pn.addElement(packageName);
  1872. }
  1873. }
  1874. }
  1875. if (containsPackages) {
  1876. // We don't need to care for duplicates here,
  1877. // Path.list does it for us.
  1878. sp.createPathElement().setLocation(baseDir);
  1879. } else {
  1880. log(baseDir + " doesn\'t contain any packages, dropping it.",
  1881. Project.MSG_VERBOSE);
  1882. }
  1883. }
  1884. }
  1885. private class JavadocOutputStream extends LogOutputStream {
  1886. JavadocOutputStream(int level) {
  1887. super(Javadoc.this, level);
  1888. }
  1889. //
  1890. // Override the logging of output in order to filter out Generating
  1891. // messages. Generating messages are set to a priority of VERBOSE
  1892. // unless they appear after what could be an informational message.
  1893. //
  1894. private String queuedLine = null;
  1895. protected void processLine(String line, int messageLevel) {
  1896. if (messageLevel == Project.MSG_INFO
  1897. && line.startsWith("Generating ")) {
  1898. if (queuedLine != null) {
  1899. super.processLine(queuedLine, Project.MSG_VERBOSE);
  1900. }
  1901. queuedLine = line;
  1902. } else {
  1903. if (queuedLine != null) {
  1904. if (line.startsWith("Building ")) {
  1905. super.processLine(queuedLine, Project.MSG_VERBOSE);
  1906. } else {
  1907. super.processLine(queuedLine, Project.MSG_INFO);
  1908. }
  1909. queuedLine = null;
  1910. }
  1911. super.processLine(line, messageLevel);
  1912. }
  1913. }
  1914. protected void logFlush() {
  1915. if (queuedLine != null) {
  1916. super.processLine(queuedLine, Project.MSG_VERBOSE);
  1917. queuedLine = null;
  1918. }
  1919. }
  1920. }
  1921. /**
  1922. * Convenience method to expand properties.
  1923. */
  1924. protected String expand(String content) {
  1925. return getProject().replaceProperties(content);
  1926. }
  1927. }