Wed, 10 Oct 2012 16:48:21 -0700
8000665: fix "internal API" comments on javadoc files
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1997, 2012, 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.internal.toolkit;
28 import java.io.*;
29 import java.util.*;
31 import com.sun.javadoc.*;
32 import com.sun.tools.doclets.internal.toolkit.builders.BuilderFactory;
33 import com.sun.tools.doclets.internal.toolkit.taglets.*;
34 import com.sun.tools.doclets.internal.toolkit.util.*;
36 /**
37 * Configure the output based on the options. Doclets should sub-class
38 * Configuration, to configure and add their own options. This class contains
39 * all user options which are supported by the 1.1 doclet and the standard
40 * doclet.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 *
47 * @author Robert Field.
48 * @author Atul Dambalkar.
49 * @author Jamie Ho
50 */
51 public abstract class Configuration {
53 /**
54 * The factory for builders.
55 */
56 protected BuilderFactory builderFactory;
58 /**
59 * The taglet manager.
60 */
61 public TagletManager tagletManager;
63 /**
64 * The path to the builder XML input file.
65 */
66 public String builderXMLPath;
68 /**
69 * The default path to the builder XML.
70 */
71 private static final String DEFAULT_BUILDER_XML = "resources/doclet.xml";
73 /**
74 * The path to Taglets
75 */
76 public String tagletpath = "";
78 /**
79 * This is true if option "-serialwarn" is used. Defualt value is false to
80 * supress excessive warnings about serial tag.
81 */
82 public boolean serialwarn = false;
84 /**
85 * The specified amount of space between tab stops.
86 */
87 public int sourcetab = DocletConstants.DEFAULT_TAB_STOP_LENGTH;
89 /**
90 * True if we should generate browsable sources.
91 */
92 public boolean linksource = false;
94 /**
95 * True if command line option "-nosince" is used. Default value is
96 * false.
97 */
98 public boolean nosince = false;
100 /**
101 * True if we should recursively copy the doc-file subdirectories
102 */
103 public boolean copydocfilesubdirs = false;
105 /**
106 * The META charset tag used for cross-platform viewing.
107 */
108 public String charset = "";
110 /**
111 * True if user wants to add member names as meta keywords.
112 * Set to false because meta keywords are ignored in general
113 * by most Internet search engines.
114 */
115 public boolean keywords = false;
117 /**
118 * The meta tag keywords instance.
119 */
120 public final MetaKeywords metakeywords = new MetaKeywords(this);
122 /**
123 * The list of doc-file subdirectories to exclude
124 */
125 protected Set<String> excludedDocFileDirs;
127 /**
128 * The list of qualifiers to exclude
129 */
130 protected Set<String> excludedQualifiers;
132 /**
133 * The Root of the generated Program Structure from the Doclet API.
134 */
135 public RootDoc root;
137 /**
138 * Destination directory name, in which doclet will generate the entire
139 * documentation. Default is current directory.
140 */
141 public String destDirName = "";
143 /**
144 * Destination directory name, in which doclet will copy the doc-files to.
145 */
146 public String docFileDestDirName = "";
148 /**
149 * Encoding for this document. Default is default encoding for this
150 * platform.
151 */
152 public String docencoding = null;
154 /**
155 * True if user wants to suppress descriptions and tags.
156 */
157 public boolean nocomment = false;
159 /**
160 * Encoding for this document. Default is default encoding for this
161 * platform.
162 */
163 public String encoding = null;
165 /**
166 * Generate author specific information for all the classes if @author
167 * tag is used in the doc comment and if -author option is used.
168 * <code>showauthor</code> is set to true if -author option is used.
169 * Default is don't show author information.
170 */
171 public boolean showauthor = false;
173 /**
174 * Generate version specific information for the all the classes
175 * if @version tag is used in the doc comment and if -version option is
176 * used. <code>showversion</code> is set to true if -version option is
177 * used.Default is don't show version information.
178 */
179 public boolean showversion = false;
181 /**
182 * Sourcepath from where to read the source files. Default is classpath.
183 *
184 */
185 public String sourcepath = "";
187 /**
188 * Don't generate deprecated API information at all, if -nodeprecated
189 * option is used. <code>nodepracted</code> is set to true if
190 * -nodeprecated option is used. Default is generate deprected API
191 * information.
192 */
193 public boolean nodeprecated = false;
195 /**
196 * The catalog of classes specified on the command-line
197 */
198 public ClassDocCatalog classDocCatalog;
200 /**
201 * Message Retriever for the doclet, to retrieve message from the resource
202 * file for this Configuration, which is common for 1.1 and standard
203 * doclets.
204 *
205 * TODO: Make this private!!!
206 */
207 public MessageRetriever message = null;
209 /**
210 * True if user wants to suppress time stamp in output.
211 * Default is false.
212 */
213 public boolean notimestamp= false;
215 /**
216 * The package grouping instance.
217 */
218 public final Group group = new Group(this);
220 /**
221 * The tracker of external package links.
222 */
223 public final Extern extern = new Extern(this);
225 /**
226 * Return the build date for the doclet.
227 */
228 public abstract String getDocletSpecificBuildDate();
230 /**
231 * This method should be defined in all those doclets(configurations),
232 * which want to derive themselves from this Configuration. This method
233 * can be used to set its own command line options.
234 *
235 * @param options The array of option names and values.
236 * @throws DocletAbortException
237 */
238 public abstract void setSpecificDocletOptions(String[][] options);
240 /**
241 * Return the doclet specific {@link MessageRetriever}
242 * @return the doclet specific MessageRetriever.
243 */
244 public abstract MessageRetriever getDocletSpecificMsg();
246 /**
247 * An array of the packages specified on the command-line merged
248 * with the array of packages that contain the classes specified on the
249 * command-line. The array is sorted.
250 */
251 public PackageDoc[] packages;
253 /**
254 * Constructor. Constructs the message retriever with resource file.
255 */
256 public Configuration() {
257 message =
258 new MessageRetriever(this,
259 "com.sun.tools.doclets.internal.toolkit.resources.doclets");
260 excludedDocFileDirs = new HashSet<String>();
261 excludedQualifiers = new HashSet<String>();
262 }
264 /**
265 * Return the builder factory for this doclet.
266 *
267 * @return the builder factory for this doclet.
268 */
269 public BuilderFactory getBuilderFactory() {
270 if (builderFactory == null) {
271 builderFactory = new BuilderFactory(this);
272 }
273 return builderFactory;
274 }
276 /**
277 * This method should be defined in all those doclets
278 * which want to inherit from this Configuration. This method
279 * should return the number of arguments to the command line
280 * option (including the option name). For example,
281 * -notimestamp is a single-argument option, so this method would
282 * return 1.
283 *
284 * @param option Command line option under consideration.
285 * @return number of arguments to option (including the
286 * option name). Zero return means option not known.
287 * Negative value means error occurred.
288 */
289 public int optionLength(String option) {
290 option = option.toLowerCase();
291 if (option.equals("-author") ||
292 option.equals("-docfilessubdirs") ||
293 option.equals("-keywords") ||
294 option.equals("-linksource") ||
295 option.equals("-nocomment") ||
296 option.equals("-nodeprecated") ||
297 option.equals("-nosince") ||
298 option.equals("-notimestamp") ||
299 option.equals("-quiet") ||
300 option.equals("-xnodate") ||
301 option.equals("-version")) {
302 return 1;
303 } else if (option.equals("-d") ||
304 option.equals("-docencoding") ||
305 option.equals("-encoding") ||
306 option.equals("-excludedocfilessubdir") ||
307 option.equals("-link") ||
308 option.equals("-sourcetab") ||
309 option.equals("-noqualifier") ||
310 option.equals("-output") ||
311 option.equals("-sourcepath") ||
312 option.equals("-tag") ||
313 option.equals("-taglet") ||
314 option.equals("-tagletpath")) {
315 return 2;
316 } else if (option.equals("-group") ||
317 option.equals("-linkoffline")) {
318 return 3;
319 } else {
320 return -1; // indicate we don't know about it
321 }
322 }
324 /**
325 * Perform error checking on the given options.
326 *
327 * @param options the given options to check.
328 * @param reporter the reporter used to report errors.
329 */
330 public abstract boolean validOptions(String options[][],
331 DocErrorReporter reporter);
333 private void initPackageArray() {
334 Set<PackageDoc> set = new HashSet<PackageDoc>(Arrays.asList(root.specifiedPackages()));
335 ClassDoc[] classes = root.specifiedClasses();
336 for (int i = 0; i < classes.length; i++) {
337 set.add(classes[i].containingPackage());
338 }
339 ArrayList<PackageDoc> results = new ArrayList<PackageDoc>(set);
340 Collections.sort(results);
341 packages = results.toArray(new PackageDoc[] {});
342 }
344 /**
345 * Set the command line options supported by this configuration.
346 *
347 * @param options the two dimensional array of options.
348 */
349 public void setOptions(String[][] options) {
350 LinkedHashSet<String[]> customTagStrs = new LinkedHashSet<String[]>();
351 for (int oi = 0; oi < options.length; ++oi) {
352 String[] os = options[oi];
353 String opt = os[0].toLowerCase();
354 if (opt.equals("-d")) {
355 destDirName = addTrailingFileSep(os[1]);
356 docFileDestDirName = destDirName;
357 } else if (opt.equals("-docfilessubdirs")) {
358 copydocfilesubdirs = true;
359 } else if (opt.equals("-docencoding")) {
360 docencoding = os[1];
361 } else if (opt.equals("-encoding")) {
362 encoding = os[1];
363 } else if (opt.equals("-author")) {
364 showauthor = true;
365 } else if (opt.equals("-nosince")) {
366 nosince = true;
367 } else if (opt.equals("-version")) {
368 showversion = true;
369 } else if (opt.equals("-nodeprecated")) {
370 nodeprecated = true;
371 } else if (opt.equals("-sourcepath")) {
372 sourcepath = os[1];
373 } else if (opt.equals("-classpath") &&
374 sourcepath.length() == 0) {
375 sourcepath = os[1];
376 } else if (opt.equals("-excludedocfilessubdir")) {
377 addToSet(excludedDocFileDirs, os[1]);
378 } else if (opt.equals("-noqualifier")) {
379 addToSet(excludedQualifiers, os[1]);
380 } else if (opt.equals("-linksource")) {
381 linksource = true;
382 } else if (opt.equals("-sourcetab")) {
383 linksource = true;
384 try {
385 sourcetab = Integer.parseInt(os[1]);
386 } catch (NumberFormatException e) {
387 //Set to -1 so that warning will be printed
388 //to indicate what is valid argument.
389 sourcetab = -1;
390 }
391 if (sourcetab <= 0) {
392 message.warning("doclet.sourcetab_warning");
393 sourcetab = DocletConstants.DEFAULT_TAB_STOP_LENGTH;
394 }
395 } else if (opt.equals("-notimestamp")) {
396 notimestamp = true;
397 } else if (opt.equals("-nocomment")) {
398 nocomment = true;
399 } else if (opt.equals("-tag") || opt.equals("-taglet")) {
400 customTagStrs.add(os);
401 } else if (opt.equals("-tagletpath")) {
402 tagletpath = os[1];
403 } else if (opt.equals("-keywords")) {
404 keywords = true;
405 } else if (opt.equals("-serialwarn")) {
406 serialwarn = true;
407 } else if (opt.equals("-group")) {
408 group.checkPackageGroups(os[1], os[2]);
409 } else if (opt.equals("-link")) {
410 String url = os[1];
411 extern.url(url, url, root, false);
412 } else if (opt.equals("-linkoffline")) {
413 String url = os[1];
414 String pkglisturl = os[2];
415 extern.url(url, pkglisturl, root, true);
416 }
417 }
418 if (sourcepath.length() == 0) {
419 sourcepath = System.getProperty("env.class.path") == null ? "" :
420 System.getProperty("env.class.path");
421 }
422 if (docencoding == null) {
423 docencoding = encoding;
424 }
426 classDocCatalog = new ClassDocCatalog(root.specifiedClasses(), this);
427 initTagletManager(customTagStrs);
428 }
430 /**
431 * Set the command line options supported by this configuration.
432 *
433 * @throws DocletAbortException
434 */
435 public void setOptions() {
436 initPackageArray();
437 setOptions(root.options());
438 setSpecificDocletOptions(root.options());
439 }
442 /**
443 * Initialize the taglet manager. The strings to initialize the simple custom tags should
444 * be in the following format: "[tag name]:[location str]:[heading]".
445 * @param customTagStrs the set two dimentional arrays of strings. These arrays contain
446 * either -tag or -taglet arguments.
447 */
448 private void initTagletManager(Set<String[]> customTagStrs) {
449 tagletManager = tagletManager == null ?
450 new TagletManager(nosince, showversion, showauthor, message) :
451 tagletManager;
452 String[] args;
453 for (Iterator<String[]> it = customTagStrs.iterator(); it.hasNext(); ) {
454 args = it.next();
455 if (args[0].equals("-taglet")) {
456 tagletManager.addCustomTag(args[1], tagletpath);
457 continue;
458 }
459 String[] tokens = Util.tokenize(args[1],
460 TagletManager.SIMPLE_TAGLET_OPT_SEPERATOR, 3);
461 if (tokens.length == 1) {
462 String tagName = args[1];
463 if (tagletManager.isKnownCustomTag(tagName)) {
464 //reorder a standard tag
465 tagletManager.addNewSimpleCustomTag(tagName, null, "");
466 } else {
467 //Create a simple tag with the heading that has the same name as the tag.
468 StringBuffer heading = new StringBuffer(tagName + ":");
469 heading.setCharAt(0, Character.toUpperCase(tagName.charAt(0)));
470 tagletManager.addNewSimpleCustomTag(tagName, heading.toString(), "a");
471 }
472 } else if (tokens.length == 2) {
473 //Add simple taglet without heading, probably to excluding it in the output.
474 tagletManager.addNewSimpleCustomTag(tokens[0], tokens[1], "");
475 } else if (tokens.length >= 3) {
476 tagletManager.addNewSimpleCustomTag(tokens[0], tokens[2], tokens[1]);
477 } else {
478 message.error("doclet.Error_invalid_custom_tag_argument", args[1]);
479 }
480 }
481 }
483 private void addToSet(Set<String> s, String str){
484 StringTokenizer st = new StringTokenizer(str, ":");
485 String current;
486 while(st.hasMoreTokens()){
487 current = st.nextToken();
488 s.add(current);
489 }
490 }
492 /**
493 * Add a trailing file separator, if not found. Remove superfluous
494 * file separators if any. Preserve the front double file separator for
495 * UNC paths.
496 *
497 * @param path Path under consideration.
498 * @return String Properly constructed path string.
499 */
500 public static String addTrailingFileSep(String path) {
501 String fs = System.getProperty("file.separator");
502 String dblfs = fs + fs;
503 int indexDblfs;
504 while ((indexDblfs = path.indexOf(dblfs, 1)) >= 0) {
505 path = path.substring(0, indexDblfs) +
506 path.substring(indexDblfs + fs.length());
507 }
508 if (!path.endsWith(fs))
509 path += fs;
510 return path;
511 }
513 /**
514 * This checks for the validity of the options used by the user.
515 * This works exactly like
516 * {@link com.sun.javadoc.Doclet#validOptions(String[][],
517 * DocErrorReporter)}. This will validate the options which are shared
518 * by our doclets. For example, this method will flag an error using
519 * the DocErrorReporter if user has used "-nohelp" and "-helpfile" option
520 * together.
521 *
522 * @param options options used on the command line.
523 * @param reporter used to report errors.
524 * @return true if all the options are valid.
525 */
526 public boolean generalValidOptions(String options[][],
527 DocErrorReporter reporter) {
528 boolean docencodingfound = false;
529 String encoding = "";
530 for (int oi = 0; oi < options.length; oi++) {
531 String[] os = options[oi];
532 String opt = os[0].toLowerCase();
533 if (opt.equals("-d")) {
534 String destdirname = addTrailingFileSep(os[1]);
535 File destDir = new File(destdirname);
536 if (!destDir.exists()) {
537 //Create the output directory (in case it doesn't exist yet)
538 reporter.printNotice(getText("doclet.dest_dir_create",
539 destdirname));
540 (new File(destdirname)).mkdirs();
541 } else if (!destDir.isDirectory()) {
542 reporter.printError(getText(
543 "doclet.destination_directory_not_directory_0",
544 destDir.getPath()));
545 return false;
546 } else if (!destDir.canWrite()) {
547 reporter.printError(getText(
548 "doclet.destination_directory_not_writable_0",
549 destDir.getPath()));
550 return false;
551 }
552 } else if (opt.equals("-docencoding")) {
553 docencodingfound = true;
554 if (!checkOutputFileEncoding(os[1], reporter)) {
555 return false;
556 }
557 } else if (opt.equals("-encoding")) {
558 encoding = os[1];
559 }
560 }
561 if (!docencodingfound && encoding.length() > 0) {
562 if (!checkOutputFileEncoding(encoding, reporter)) {
563 return false;
564 }
565 }
566 return true;
567 }
569 /**
570 * Check the validity of the given Source or Output File encoding on this
571 * platform.
572 *
573 * @param docencoding output file encoding.
574 * @param reporter used to report errors.
575 */
576 private boolean checkOutputFileEncoding(String docencoding,
577 DocErrorReporter reporter) {
578 OutputStream ost= new ByteArrayOutputStream();
579 OutputStreamWriter osw = null;
580 try {
581 osw = new OutputStreamWriter(ost, docencoding);
582 } catch (UnsupportedEncodingException exc) {
583 reporter.printError(getText("doclet.Encoding_not_supported",
584 docencoding));
585 return false;
586 } finally {
587 try {
588 if (osw != null) {
589 osw.close();
590 }
591 } catch (IOException exc) {
592 }
593 }
594 return true;
595 }
597 /**
598 * Return true if the given doc-file subdirectory should be excluded and
599 * false otherwise.
600 * @param docfilesubdir the doc-files subdirectory to check.
601 */
602 public boolean shouldExcludeDocFileDir(String docfilesubdir){
603 if (excludedDocFileDirs.contains(docfilesubdir)) {
604 return true;
605 } else {
606 return false;
607 }
608 }
610 /**
611 * Return true if the given qualifier should be excluded and false otherwise.
612 * @param qualifier the qualifier to check.
613 */
614 public boolean shouldExcludeQualifier(String qualifier){
615 if (excludedQualifiers.contains("all") ||
616 excludedQualifiers.contains(qualifier) ||
617 excludedQualifiers.contains(qualifier + ".*")) {
618 return true;
619 } else {
620 int index = -1;
621 while ((index = qualifier.indexOf(".", index + 1)) != -1) {
622 if (excludedQualifiers.contains(qualifier.substring(0, index + 1) + "*")) {
623 return true;
624 }
625 }
626 return false;
627 }
628 }
630 /**
631 * Return the qualified name of the <code>ClassDoc</code> if it's qualifier is not excluded. Otherwise,
632 * return the unqualified <code>ClassDoc</code> name.
633 * @param cd the <code>ClassDoc</code> to check.
634 */
635 public String getClassName(ClassDoc cd) {
636 PackageDoc pd = cd.containingPackage();
637 if (pd != null && shouldExcludeQualifier(cd.containingPackage().name())) {
638 return cd.name();
639 } else {
640 return cd.qualifiedName();
641 }
642 }
644 public String getText(String key) {
645 try {
646 //Check the doclet specific properties file.
647 return getDocletSpecificMsg().getText(key);
648 } catch (Exception e) {
649 //Check the shared properties file.
650 return message.getText(key);
651 }
652 }
654 public String getText(String key, String a1) {
655 try {
656 //Check the doclet specific properties file.
657 return getDocletSpecificMsg().getText(key, a1);
658 } catch (Exception e) {
659 //Check the shared properties file.
660 return message.getText(key, a1);
661 }
662 }
664 public String getText(String key, String a1, String a2) {
665 try {
666 //Check the doclet specific properties file.
667 return getDocletSpecificMsg().getText(key, a1, a2);
668 } catch (Exception e) {
669 //Check the shared properties file.
670 return message.getText(key, a1, a2);
671 }
672 }
674 public String getText(String key, String a1, String a2, String a3) {
675 try {
676 //Check the doclet specific properties file.
677 return getDocletSpecificMsg().getText(key, a1, a2, a3);
678 } catch (Exception e) {
679 //Check the shared properties file.
680 return message.getText(key, a1, a2, a3);
681 }
682 }
684 /**
685 * Return true if the ClassDoc element is getting documented, depending upon
686 * -nodeprecated option and the deprecation information. Return true if
687 * -nodeprecated is not used. Return false if -nodeprecated is used and if
688 * either ClassDoc element is deprecated or the containing package is deprecated.
689 *
690 * @param cd the ClassDoc for which the page generation is checked
691 */
692 public boolean isGeneratedDoc(ClassDoc cd) {
693 if (!nodeprecated) {
694 return true;
695 }
696 return !(Util.isDeprecated(cd) || Util.isDeprecated(cd.containingPackage()));
697 }
699 /**
700 * Return the doclet specific instance of a writer factory.
701 * @return the {@link WriterFactory} for the doclet.
702 */
703 public abstract WriterFactory getWriterFactory();
705 /**
706 * Return the input stream to the builder XML.
707 *
708 * @return the input steam to the builder XML.
709 * @throws FileNotFoundException when the given XML file cannot be found.
710 */
711 public InputStream getBuilderXML() throws FileNotFoundException {
712 return builderXMLPath == null ?
713 Configuration.class.getResourceAsStream(DEFAULT_BUILDER_XML) :
714 new FileInputStream(new File(builderXMLPath));
715 }
717 /**
718 * Return the Locale for this document.
719 */
720 public abstract Locale getLocale();
722 /**
723 * Return the comparator that will be used to sort member documentation.
724 * To no do any sorting, return null.
725 *
726 * @return the {@link java.util.Comparator} used to sort members.
727 */
728 public abstract Comparator<ProgramElementDoc> getMemberComparator();
729 }