langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/ToolOption.java
changeset 37938 42baa89d2156
parent 36526 3b41f1c69604
child 38617 d93a7f64e231
equal deleted inserted replaced
37858:7c04fcb12bd4 37938:42baa89d2156
       
     1 /*
       
     2  * Copyright (c) 2012, 2016, 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 
       
    26 package com.sun.tools.javadoc.main;
       
    27 
       
    28 import java.util.LinkedHashMap;
       
    29 import java.util.Map;
       
    30 import java.util.StringTokenizer;
       
    31 
       
    32 import com.sun.tools.javac.code.Flags;
       
    33 import com.sun.tools.javac.main.Option;
       
    34 import com.sun.tools.javac.util.ListBuffer;
       
    35 import com.sun.tools.javac.util.Options;
       
    36 
       
    37 
       
    38 /**
       
    39  * javadoc tool options.
       
    40  *
       
    41  *  <p><b>This is NOT part of any supported API.
       
    42  *  If you write code that depends on this, you do so at your own risk.
       
    43  *  This code and its internal interfaces are subject to change or
       
    44  *  deletion without notice.</b>
       
    45  */
       
    46 public enum ToolOption {
       
    47     // ----- options for underlying compiler -----
       
    48 
       
    49     BOOTCLASSPATH("-bootclasspath", true) {
       
    50         @Override
       
    51         public void process(Helper helper, String arg) {
       
    52             helper.setFileManagerOpt(Option.BOOTCLASSPATH, arg);
       
    53         }
       
    54     },
       
    55 
       
    56     CLASSPATH("-classpath", true) {
       
    57         @Override
       
    58         public void process(Helper helper, String arg) {
       
    59             helper.setFileManagerOpt(Option.CLASSPATH, arg);
       
    60         }
       
    61     },
       
    62 
       
    63     CP("-cp", true) {
       
    64         @Override
       
    65         public void process(Helper helper, String arg) {
       
    66             helper.setFileManagerOpt(Option.CP, arg);
       
    67         }
       
    68     },
       
    69 
       
    70     EXTDIRS("-extdirs", true) {
       
    71         @Override
       
    72         public void process(Helper helper, String arg) {
       
    73             helper.setFileManagerOpt(Option.EXTDIRS, arg);
       
    74         }
       
    75     },
       
    76 
       
    77     SOURCEPATH("-sourcepath", true) {
       
    78         @Override
       
    79         public void process(Helper helper, String arg) {
       
    80             helper.setFileManagerOpt(Option.SOURCEPATH, arg);
       
    81         }
       
    82     },
       
    83 
       
    84     SYSCLASSPATH("-sysclasspath", true) {
       
    85         @Override
       
    86         public void process(Helper helper, String arg) {
       
    87             helper.setFileManagerOpt(Option.BOOTCLASSPATH, arg);
       
    88         }
       
    89     },
       
    90 
       
    91     MODULESOURCEPATH("-modulesourcepath", true) {
       
    92         @Override
       
    93         public void process(Helper helper, String arg) {
       
    94             helper.setFileManagerOpt(Option.MODULESOURCEPATH, arg);
       
    95         }
       
    96     },
       
    97 
       
    98     UPGRADEMODULEPATH("-upgrademodulepath", true) {
       
    99         @Override
       
   100         public void process(Helper helper, String arg) {
       
   101             helper.setFileManagerOpt(Option.UPGRADEMODULEPATH, arg);
       
   102         }
       
   103     },
       
   104 
       
   105     SYSTEM("-system", true) {
       
   106         @Override
       
   107         public void process(Helper helper, String arg) {
       
   108             helper.setFileManagerOpt(Option.SYSTEM, arg);
       
   109         }
       
   110     },
       
   111 
       
   112     MODULEPATH("-modulepath", true) {
       
   113         @Override
       
   114         public void process(Helper helper, String arg) {
       
   115             helper.setFileManagerOpt(Option.MODULEPATH, arg);
       
   116         }
       
   117     },
       
   118 
       
   119     ADDMODS("-addmods", true) {
       
   120         @Override
       
   121         public void process(Helper helper, String arg) {
       
   122             helper.setCompilerOpt(opt, arg);
       
   123         }
       
   124     },
       
   125 
       
   126     LIMITMODS("-limitmods", true) {
       
   127         @Override
       
   128         public void process(Helper helper, String arg) {
       
   129             helper.setCompilerOpt(opt, arg);
       
   130         }
       
   131     },
       
   132 
       
   133     ENCODING("-encoding", true) {
       
   134         @Override
       
   135         public void process(Helper helper, String arg) {
       
   136             helper.encoding = arg;
       
   137             helper.setCompilerOpt(opt, arg);
       
   138         }
       
   139     },
       
   140 
       
   141     RELEASE("-release", true) {
       
   142         @Override
       
   143         public void process(Helper helper, String arg) {
       
   144             helper.setCompilerOpt(opt, arg);
       
   145         }
       
   146     },
       
   147 
       
   148     SOURCE("-source", true) {
       
   149         @Override
       
   150         public void process(Helper helper, String arg) {
       
   151             helper.setCompilerOpt(opt, arg);
       
   152         }
       
   153     },
       
   154 
       
   155     XMAXERRS("-Xmaxerrs", true) {
       
   156         @Override
       
   157         public void process(Helper helper, String arg) {
       
   158             helper.setCompilerOpt(opt, arg);
       
   159         }
       
   160     },
       
   161 
       
   162     XMAXWARNS("-Xmaxwarns", true) {
       
   163         @Override
       
   164         public void process(Helper helper, String arg) {
       
   165             helper.setCompilerOpt(opt, arg);
       
   166         }
       
   167     },
       
   168 
       
   169     // ----- doclet options -----
       
   170 
       
   171     DOCLET("-doclet", true), // handled in setDocletInvoker
       
   172 
       
   173     DOCLETPATH("-docletpath", true), // handled in setDocletInvoker
       
   174 
       
   175     // ----- selection options -----
       
   176 
       
   177     SUBPACKAGES("-subpackages", true) {
       
   178         @Override
       
   179         public void process(Helper helper, String arg) {
       
   180             helper.addToList(helper.subPackages, arg);
       
   181         }
       
   182     },
       
   183 
       
   184     EXCLUDE("-exclude", true) {
       
   185         @Override
       
   186         public void process(Helper helper, String arg) {
       
   187             helper.addToList(helper.excludedPackages, arg);
       
   188         }
       
   189     },
       
   190 
       
   191     // ----- filtering options -----
       
   192 
       
   193     PACKAGE("-package") {
       
   194         @Override
       
   195         public void process(Helper helper) {
       
   196             helper.setFilter(
       
   197                     Flags.PUBLIC | Flags.PROTECTED | ModifierFilter.PACKAGE);
       
   198         }
       
   199     },
       
   200 
       
   201     PRIVATE("-private") {
       
   202         @Override
       
   203         public void process(Helper helper) {
       
   204             helper.setFilter(ModifierFilter.ALL_ACCESS);
       
   205         }
       
   206     },
       
   207 
       
   208     PROTECTED("-protected") {
       
   209         @Override
       
   210         public void process(Helper helper) {
       
   211             helper.setFilter(Flags.PUBLIC | Flags.PROTECTED);
       
   212         }
       
   213     },
       
   214 
       
   215     PUBLIC("-public") {
       
   216         @Override
       
   217         public void process(Helper helper) {
       
   218             helper.setFilter(Flags.PUBLIC);
       
   219         }
       
   220     },
       
   221 
       
   222     // ----- output control options -----
       
   223 
       
   224     PROMPT("-prompt") {
       
   225         @Override
       
   226         public void process(Helper helper) {
       
   227             helper.compOpts.put("-prompt", "-prompt");
       
   228             helper.promptOnError = true;
       
   229         }
       
   230     },
       
   231 
       
   232     QUIET("-quiet") {
       
   233         @Override
       
   234         public void process(Helper helper) {
       
   235             helper.quiet = true;
       
   236         }
       
   237     },
       
   238 
       
   239     VERBOSE("-verbose") {
       
   240         @Override
       
   241         public void process(Helper helper) {
       
   242             helper.compOpts.put("-verbose", "");
       
   243         }
       
   244     },
       
   245 
       
   246     XWERROR("-Xwerror") {
       
   247         @Override
       
   248         public void process(Helper helper) {
       
   249             helper.rejectWarnings = true;
       
   250 
       
   251         }
       
   252     },
       
   253 
       
   254     // ----- other options -----
       
   255 
       
   256     BREAKITERATOR("-breakiterator") {
       
   257         @Override
       
   258         public void process(Helper helper) {
       
   259             helper.breakiterator = true;
       
   260         }
       
   261     },
       
   262 
       
   263     LOCALE("-locale", true) {
       
   264         @Override
       
   265         public void process(Helper helper, String arg) {
       
   266             helper.docLocale = arg;
       
   267         }
       
   268     },
       
   269 
       
   270     OVERVIEW("-overview", true),
       
   271 
       
   272     XCLASSES("-Xclasses") {
       
   273         @Override
       
   274         public void process(Helper helper) {
       
   275             helper.docClasses = true;
       
   276 
       
   277         }
       
   278     },
       
   279 
       
   280     // ----- help options -----
       
   281 
       
   282     HELP("-help") {
       
   283         @Override
       
   284         public void process(Helper helper) {
       
   285             helper.usage();
       
   286         }
       
   287     },
       
   288 
       
   289     X("-X") {
       
   290         @Override
       
   291         public void process(Helper helper) {
       
   292             helper.Xusage();
       
   293         }
       
   294     };
       
   295 
       
   296     public final String opt;
       
   297     public final boolean hasArg;
       
   298 
       
   299     ToolOption(String opt) {
       
   300         this(opt, false);
       
   301     }
       
   302 
       
   303     ToolOption(String opt, boolean hasArg) {
       
   304         this.opt = opt;
       
   305         this.hasArg = hasArg;
       
   306     }
       
   307 
       
   308     void process(Helper helper, String arg) { }
       
   309 
       
   310     void process(Helper helper) { }
       
   311 
       
   312     static ToolOption get(String name) {
       
   313         for (ToolOption o: values()) {
       
   314             if (name.equals(o.opt))
       
   315                 return o;
       
   316         }
       
   317         return null;
       
   318     }
       
   319 
       
   320     static abstract class Helper {
       
   321         /** List of decoded options. */
       
   322         final ListBuffer<String[]> options = new ListBuffer<>();
       
   323 
       
   324         /** Selected packages, from -subpackages. */
       
   325         final ListBuffer<String> subPackages = new ListBuffer<>();
       
   326 
       
   327         /** Excluded packages, from -exclude. */
       
   328         final ListBuffer<String> excludedPackages = new ListBuffer<>();
       
   329 
       
   330         // File manager options
       
   331         final Map<Option, String> fileManagerOpts = new LinkedHashMap<>();
       
   332 
       
   333         /** javac options, set by various options. */
       
   334         Options compOpts; // = Options.instance(context)
       
   335 
       
   336         /* Encoding for javac, and files written? set by -encoding. */
       
   337         String encoding = null;
       
   338 
       
   339         /** Set by -breakiterator. */
       
   340         boolean breakiterator = false;
       
   341 
       
   342         /** Set by -quiet. */
       
   343         boolean quiet = false;
       
   344 
       
   345         /** Set by -Xclasses. */
       
   346         boolean docClasses = false;
       
   347 
       
   348         /** Set by -Xwerror. */
       
   349         boolean rejectWarnings = false;
       
   350 
       
   351         /** Set by -prompt. */
       
   352         boolean promptOnError;
       
   353 
       
   354         /** Set by -locale. */
       
   355         String docLocale = "";
       
   356 
       
   357         /** Set by -public, private, -protected, -package. */
       
   358         ModifierFilter showAccess = null;
       
   359 
       
   360         abstract void usage();
       
   361         abstract void Xusage();
       
   362 
       
   363         abstract void usageError(String msg, Object... args);
       
   364 
       
   365         void addToList(ListBuffer<String> list, String str){
       
   366             StringTokenizer st = new StringTokenizer(str, ":");
       
   367             String current;
       
   368             while(st.hasMoreTokens()){
       
   369                 current = st.nextToken();
       
   370                 list.append(current);
       
   371             }
       
   372         }
       
   373 
       
   374         void setFilter(long filterBits) {
       
   375             if (showAccess != null) {
       
   376                 usageError("main.incompatible.access.flags");
       
   377             }
       
   378             showAccess = new ModifierFilter(filterBits);
       
   379         }
       
   380 
       
   381         void setCompilerOpt(String opt, String arg) {
       
   382             if (compOpts.get(opt) != null) {
       
   383                 usageError("main.option.already.seen", opt);
       
   384             }
       
   385             compOpts.put(opt, arg);
       
   386         }
       
   387 
       
   388         void setFileManagerOpt(Option opt, String arg) {
       
   389             fileManagerOpts.put(opt, arg);
       
   390         }
       
   391     }
       
   392 }