Tue, 14 May 2013 10:14:54 -0700
8012175: Convert TagletOutputImpl to use ContentBuilder instead of StringBuilder
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1998, 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 */
26 package com.sun.tools.doclets.formats.html;
28 import java.net.*;
29 import java.util.*;
31 import javax.tools.JavaFileManager;
33 import com.sun.javadoc.*;
34 import com.sun.tools.doclets.formats.html.markup.ContentBuilder;
35 import com.sun.tools.doclets.internal.toolkit.*;
36 import com.sun.tools.doclets.internal.toolkit.util.*;
37 import com.sun.tools.doclint.DocLint;
38 import com.sun.tools.javac.file.JavacFileManager;
39 import com.sun.tools.javac.util.Context;
40 import com.sun.tools.javadoc.RootDocImpl;
42 /**
43 * Configure the output based on the command line options.
44 * <p>
45 * Also determine the length of the command line option. For example,
46 * for a option "-header" there will be a string argument associated, then the
47 * the length of option "-header" is two. But for option "-nohelp" no argument
48 * is needed so it's length is 1.
49 * </p>
50 * <p>
51 * Also do the error checking on the options used. For example it is illegal to
52 * use "-helpfile" option when already "-nohelp" option is used.
53 * </p>
54 *
55 * <p><b>This is NOT part of any supported API.
56 * If you write code that depends on this, you do so at your own risk.
57 * This code and its internal interfaces are subject to change or
58 * deletion without notice.</b>
59 *
60 * @author Robert Field.
61 * @author Atul Dambalkar.
62 * @author Jamie Ho
63 * @author Bhavesh Patel (Modified)
64 */
65 public class ConfigurationImpl extends Configuration {
67 /**
68 * The build date. Note: For now, we will use
69 * a version number instead of a date.
70 */
71 public static final String BUILD_DATE = System.getProperty("java.version");
73 /**
74 * Argument for command line option "-header".
75 */
76 public String header = "";
78 /**
79 * Argument for command line option "-packagesheader".
80 */
81 public String packagesheader = "";
83 /**
84 * Argument for command line option "-footer".
85 */
86 public String footer = "";
88 /**
89 * Argument for command line option "-doctitle".
90 */
91 public String doctitle = "";
93 /**
94 * Argument for command line option "-windowtitle".
95 */
96 public String windowtitle = "";
98 /**
99 * Argument for command line option "-top".
100 */
101 public String top = "";
103 /**
104 * Argument for command line option "-bottom".
105 */
106 public String bottom = "";
108 /**
109 * Argument for command line option "-helpfile".
110 */
111 public String helpfile = "";
113 /**
114 * Argument for command line option "-stylesheetfile".
115 */
116 public String stylesheetfile = "";
118 /**
119 * Argument for command line option "-Xdocrootparent".
120 */
121 public String docrootparent = "";
123 /**
124 * True if command line option "-nohelp" is used. Default value is false.
125 */
126 public boolean nohelp = false;
128 /**
129 * True if command line option "-splitindex" is used. Default value is
130 * false.
131 */
132 public boolean splitindex = false;
134 /**
135 * False if command line option "-noindex" is used. Default value is true.
136 */
137 public boolean createindex = true;
139 /**
140 * True if command line option "-use" is used. Default value is false.
141 */
142 public boolean classuse = false;
144 /**
145 * False if command line option "-notree" is used. Default value is true.
146 */
147 public boolean createtree = true;
149 /**
150 * True if command line option "-nodeprecated" is used. Default value is
151 * false.
152 */
153 public boolean nodeprecatedlist = false;
155 /**
156 * True if command line option "-nonavbar" is used. Default value is false.
157 */
158 public boolean nonavbar = false;
160 /**
161 * True if command line option "-nooverview" is used. Default value is
162 * false
163 */
164 private boolean nooverview = false;
166 /**
167 * True if command line option "-overview" is used. Default value is false.
168 */
169 public boolean overview = false;
171 /**
172 * This is true if option "-overview" is used or option "-overview" is not
173 * used and number of packages is more than one.
174 */
175 public boolean createoverview = false;
177 /**
178 * Collected set of doclint options
179 */
180 public Set<String> doclintOpts = new LinkedHashSet<String>();
182 /**
183 * Unique Resource Handler for this package.
184 */
185 public final MessageRetriever standardmessage;
187 /**
188 * First file to appear in the right-hand frame in the generated
189 * documentation.
190 */
191 public DocPath topFile = DocPath.empty;
193 /**
194 * The classdoc for the class file getting generated.
195 */
196 public ClassDoc currentcd = null; // Set this classdoc in the ClassWriter.
198 /**
199 * Constructor. Initializes resource for the
200 * {@link com.sun.tools.doclets.internal.toolkit.util.MessageRetriever MessageRetriever}.
201 */
202 public ConfigurationImpl() {
203 standardmessage = new MessageRetriever(this,
204 "com.sun.tools.doclets.formats.html.resources.standard");
205 }
207 /**
208 * Return the build date for the doclet.
209 */
210 @Override
211 public String getDocletSpecificBuildDate() {
212 return BUILD_DATE;
213 }
215 /**
216 * Depending upon the command line options provided by the user, set
217 * configure the output generation environment.
218 *
219 * @param options The array of option names and values.
220 */
221 @Override
222 public void setSpecificDocletOptions(String[][] options) {
223 for (int oi = 0; oi < options.length; ++oi) {
224 String[] os = options[oi];
225 String opt = os[0].toLowerCase();
226 if (opt.equals("-footer")) {
227 footer = os[1];
228 } else if (opt.equals("-header")) {
229 header = os[1];
230 } else if (opt.equals("-packagesheader")) {
231 packagesheader = os[1];
232 } else if (opt.equals("-doctitle")) {
233 doctitle = os[1];
234 } else if (opt.equals("-windowtitle")) {
235 windowtitle = os[1];
236 } else if (opt.equals("-top")) {
237 top = os[1];
238 } else if (opt.equals("-bottom")) {
239 bottom = os[1];
240 } else if (opt.equals("-helpfile")) {
241 helpfile = os[1];
242 } else if (opt.equals("-stylesheetfile")) {
243 stylesheetfile = os[1];
244 } else if (opt.equals("-charset")) {
245 charset = os[1];
246 } else if (opt.equals("-xdocrootparent")) {
247 docrootparent = os[1];
248 } else if (opt.equals("-nohelp")) {
249 nohelp = true;
250 } else if (opt.equals("-splitindex")) {
251 splitindex = true;
252 } else if (opt.equals("-noindex")) {
253 createindex = false;
254 } else if (opt.equals("-use")) {
255 classuse = true;
256 } else if (opt.equals("-notree")) {
257 createtree = false;
258 } else if (opt.equals("-nodeprecatedlist")) {
259 nodeprecatedlist = true;
260 } else if (opt.equals("-nonavbar")) {
261 nonavbar = true;
262 } else if (opt.equals("-nooverview")) {
263 nooverview = true;
264 } else if (opt.equals("-overview")) {
265 overview = true;
266 } else if (opt.equals("-xdoclint")) {
267 doclintOpts.add(null);
268 } else if (opt.startsWith("-xdoclint:")) {
269 doclintOpts.add(opt.substring(opt.indexOf(":") + 1));
270 }
271 }
272 if (root.specifiedClasses().length > 0) {
273 Map<String,PackageDoc> map = new HashMap<String,PackageDoc>();
274 PackageDoc pd;
275 ClassDoc[] classes = root.classes();
276 for (int i = 0; i < classes.length; i++) {
277 pd = classes[i].containingPackage();
278 if(! map.containsKey(pd.name())) {
279 map.put(pd.name(), pd);
280 }
281 }
282 }
283 setCreateOverview();
284 setTopFile(root);
286 if (root instanceof RootDocImpl) {
287 ((RootDocImpl) root).initDocLint(doclintOpts);
288 }
289 }
291 /**
292 * Returns the "length" of a given option. If an option takes no
293 * arguments, its length is one. If it takes one argument, it's
294 * length is two, and so on. This method is called by JavaDoc to
295 * parse the options it does not recognize. It then calls
296 * {@link #validOptions(String[][], DocErrorReporter)} to
297 * validate them.
298 * <b>Note:</b><br>
299 * The options arrive as case-sensitive strings. For options that
300 * are not case-sensitive, use toLowerCase() on the option string
301 * before comparing it.
302 * </blockquote>
303 *
304 * @return number of arguments + 1 for a option. Zero return means
305 * option not known. Negative value means error occurred.
306 */
307 public int optionLength(String option) {
308 int result = -1;
309 if ((result = super.optionLength(option)) > 0) {
310 return result;
311 }
312 // otherwise look for the options we have added
313 option = option.toLowerCase();
314 if (option.equals("-nodeprecatedlist") ||
315 option.equals("-noindex") ||
316 option.equals("-notree") ||
317 option.equals("-nohelp") ||
318 option.equals("-splitindex") ||
319 option.equals("-serialwarn") ||
320 option.equals("-use") ||
321 option.equals("-nonavbar") ||
322 option.equals("-nooverview") ||
323 option.equals("-xdoclint") ||
324 option.startsWith("-xdoclint:")) {
325 return 1;
326 } else if (option.equals("-help")) {
327 System.out.println(getText("doclet.usage"));
328 return 1;
329 } else if (option.equals("-footer") ||
330 option.equals("-header") ||
331 option.equals("-packagesheader") ||
332 option.equals("-doctitle") ||
333 option.equals("-windowtitle") ||
334 option.equals("-top") ||
335 option.equals("-bottom") ||
336 option.equals("-helpfile") ||
337 option.equals("-stylesheetfile") ||
338 option.equals("-charset") ||
339 option.equals("-overview") ||
340 option.equals("-xdocrootparent")) {
341 return 2;
342 } else {
343 return 0;
344 }
345 }
347 /**
348 * {@inheritDoc}
349 */
350 @Override
351 public boolean validOptions(String options[][],
352 DocErrorReporter reporter) {
353 boolean helpfile = false;
354 boolean nohelp = false;
355 boolean overview = false;
356 boolean nooverview = false;
357 boolean splitindex = false;
358 boolean noindex = false;
359 // check shared options
360 if (!generalValidOptions(options, reporter)) {
361 return false;
362 }
363 // otherwise look at our options
364 for (int oi = 0; oi < options.length; ++oi) {
365 String[] os = options[oi];
366 String opt = os[0].toLowerCase();
367 if (opt.equals("-helpfile")) {
368 if (nohelp == true) {
369 reporter.printError(getText("doclet.Option_conflict",
370 "-helpfile", "-nohelp"));
371 return false;
372 }
373 if (helpfile == true) {
374 reporter.printError(getText("doclet.Option_reuse",
375 "-helpfile"));
376 return false;
377 }
378 DocFile help = DocFile.createFileForInput(this, os[1]);
379 if (!help.exists()) {
380 reporter.printError(getText("doclet.File_not_found", os[1]));
381 return false;
382 }
383 helpfile = true;
384 } else if (opt.equals("-nohelp")) {
385 if (helpfile == true) {
386 reporter.printError(getText("doclet.Option_conflict",
387 "-nohelp", "-helpfile"));
388 return false;
389 }
390 nohelp = true;
391 } else if (opt.equals("-xdocrootparent")) {
392 try {
393 new URL(os[1]);
394 } catch (MalformedURLException e) {
395 reporter.printError(getText("doclet.MalformedURL", os[1]));
396 return false;
397 }
398 } else if (opt.equals("-overview")) {
399 if (nooverview == true) {
400 reporter.printError(getText("doclet.Option_conflict",
401 "-overview", "-nooverview"));
402 return false;
403 }
404 if (overview == true) {
405 reporter.printError(getText("doclet.Option_reuse",
406 "-overview"));
407 return false;
408 }
409 overview = true;
410 } else if (opt.equals("-nooverview")) {
411 if (overview == true) {
412 reporter.printError(getText("doclet.Option_conflict",
413 "-nooverview", "-overview"));
414 return false;
415 }
416 nooverview = true;
417 } else if (opt.equals("-splitindex")) {
418 if (noindex == true) {
419 reporter.printError(getText("doclet.Option_conflict",
420 "-splitindex", "-noindex"));
421 return false;
422 }
423 splitindex = true;
424 } else if (opt.equals("-noindex")) {
425 if (splitindex == true) {
426 reporter.printError(getText("doclet.Option_conflict",
427 "-noindex", "-splitindex"));
428 return false;
429 }
430 noindex = true;
431 } else if (opt.startsWith("-xdoclint:")) {
432 if (opt.contains("/")) {
433 reporter.printError(getText("doclet.Option_doclint_no_qualifiers"));
434 return false;
435 }
436 if (!DocLint.isValidOption(
437 opt.replace("-xdoclint:", DocLint.XMSGS_CUSTOM_PREFIX))) {
438 reporter.printError(getText("doclet.Option_doclint_invalid_arg"));
439 return false;
440 }
441 }
442 }
443 return true;
444 }
446 /**
447 * {@inheritDoc}
448 */
449 @Override
450 public MessageRetriever getDocletSpecificMsg() {
451 return standardmessage;
452 }
454 /**
455 * Decide the page which will appear first in the right-hand frame. It will
456 * be "overview-summary.html" if "-overview" option is used or no
457 * "-overview" but the number of packages is more than one. It will be
458 * "package-summary.html" of the respective package if there is only one
459 * package to document. It will be a class page(first in the sorted order),
460 * if only classes are provided on the command line.
461 *
462 * @param root Root of the program structure.
463 */
464 protected void setTopFile(RootDoc root) {
465 if (!checkForDeprecation(root)) {
466 return;
467 }
468 if (createoverview) {
469 topFile = DocPaths.OVERVIEW_SUMMARY;
470 } else {
471 if (packages.length == 1 && packages[0].name().equals("")) {
472 if (root.classes().length > 0) {
473 ClassDoc[] classarr = root.classes();
474 Arrays.sort(classarr);
475 ClassDoc cd = getValidClass(classarr);
476 topFile = DocPath.forClass(cd);
477 }
478 } else {
479 topFile = DocPath.forPackage(packages[0]).resolve(DocPaths.PACKAGE_SUMMARY);
480 }
481 }
482 }
484 protected ClassDoc getValidClass(ClassDoc[] classarr) {
485 if (!nodeprecated) {
486 return classarr[0];
487 }
488 for (int i = 0; i < classarr.length; i++) {
489 if (classarr[i].tags("deprecated").length == 0) {
490 return classarr[i];
491 }
492 }
493 return null;
494 }
496 protected boolean checkForDeprecation(RootDoc root) {
497 ClassDoc[] classarr = root.classes();
498 for (int i = 0; i < classarr.length; i++) {
499 if (isGeneratedDoc(classarr[i])) {
500 return true;
501 }
502 }
503 return false;
504 }
506 /**
507 * Generate "overview.html" page if option "-overview" is used or number of
508 * packages is more than one. Sets {@link #createoverview} field to true.
509 */
510 protected void setCreateOverview() {
511 if ((overview || packages.length > 1) && !nooverview) {
512 createoverview = true;
513 }
514 }
516 /**
517 * {@inheritDoc}
518 */
519 @Override
520 public WriterFactory getWriterFactory() {
521 return new WriterFactoryImpl(this);
522 }
524 /**
525 * {@inheritDoc}
526 */
527 @Override
528 public Comparator<ProgramElementDoc> getMemberComparator() {
529 return null;
530 }
532 /**
533 * {@inheritDoc}
534 */
535 @Override
536 public Locale getLocale() {
537 if (root instanceof RootDocImpl)
538 return ((RootDocImpl)root).getLocale();
539 else
540 return Locale.getDefault();
541 }
543 /**
544 * {@inheritDoc}
545 */
546 @Override
547 public JavaFileManager getFileManager() {
548 if (fileManager == null) {
549 if (root instanceof RootDocImpl)
550 fileManager = ((RootDocImpl) root).getFileManager();
551 else
552 fileManager = new JavacFileManager(new Context(), false, null);
553 }
554 return fileManager;
555 }
557 private JavaFileManager fileManager;
559 @Override
560 public boolean showMessage(SourcePosition pos, String key) {
561 if (root instanceof RootDocImpl) {
562 return pos == null || ((RootDocImpl) root).showTagMessages();
563 }
564 return true;
565 }
567 @Override
568 public Content getContentForResource() {
569 return new ContentBuilder();
570 }
571 }