src/share/classes/com/sun/tools/doclets/formats/html/ConfigurationImpl.java

Thu, 29 Aug 2013 11:41:20 -0700

author
jjg
date
Thu, 29 Aug 2013 11:41:20 -0700
changeset 1985
0e6577980181
parent 1797
019063968164
child 2169
667843bd2193
permissions
-rw-r--r--

8001669: javadoc internal DocletAbortException should set cause when appropriate
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             // Uugh: first, this should not be hidden inside optionLength,
   328             // and second, we should not be writing directly to stdout.
   329             // But we have no access to a DocErrorReporter, which would
   330             // allow use of reporter.printNotice
   331             System.out.println(getText("doclet.usage"));
   332             return 1;
   333         } else if (option.equals("-x")) {
   334             // Uugh: first, this should not be hidden inside optionLength,
   335             // and second, we should not be writing directly to stdout.
   336             // But we have no access to a DocErrorReporter, which would
   337             // allow use of reporter.printNotice
   338             System.out.println(getText("doclet.X.usage"));
   339             return 1;
   340         } else if (option.equals("-footer") ||
   341                    option.equals("-header") ||
   342                    option.equals("-packagesheader") ||
   343                    option.equals("-doctitle") ||
   344                    option.equals("-windowtitle") ||
   345                    option.equals("-top") ||
   346                    option.equals("-bottom") ||
   347                    option.equals("-helpfile") ||
   348                    option.equals("-stylesheetfile") ||
   349                    option.equals("-charset") ||
   350                    option.equals("-overview") ||
   351                    option.equals("-xdocrootparent")) {
   352             return 2;
   353         } else {
   354             return 0;
   355         }
   356     }
   358     /**
   359      * {@inheritDoc}
   360      */
   361     @Override
   362     public boolean validOptions(String options[][],
   363             DocErrorReporter reporter) {
   364         boolean helpfile = false;
   365         boolean nohelp = false;
   366         boolean overview = false;
   367         boolean nooverview = false;
   368         boolean splitindex = false;
   369         boolean noindex = false;
   370         // check shared options
   371         if (!generalValidOptions(options, reporter)) {
   372             return false;
   373         }
   374         // otherwise look at our options
   375         for (int oi = 0; oi < options.length; ++oi) {
   376             String[] os = options[oi];
   377             String opt = os[0].toLowerCase();
   378             if (opt.equals("-helpfile")) {
   379                 if (nohelp == true) {
   380                     reporter.printError(getText("doclet.Option_conflict",
   381                         "-helpfile", "-nohelp"));
   382                     return false;
   383                 }
   384                 if (helpfile == true) {
   385                     reporter.printError(getText("doclet.Option_reuse",
   386                         "-helpfile"));
   387                     return false;
   388                 }
   389                 DocFile help = DocFile.createFileForInput(this, os[1]);
   390                 if (!help.exists()) {
   391                     reporter.printError(getText("doclet.File_not_found", os[1]));
   392                     return false;
   393                 }
   394                 helpfile = true;
   395             } else  if (opt.equals("-nohelp")) {
   396                 if (helpfile == true) {
   397                     reporter.printError(getText("doclet.Option_conflict",
   398                         "-nohelp", "-helpfile"));
   399                     return false;
   400                 }
   401                 nohelp = true;
   402             } else if (opt.equals("-xdocrootparent")) {
   403                 try {
   404                     new URL(os[1]);
   405                 } catch (MalformedURLException e) {
   406                     reporter.printError(getText("doclet.MalformedURL", os[1]));
   407                     return false;
   408                 }
   409             } else if (opt.equals("-overview")) {
   410                 if (nooverview == true) {
   411                     reporter.printError(getText("doclet.Option_conflict",
   412                         "-overview", "-nooverview"));
   413                     return false;
   414                 }
   415                 if (overview == true) {
   416                     reporter.printError(getText("doclet.Option_reuse",
   417                         "-overview"));
   418                     return false;
   419                 }
   420                 overview = true;
   421             } else  if (opt.equals("-nooverview")) {
   422                 if (overview == true) {
   423                     reporter.printError(getText("doclet.Option_conflict",
   424                         "-nooverview", "-overview"));
   425                     return false;
   426                 }
   427                 nooverview = true;
   428             } else if (opt.equals("-splitindex")) {
   429                 if (noindex == true) {
   430                     reporter.printError(getText("doclet.Option_conflict",
   431                         "-splitindex", "-noindex"));
   432                     return false;
   433                 }
   434                 splitindex = true;
   435             } else if (opt.equals("-noindex")) {
   436                 if (splitindex == true) {
   437                     reporter.printError(getText("doclet.Option_conflict",
   438                         "-noindex", "-splitindex"));
   439                     return false;
   440                 }
   441                 noindex = true;
   442             } else if (opt.startsWith("-xdoclint:")) {
   443                 if (opt.contains("/")) {
   444                     reporter.printError(getText("doclet.Option_doclint_no_qualifiers"));
   445                     return false;
   446                 }
   447                 if (!DocLint.isValidOption(
   448                         opt.replace("-xdoclint:", DocLint.XMSGS_CUSTOM_PREFIX))) {
   449                     reporter.printError(getText("doclet.Option_doclint_invalid_arg"));
   450                     return false;
   451                 }
   452             }
   453         }
   454         return true;
   455     }
   457     /**
   458      * {@inheritDoc}
   459      */
   460     @Override
   461     public MessageRetriever getDocletSpecificMsg() {
   462         return standardmessage;
   463     }
   465     /**
   466      * Decide the page which will appear first in the right-hand frame. It will
   467      * be "overview-summary.html" if "-overview" option is used or no
   468      * "-overview" but the number of packages is more than one. It will be
   469      * "package-summary.html" of the respective package if there is only one
   470      * package to document. It will be a class page(first in the sorted order),
   471      * if only classes are provided on the command line.
   472      *
   473      * @param root Root of the program structure.
   474      */
   475     protected void setTopFile(RootDoc root) {
   476         if (!checkForDeprecation(root)) {
   477             return;
   478         }
   479         if (createoverview) {
   480             topFile = DocPaths.OVERVIEW_SUMMARY;
   481         } else {
   482             if (packages.length == 1 && packages[0].name().equals("")) {
   483                 if (root.classes().length > 0) {
   484                     ClassDoc[] classarr = root.classes();
   485                     Arrays.sort(classarr);
   486                     ClassDoc cd = getValidClass(classarr);
   487                     topFile = DocPath.forClass(cd);
   488                 }
   489             } else {
   490                 topFile = DocPath.forPackage(packages[0]).resolve(DocPaths.PACKAGE_SUMMARY);
   491             }
   492         }
   493     }
   495     protected ClassDoc getValidClass(ClassDoc[] classarr) {
   496         if (!nodeprecated) {
   497             return classarr[0];
   498         }
   499         for (int i = 0; i < classarr.length; i++) {
   500             if (classarr[i].tags("deprecated").length == 0) {
   501                 return classarr[i];
   502             }
   503         }
   504         return null;
   505     }
   507     protected boolean checkForDeprecation(RootDoc root) {
   508         ClassDoc[] classarr = root.classes();
   509         for (int i = 0; i < classarr.length; i++) {
   510             if (isGeneratedDoc(classarr[i])) {
   511                 return true;
   512             }
   513         }
   514         return false;
   515     }
   517     /**
   518      * Generate "overview.html" page if option "-overview" is used or number of
   519      * packages is more than one. Sets {@link #createoverview} field to true.
   520      */
   521     protected void setCreateOverview() {
   522         if ((overview || packages.length > 1) && !nooverview) {
   523             createoverview = true;
   524         }
   525     }
   527     /**
   528      * {@inheritDoc}
   529      */
   530     @Override
   531     public WriterFactory getWriterFactory() {
   532         return new WriterFactoryImpl(this);
   533     }
   535     /**
   536      * {@inheritDoc}
   537      */
   538     @Override
   539     public Comparator<ProgramElementDoc> getMemberComparator() {
   540         return null;
   541     }
   543     /**
   544      * {@inheritDoc}
   545      */
   546     @Override
   547     public Locale getLocale() {
   548         if (root instanceof RootDocImpl)
   549             return ((RootDocImpl)root).getLocale();
   550         else
   551             return Locale.getDefault();
   552     }
   554     /**
   555      * {@inheritDoc}
   556      */
   557     @Override
   558     public JavaFileManager getFileManager() {
   559         if (fileManager == null) {
   560             if (root instanceof RootDocImpl)
   561                 fileManager = ((RootDocImpl) root).getFileManager();
   562             else
   563                 fileManager = new JavacFileManager(new Context(), false, null);
   564         }
   565         return fileManager;
   566     }
   568     private JavaFileManager fileManager;
   570     @Override
   571     public boolean showMessage(SourcePosition pos, String key) {
   572         if (root instanceof RootDocImpl) {
   573             return pos == null || ((RootDocImpl) root).showTagMessages();
   574         }
   575         return true;
   576     }
   578     @Override
   579     public Content newContent() {
   580         return new ContentBuilder();
   581     }
   582 }

mercurial