Thu, 31 Aug 2017 15:17:03 +0800
merge
1 /*
2 * Copyright (c) 1997, 2013, 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 java.io.*;
28 import java.util.*;
30 import com.sun.javadoc.*;
31 import com.sun.tools.javac.sym.Profiles;
32 import com.sun.tools.javac.jvm.Profile;
33 import com.sun.tools.doclets.internal.toolkit.*;
34 import com.sun.tools.doclets.internal.toolkit.builders.*;
35 import com.sun.tools.doclets.internal.toolkit.util.*;
37 /**
38 * The class with "start" method, calls individual Writers.
39 *
40 * <p><b>This is NOT part of any supported API.
41 * If you write code that depends on this, you do so at your own risk.
42 * This code and its internal interfaces are subject to change or
43 * deletion without notice.</b>
44 *
45 * @author Atul M Dambalkar
46 * @author Robert Field
47 * @author Jamie Ho
48 *
49 */
50 public class HtmlDoclet extends AbstractDoclet {
51 // An instance will be created by validOptions, and used by start.
52 private static HtmlDoclet docletToStart = null;
54 public HtmlDoclet() {
55 configuration = new ConfigurationImpl();
56 }
58 /**
59 * The global configuration information for this run.
60 */
61 public final ConfigurationImpl configuration;
63 /**
64 * The "start" method as required by Javadoc.
65 *
66 * @param root the root of the documentation tree.
67 * @see com.sun.javadoc.RootDoc
68 * @return true if the doclet ran without encountering any errors.
69 */
70 public static boolean start(RootDoc root) {
71 // In typical use, options will have been set up by calling validOptions,
72 // which will create an HtmlDoclet for use here.
73 HtmlDoclet doclet;
74 if (docletToStart != null) {
75 doclet = docletToStart;
76 docletToStart = null;
77 } else {
78 doclet = new HtmlDoclet();
79 }
80 return doclet.start(doclet, root);
81 }
83 /**
84 * Create the configuration instance.
85 * Override this method to use a different
86 * configuration.
87 */
88 public Configuration configuration() {
89 return configuration;
90 }
92 /**
93 * Start the generation of files. Call generate methods in the individual
94 * writers, which will in turn genrate the documentation files. Call the
95 * TreeWriter generation first to ensure the Class Hierarchy is built
96 * first and then can be used in the later generation.
97 *
98 * For new format.
99 *
100 * @see com.sun.javadoc.RootDoc
101 */
102 protected void generateOtherFiles(RootDoc root, ClassTree classtree)
103 throws Exception {
104 super.generateOtherFiles(root, classtree);
105 if (configuration.linksource) {
106 SourceToHTMLConverter.convertRoot(configuration,
107 root, DocPaths.SOURCE_OUTPUT);
108 }
110 if (configuration.topFile.isEmpty()) {
111 configuration.standardmessage.
112 error("doclet.No_Non_Deprecated_Classes_To_Document");
113 return;
114 }
115 boolean nodeprecated = configuration.nodeprecated;
116 performCopy(configuration.helpfile);
117 performCopy(configuration.stylesheetfile);
118 // do early to reduce memory footprint
119 if (configuration.classuse) {
120 ClassUseWriter.generate(configuration, classtree);
121 }
122 IndexBuilder indexbuilder = new IndexBuilder(configuration, nodeprecated);
124 if (configuration.createtree) {
125 TreeWriter.generate(configuration, classtree);
126 }
127 if (configuration.createindex) {
128 if (configuration.splitindex) {
129 SplitIndexWriter.generate(configuration, indexbuilder);
130 } else {
131 SingleIndexWriter.generate(configuration, indexbuilder);
132 }
133 }
135 if (!(configuration.nodeprecatedlist || nodeprecated)) {
136 DeprecatedListWriter.generate(configuration);
137 }
139 AllClassesFrameWriter.generate(configuration,
140 new IndexBuilder(configuration, nodeprecated, true));
142 FrameOutputWriter.generate(configuration);
144 if (configuration.createoverview) {
145 PackageIndexWriter.generate(configuration);
146 }
147 if (configuration.helpfile.length() == 0 &&
148 !configuration.nohelp) {
149 HelpWriter.generate(configuration);
150 }
151 // If a stylesheet file is not specified, copy the default stylesheet
152 // and replace newline with platform-specific newline.
153 DocFile f;
154 if (configuration.stylesheetfile.length() == 0) {
155 f = DocFile.createFileForOutput(configuration, DocPaths.STYLESHEET);
156 f.copyResource(DocPaths.RESOURCES.resolve(DocPaths.STYLESHEET), false, true);
157 }
158 f = DocFile.createFileForOutput(configuration, DocPaths.JAVASCRIPT);
159 f.copyResource(DocPaths.RESOURCES.resolve(DocPaths.JAVASCRIPT), true, true);
160 }
162 /**
163 * {@inheritDoc}
164 */
165 protected void generateClassFiles(ClassDoc[] arr, ClassTree classtree) {
166 Arrays.sort(arr);
167 for(int i = 0; i < arr.length; i++) {
168 if (!(configuration.isGeneratedDoc(arr[i]) && arr[i].isIncluded())) {
169 continue;
170 }
171 ClassDoc prev = (i == 0)?
172 null:
173 arr[i-1];
174 ClassDoc curr = arr[i];
175 ClassDoc next = (i+1 == arr.length)?
176 null:
177 arr[i+1];
178 try {
179 if (curr.isAnnotationType()) {
180 AbstractBuilder annotationTypeBuilder =
181 configuration.getBuilderFactory()
182 .getAnnotationTypeBuilder((AnnotationTypeDoc) curr,
183 prev, next);
184 annotationTypeBuilder.build();
185 } else {
186 AbstractBuilder classBuilder =
187 configuration.getBuilderFactory()
188 .getClassBuilder(curr, prev, next, classtree);
189 classBuilder.build();
190 }
191 } catch (IOException e) {
192 throw new DocletAbortException(e);
193 } catch (DocletAbortException de) {
194 throw de;
195 } catch (Exception e) {
196 e.printStackTrace();
197 throw new DocletAbortException(e);
198 }
199 }
200 }
202 /**
203 * {@inheritDoc}
204 */
205 protected void generateProfileFiles() throws Exception {
206 if (configuration.showProfiles && configuration.profilePackages.size() > 0) {
207 ProfileIndexFrameWriter.generate(configuration);
208 Profile prevProfile = null, nextProfile;
209 String profileName;
210 for (int i = 1; i < configuration.profiles.getProfileCount(); i++) {
211 profileName = Profile.lookup(i).name;
212 // Generate profile package pages only if there are any packages
213 // in a profile to be documented. The profilePackages map will not
214 // contain an entry for the profile if there are no packages to be documented.
215 if (!configuration.shouldDocumentProfile(profileName))
216 continue;
217 ProfilePackageIndexFrameWriter.generate(configuration, profileName);
218 PackageDoc[] packages = configuration.profilePackages.get(
219 profileName);
220 PackageDoc prev = null, next;
221 for (int j = 0; j < packages.length; j++) {
222 // if -nodeprecated option is set and the package is marked as
223 // deprecated, do not generate the profilename-package-summary.html
224 // and profilename-package-frame.html pages for that package.
225 if (!(configuration.nodeprecated && Util.isDeprecated(packages[j]))) {
226 ProfilePackageFrameWriter.generate(configuration, packages[j], i);
227 next = (j + 1 < packages.length
228 && packages[j + 1].name().length() > 0) ? packages[j + 1] : null;
229 AbstractBuilder profilePackageSummaryBuilder =
230 configuration.getBuilderFactory().getProfilePackageSummaryBuilder(
231 packages[j], prev, next, Profile.lookup(i));
232 profilePackageSummaryBuilder.build();
233 prev = packages[j];
234 }
235 }
236 nextProfile = (i + 1 < configuration.profiles.getProfileCount()) ?
237 Profile.lookup(i + 1) : null;
238 AbstractBuilder profileSummaryBuilder =
239 configuration.getBuilderFactory().getProfileSummaryBuilder(
240 Profile.lookup(i), prevProfile, nextProfile);
241 profileSummaryBuilder.build();
242 prevProfile = Profile.lookup(i);
243 }
244 }
245 }
247 /**
248 * {@inheritDoc}
249 */
250 protected void generatePackageFiles(ClassTree classtree) throws Exception {
251 PackageDoc[] packages = configuration.packages;
252 if (packages.length > 1) {
253 PackageIndexFrameWriter.generate(configuration);
254 }
255 PackageDoc prev = null, next;
256 for (int i = 0; i < packages.length; i++) {
257 // if -nodeprecated option is set and the package is marked as
258 // deprecated, do not generate the package-summary.html, package-frame.html
259 // and package-tree.html pages for that package.
260 if (!(configuration.nodeprecated && Util.isDeprecated(packages[i]))) {
261 PackageFrameWriter.generate(configuration, packages[i]);
262 next = (i + 1 < packages.length &&
263 packages[i + 1].name().length() > 0) ? packages[i + 1] : null;
264 //If the next package is unnamed package, skip 2 ahead if possible
265 next = (i + 2 < packages.length && next == null) ? packages[i + 2] : next;
266 AbstractBuilder packageSummaryBuilder =
267 configuration.getBuilderFactory().getPackageSummaryBuilder(
268 packages[i], prev, next);
269 packageSummaryBuilder.build();
270 if (configuration.createtree) {
271 PackageTreeWriter.generate(configuration,
272 packages[i], prev, next,
273 configuration.nodeprecated);
274 }
275 prev = packages[i];
276 }
277 }
278 }
280 public static final ConfigurationImpl sharedInstanceForOptions =
281 new ConfigurationImpl();
283 /**
284 * Check for doclet added options here.
285 *
286 * @return number of arguments to option. Zero return means
287 * option not known. Negative value means error occurred.
288 */
289 public static int optionLength(String option) {
290 // Construct temporary configuration for check
291 return sharedInstanceForOptions.optionLength(option);
292 }
294 /**
295 * Check that options have the correct arguments here.
296 * <P>
297 * This method is not required and will default gracefully
298 * (to true) if absent.
299 * <P>
300 * Printing option related error messages (using the provided
301 * DocErrorReporter) is the responsibility of this method.
302 *
303 * @return true if the options are valid.
304 */
305 public static boolean validOptions(String options[][],
306 DocErrorReporter reporter) {
307 docletToStart = new HtmlDoclet();
308 return docletToStart.configuration.validOptions(options, reporter);
309 }
311 private void performCopy(String filename) {
312 if (filename.isEmpty())
313 return;
315 try {
316 DocFile fromfile = DocFile.createFileForInput(configuration, filename);
317 DocPath path = DocPath.create(fromfile.getName());
318 DocFile toFile = DocFile.createFileForOutput(configuration, path);
319 if (toFile.isSameFile(fromfile))
320 return;
322 configuration.message.notice((SourcePosition) null,
323 "doclet.Copying_File_0_To_File_1",
324 fromfile.toString(), path.getPath());
325 toFile.copyFile(fromfile);
326 } catch (IOException exc) {
327 configuration.message.error((SourcePosition) null,
328 "doclet.perform_copy_exception_encountered",
329 exc.toString());
330 throw new DocletAbortException(exc);
331 }
332 }
333 }