1 /*
   2  * Copyright (c) 2012, 2014, 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;
  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     ENCODING("-encoding", true) {
  92         @Override
  93         public void process(Helper helper, String arg) {
  94             helper.encoding = arg;
  95             helper.setCompilerOpt(opt, arg);
  96         }
  97     },
  98 
  99     RELEASE("-release", true) {
 100         @Override
 101         public void process(Helper helper, String arg) {
 102             helper.setCompilerOpt(opt, arg);
 103         }
 104     },
 105 
 106     SOURCE("-source", true) {
 107         @Override
 108         public void process(Helper helper, String arg) {
 109             helper.setCompilerOpt(opt, arg);
 110         }
 111     },
 112 
 113     XMAXERRS("-Xmaxerrs", true) {
 114         @Override
 115         public void process(Helper helper, String arg) {
 116             helper.setCompilerOpt(opt, arg);
 117         }
 118     },
 119 
 120     XMAXWARNS("-Xmaxwarns", true) {
 121         @Override
 122         public void process(Helper helper, String arg) {
 123             helper.setCompilerOpt(opt, arg);
 124         }
 125     },
 126 
 127     // ----- doclet options -----
 128 
 129     DOCLET("-doclet", true), // handled in setDocletInvoker
 130 
 131     DOCLETPATH("-docletpath", true), // handled in setDocletInvoker
 132 
 133     // ----- selection options -----
 134 
 135     SUBPACKAGES("-subpackages", true) {
 136         @Override
 137         public void process(Helper helper, String arg) {
 138             helper.addToList(helper.subPackages, arg);
 139         }
 140     },
 141 
 142     EXCLUDE("-exclude", true) {
 143         @Override
 144         public void process(Helper helper, String arg) {
 145             helper.addToList(helper.excludedPackages, arg);
 146         }
 147     },
 148 
 149     // ----- filtering options -----
 150 
 151     PACKAGE("-package") {
 152         @Override
 153         public void process(Helper helper) {
 154             helper.setFilter(
 155                     Flags.PUBLIC | Flags.PROTECTED | ModifierFilter.PACKAGE);
 156         }
 157     },
 158 
 159     PRIVATE("-private") {
 160         @Override
 161         public void process(Helper helper) {
 162             helper.setFilter(ModifierFilter.ALL_ACCESS);
 163         }
 164     },
 165 
 166     PROTECTED("-protected") {
 167         @Override
 168         public void process(Helper helper) {
 169             helper.setFilter(Flags.PUBLIC | Flags.PROTECTED);
 170         }
 171     },
 172 
 173     PUBLIC("-public") {
 174         @Override
 175         public void process(Helper helper) {
 176             helper.setFilter(Flags.PUBLIC);
 177         }
 178     },
 179 
 180     // ----- output control options -----
 181 
 182     PROMPT("-prompt") {
 183         @Override
 184         public void process(Helper helper) {
 185             helper.compOpts.put("-prompt", "-prompt");
 186             helper.promptOnError = true;
 187         }
 188     },
 189 
 190     QUIET("-quiet") {
 191         @Override
 192         public void process(Helper helper) {
 193             helper.quiet = true;
 194         }
 195     },
 196 
 197     VERBOSE("-verbose") {
 198         @Override
 199         public void process(Helper helper) {
 200             helper.compOpts.put("-verbose", "");
 201         }
 202     },
 203 
 204     XWERROR("-Xwerror") {
 205         @Override
 206         public void process(Helper helper) {
 207             helper.rejectWarnings = true;
 208 
 209         }
 210     },
 211 
 212     // ----- other options -----
 213 
 214     BREAKITERATOR("-breakiterator") {
 215         @Override
 216         public void process(Helper helper) {
 217             helper.breakiterator = true;
 218         }
 219     },
 220 
 221     LOCALE("-locale", true) {
 222         @Override
 223         public void process(Helper helper, String arg) {
 224             helper.docLocale = arg;
 225         }
 226     },
 227 
 228     OVERVIEW("-overview", true),
 229 
 230     XCLASSES("-Xclasses") {
 231         @Override
 232         public void process(Helper helper) {
 233             helper.docClasses = true;
 234 
 235         }
 236     },
 237 
 238     // ----- help options -----
 239 
 240     HELP("-help") {
 241         @Override
 242         public void process(Helper helper) {
 243             helper.usage();
 244         }
 245     },
 246 
 247     X("-X") {
 248         @Override
 249         public void process(Helper helper) {
 250             helper.Xusage();
 251         }
 252     };
 253 
 254     public final String opt;
 255     public final boolean hasArg;
 256 
 257     ToolOption(String opt) {
 258         this(opt, false);
 259     }
 260 
 261     ToolOption(String opt, boolean hasArg) {
 262         this.opt = opt;
 263         this.hasArg = hasArg;
 264     }
 265 
 266     void process(Helper helper, String arg) { }
 267 
 268     void process(Helper helper) { }
 269 
 270     static ToolOption get(String name) {
 271         for (ToolOption o: values()) {
 272             if (name.equals(o.opt))
 273                 return o;
 274         }
 275         return null;
 276     }
 277 
 278     static abstract class Helper {
 279         /** List of decoded options. */
 280         final ListBuffer<String[]> options = new ListBuffer<>();
 281 
 282         /** Selected packages, from -subpackages. */
 283         final ListBuffer<String> subPackages = new ListBuffer<>();
 284 
 285         /** Excluded packages, from -exclude. */
 286         final ListBuffer<String> excludedPackages = new ListBuffer<>();
 287 
 288         // File manager options
 289         final Map<Option, String> fileManagerOpts = new LinkedHashMap<>();
 290 
 291         /** javac options, set by various options. */
 292         Options compOpts; // = Options.instance(context)
 293 
 294         /* Encoding for javac, and files written? set by -encoding. */
 295         String encoding = null;
 296 
 297         /** Set by -breakiterator. */
 298         boolean breakiterator = false;
 299 
 300         /** Set by -quiet. */
 301         boolean quiet = false;
 302 
 303         /** Set by -Xclasses. */
 304         boolean docClasses = false;
 305 
 306         /** Set by -Xwerror. */
 307         boolean rejectWarnings = false;
 308 
 309         /** Set by -prompt. */
 310         boolean promptOnError;
 311 
 312         /** Set by -locale. */
 313         String docLocale = "";
 314 
 315         /** Set by -public, private, -protected, -package. */
 316         ModifierFilter showAccess = null;
 317 
 318         abstract void usage();
 319         abstract void Xusage();
 320 
 321         abstract void usageError(String msg, Object... args);
 322 
 323         void addToList(ListBuffer<String> list, String str){
 324             StringTokenizer st = new StringTokenizer(str, ":");
 325             String current;
 326             while(st.hasMoreTokens()){
 327                 current = st.nextToken();
 328                 list.append(current);
 329             }
 330         }
 331 
 332         void setFilter(long filterBits) {
 333             if (showAccess != null) {
 334                 usageError("main.incompatible.access.flags");
 335             }
 336             showAccess = new ModifierFilter(filterBits);
 337         }
 338 
 339         void setCompilerOpt(String opt, String arg) {
 340             if (compOpts.get(opt) != null) {
 341                 usageError("main.option.already.seen", opt);
 342             }
 343             compOpts.put(opt, arg);
 344         }
 345 
 346         void setFileManagerOpt(Option opt, String arg) {
 347             fileManagerOpts.put(opt, arg);
 348         }
 349     }
 350 }