Thu, 13 Jan 2011 21:28:38 -0800
7010528: javadoc performance regression
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.tools.doclets.formats.html;
27 import com.sun.tools.doclets.internal.toolkit.*;
28 import com.sun.tools.doclets.internal.toolkit.builders.*;
29 import com.sun.tools.doclets.internal.toolkit.util.*;
31 import com.sun.javadoc.*;
32 import java.util.*;
33 import java.io.*;
35 /**
36 * The class with "start" method, calls individual Writers.
37 *
38 * @author Atul M Dambalkar
39 * @author Robert Field
40 * @author Jamie Ho
41 *
42 */
43 public class HtmlDoclet extends AbstractDoclet {
44 public HtmlDoclet() {
45 configuration = (ConfigurationImpl) configuration();
46 }
48 /**
49 * The global configuration information for this run.
50 */
51 public ConfigurationImpl configuration;
53 /**
54 * The "start" method as required by Javadoc.
55 *
56 * @param root the root of the documentation tree.
57 * @see com.sun.javadoc.RootDoc
58 * @return true if the doclet ran without encountering any errors.
59 */
60 public static boolean start(RootDoc root) {
61 try {
62 HtmlDoclet doclet = new HtmlDoclet();
63 return doclet.start(doclet, root);
64 } finally {
65 ConfigurationImpl.reset();
66 }
67 }
69 /**
70 * Create the configuration instance.
71 * Override this method to use a different
72 * configuration.
73 */
74 public Configuration configuration() {
75 return ConfigurationImpl.getInstance();
76 }
78 /**
79 * Start the generation of files. Call generate methods in the individual
80 * writers, which will in turn genrate the documentation files. Call the
81 * TreeWriter generation first to ensure the Class Hierarchy is built
82 * first and then can be used in the later generation.
83 *
84 * For new format.
85 *
86 * @see com.sun.javadoc.RootDoc
87 */
88 protected void generateOtherFiles(RootDoc root, ClassTree classtree)
89 throws Exception {
90 super.generateOtherFiles(root, classtree);
91 if (configuration.linksource) {
92 if (configuration.destDirName.length() > 0) {
93 SourceToHTMLConverter.convertRoot(configuration,
94 root, configuration.destDirName + File.separator
95 + DocletConstants.SOURCE_OUTPUT_DIR_NAME);
96 } else {
97 SourceToHTMLConverter.convertRoot(configuration,
98 root, DocletConstants.SOURCE_OUTPUT_DIR_NAME);
99 }
100 }
102 if (configuration.topFile.length() == 0) {
103 configuration.standardmessage.
104 error("doclet.No_Non_Deprecated_Classes_To_Document");
105 return;
106 }
107 boolean nodeprecated = configuration.nodeprecated;
108 String configdestdir = configuration.destDirName;
109 String confighelpfile = configuration.helpfile;
110 String configstylefile = configuration.stylesheetfile;
111 performCopy(configdestdir, confighelpfile);
112 performCopy(configdestdir, configstylefile);
113 Util.copyResourceFile(configuration, "inherit.gif", false);
114 // do early to reduce memory footprint
115 if (configuration.classuse) {
116 ClassUseWriter.generate(configuration, classtree);
117 }
118 IndexBuilder indexbuilder = new IndexBuilder(configuration, nodeprecated);
120 if (configuration.createtree) {
121 TreeWriter.generate(configuration, classtree);
122 }
123 if (configuration.createindex) {
124 if (configuration.splitindex) {
125 SplitIndexWriter.generate(configuration, indexbuilder);
126 } else {
127 SingleIndexWriter.generate(configuration, indexbuilder);
128 }
129 }
131 if (!(configuration.nodeprecatedlist || nodeprecated)) {
132 DeprecatedListWriter.generate(configuration);
133 }
135 AllClassesFrameWriter.generate(configuration,
136 new IndexBuilder(configuration, nodeprecated, true));
138 FrameOutputWriter.generate(configuration);
140 if (configuration.createoverview) {
141 PackageIndexWriter.generate(configuration);
142 }
143 if (configuration.helpfile.length() == 0 &&
144 !configuration.nohelp) {
145 HelpWriter.generate(configuration);
146 }
147 // If a stylesheet file is not specified, copy the default stylesheet
148 // and replace newline with platform-specific newline.
149 if (configuration.stylesheetfile.length() == 0) {
150 Util.copyFile(configuration, "stylesheet.css", Util.RESOURCESDIR,
151 (configdestdir.isEmpty()) ?
152 System.getProperty("user.dir") : configdestdir, false, true);
153 }
154 }
156 /**
157 * {@inheritDoc}
158 */
159 protected void generateClassFiles(ClassDoc[] arr, ClassTree classtree) {
160 Arrays.sort(arr);
161 for(int i = 0; i < arr.length; i++) {
162 if (!(configuration.isGeneratedDoc(arr[i]) && arr[i].isIncluded())) {
163 continue;
164 }
165 ClassDoc prev = (i == 0)?
166 null:
167 arr[i-1];
168 ClassDoc curr = arr[i];
169 ClassDoc next = (i+1 == arr.length)?
170 null:
171 arr[i+1];
172 try {
173 if (curr.isAnnotationType()) {
174 AbstractBuilder annotationTypeBuilder =
175 configuration.getBuilderFactory()
176 .getAnnotationTypeBuilder((AnnotationTypeDoc) curr,
177 prev, next);
178 annotationTypeBuilder.build();
179 } else {
180 AbstractBuilder classBuilder =
181 configuration.getBuilderFactory()
182 .getClassBuilder(curr, prev, next, classtree);
183 classBuilder.build();
184 }
185 } catch (Exception e) {
186 e.printStackTrace();
187 throw new DocletAbortException();
188 }
189 }
190 }
192 /**
193 * {@inheritDoc}
194 */
195 protected void generatePackageFiles(ClassTree classtree) throws Exception {
196 PackageDoc[] packages = configuration.packages;
197 if (packages.length > 1) {
198 PackageIndexFrameWriter.generate(configuration);
199 }
200 PackageDoc prev = null, next;
201 for(int i = 0; i < packages.length; i++) {
202 PackageFrameWriter.generate(configuration, packages[i]);
203 next = (i + 1 < packages.length && packages[i+1].name().length() > 0) ?
204 packages[i+1] : null;
205 //If the next package is unnamed package, skip 2 ahead if possible
206 next = (i + 2 < packages.length && next == null) ?
207 packages[i+2]: next;
208 AbstractBuilder packageSummaryBuilder = configuration.
209 getBuilderFactory().getPackageSummaryBuilder(
210 packages[i], prev, next);
211 packageSummaryBuilder.build();
212 if (configuration.createtree) {
213 PackageTreeWriter.generate(configuration,
214 packages[i], prev, next,
215 configuration.nodeprecated);
216 }
217 prev = packages[i];
218 }
219 }
221 /**
222 * Check for doclet added options here.
223 *
224 * @return number of arguments to option. Zero return means
225 * option not known. Negative value means error occurred.
226 */
227 public static int optionLength(String option) {
228 // Construct temporary configuration for check
229 return (ConfigurationImpl.getInstance()).optionLength(option);
230 }
232 /**
233 * Check that options have the correct arguments here.
234 * <P>
235 * This method is not required and will default gracefully
236 * (to true) if absent.
237 * <P>
238 * Printing option related error messages (using the provided
239 * DocErrorReporter) is the responsibility of this method.
240 *
241 * @return true if the options are valid.
242 */
243 public static boolean validOptions(String options[][],
244 DocErrorReporter reporter) {
245 // Construct temporary configuration for check
246 return (ConfigurationImpl.getInstance()).validOptions(options, reporter);
247 }
249 private void performCopy(String configdestdir, String filename) {
250 try {
251 String destdir = (configdestdir.length() > 0) ?
252 configdestdir + File.separatorChar: "";
253 if (filename.length() > 0) {
254 File helpstylefile = new File(filename);
255 String parent = helpstylefile.getParent();
256 String helpstylefilename = (parent == null)?
257 filename:
258 filename.substring(parent.length() + 1);
259 File desthelpfile = new File(destdir + helpstylefilename);
260 if (!desthelpfile.getCanonicalPath().equals(
261 helpstylefile.getCanonicalPath())) {
262 configuration.message.
263 notice((SourcePosition) null,
264 "doclet.Copying_File_0_To_File_1",
265 helpstylefile.toString(), desthelpfile.toString());
266 Util.copyFile(desthelpfile, helpstylefile);
267 }
268 }
269 } catch (IOException exc) {
270 configuration.message.
271 error((SourcePosition) null,
272 "doclet.perform_copy_exception_encountered",
273 exc.toString());
274 throw new DocletAbortException();
275 }
276 }
277 }