langtools/src/share/classes/com/sun/tools/javadoc/Start.java
author jjg
Fri, 11 Jun 2010 17:24:23 -0700
changeset 5850 6f095ff5b469
parent 5520 86e4b9a9da40
child 7681 1f0819a3341f
permissions -rw-r--r--
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.main.CommandLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.ListBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.util.StringTokenizer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * Main program of Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * Previously named "Main".
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @since 1.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
class Start {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final String defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    56
    private final ClassLoader docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private static final String javadocName = "javadoc";
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private static final String standardDocletClassName =
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        "com.sun.tools.doclets.standard.Standard";
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private ListBuffer<String[]> options = new ListBuffer<String[]>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private ModifierFilter showAccess = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private long defaultFilter = PUBLIC | PROTECTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private Messager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    String docLocale = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    boolean breakiterator = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    boolean quiet = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    String encoding = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    private DocletInvoker docletInvoker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private static final int F_VERBOSE = 1 << 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    private static final int F_WARNINGS = 1 << 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /* Treat warnings as errors. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private boolean rejectWarnings = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    Start(String programName,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
          PrintWriter errWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
          PrintWriter warnWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
          PrintWriter noticeWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
          String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    90
        this(programName, errWriter, warnWriter, noticeWriter, defaultDocletClassName, null);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    91
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    92
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    93
    Start(String programName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    94
          PrintWriter errWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    95
          PrintWriter warnWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    96
          PrintWriter noticeWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    97
          String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    98
          ClassLoader docletParentClassLoader) {
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
    99
        Context tempContext = new Context(); // interim context until option decoding completed
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   100
        messager = new Messager(tempContext, programName, errWriter, warnWriter, noticeWriter);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   102
        this.docletParentClassLoader = docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    Start(String programName, String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   106
        this(programName, defaultDocletClassName, null);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   107
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   108
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   109
    Start(String programName, String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   110
          ClassLoader docletParentClassLoader) {
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   111
        Context tempContext = new Context(); // interim context until option decoding completed
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   112
        messager = new Messager(tempContext, programName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   114
        this.docletParentClassLoader = docletParentClassLoader;
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   115
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   116
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   117
    Start(String programName, ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   118
        this(programName, standardDocletClassName, docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    Start(String programName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        this(programName, standardDocletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   125
    Start(ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   126
        this(javadocName, docletParentClassLoader);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   127
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   128
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    Start() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        this(javadocName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     * Usage
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    private void usage() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        messager.notice("main.usage");
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        // let doclet print usage information (does nothing on error)
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        if (docletInvoker != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            docletInvoker.optionLength("-help");
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /**
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   146
     * Usage
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   147
     */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   148
    private void Xusage() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   149
        messager.notice("main.Xusage");
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   150
    }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   151
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   152
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * Exit
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    private void exit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        messager.exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * Main program - external wrapper
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
1363
d7fc53d64560 6748601: javadoc API should allow varargs use
jjg
parents: 10
diff changeset
   163
    int begin(String... argv) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        boolean failed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            failed = !parseAndExecute(argv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        } catch(Messager.ExitJavadoc exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            // ignore, we just exit this way
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        } catch (OutOfMemoryError ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            messager.error(null, "main.out.of.memory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        } catch (Error ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            ee.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            messager.error(null, "main.fatal.error");
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        } catch (Exception ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            ee.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            messager.error(null, "main.fatal.exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            messager.exitNotice();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            messager.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        failed |= messager.nerrors() > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        failed |= rejectWarnings && messager.nwarnings() > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        return failed ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    private void addToList(ListBuffer<String> list, String str){
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        StringTokenizer st = new StringTokenizer(str, ":");
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        String current;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        while(st.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            current = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            list.append(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * Main program - internal
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
1363
d7fc53d64560 6748601: javadoc API should allow varargs use
jjg
parents: 10
diff changeset
   202
    private boolean parseAndExecute(String... argv) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        long tm = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        ListBuffer<String> javaNames = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        // Preprocess @file arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            argv = CommandLine.parse(argv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        } catch (FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            messager.error(null, "main.cant.read", e.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            e.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        setDocletInvoker(argv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        ListBuffer<String> subPackages = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        ListBuffer<String> excludedPackages = new ListBuffer<String>();
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   221
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   222
        Context context = new Context();
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   223
        // Setup a new Messager, using the same initial parameters as the
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   224
        // existing Messager, except that this one will be able to use any
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   225
        // options that may be set up below.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   226
        Messager.preRegister(context,
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   227
                messager.programName,
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   228
                messager.errWriter, messager.warnWriter, messager.noticeWriter);
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   229
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        Options compOpts = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        boolean docClasses = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        // Parse arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            String arg = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            if (arg.equals("-subpackages")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                addToList(subPackages, argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            } else if (arg.equals("-exclude")){
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                addToList(excludedPackages, argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            } else if (arg.equals("-verbose")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                compOpts.put("-verbose", "");
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            } else if (arg.equals("-encoding")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                encoding = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                compOpts.put("-encoding", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            } else if (arg.equals("-breakiterator")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                breakiterator = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                setOption("-breakiterator");
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            } else if (arg.equals("-quiet")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                quiet = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                setOption("-quiet");
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            } else if (arg.equals("-help")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                usage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            } else if (arg.equals("-Xclasses")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                docClasses = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            } else if (arg.equals("-Xwerror")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                rejectWarnings = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            } else if (arg.equals("-private")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                setFilter(ModifierFilter.ALL_ACCESS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            } else if (arg.equals("-package")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                setFilter(PUBLIC | PROTECTED |
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                          ModifierFilter.PACKAGE );
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            } else if (arg.equals("-protected")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                setFilter(PUBLIC | PROTECTED );
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            } else if (arg.equals("-public")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                setOption(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                setFilter(PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            } else if (arg.equals("-source")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                if (compOpts.get("-source") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                compOpts.put("-source", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            } else if (arg.equals("-prompt")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                compOpts.put("-prompt", "-prompt");
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                messager.promptOnError = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            } else if (arg.equals("-sourcepath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                if (compOpts.get("-sourcepath") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                compOpts.put("-sourcepath", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            } else if (arg.equals("-classpath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                if (compOpts.get("-classpath") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                compOpts.put("-classpath", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            } else if (arg.equals("-sysclasspath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                if (compOpts.get("-bootclasspath") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                compOpts.put("-bootclasspath", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            } else if (arg.equals("-bootclasspath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                if (compOpts.get("-bootclasspath") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                compOpts.put("-bootclasspath", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            } else if (arg.equals("-extdirs")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                if (compOpts.get("-extdirs") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    usageError("main.option.already.seen", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                compOpts.put("-extdirs", argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            } else if (arg.equals("-overview")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            } else if (arg.equals("-doclet")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                i++;  // handled in setDocletInvoker
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            } else if (arg.equals("-docletpath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                i++;  // handled in setDocletInvoker
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            } else if (arg.equals("-locale")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                if (i != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                    usageError("main.locale_first");
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                docLocale = argv[i];
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   327
            } else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   328
                oneArg(argv, i++);
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   329
                if (compOpts.get(arg) != null) {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   330
                    usageError("main.option.already.seen", arg);
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   331
                }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   332
                compOpts.put(arg, argv[i]);
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   333
            } else if (arg.equals("-X")) {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   334
                Xusage();
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   335
                exit();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            } else if (arg.startsWith("-XD")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                String s = arg.substring("-XD".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                int eq = s.indexOf('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                String key = (eq < 0) ? s : s.substring(0, eq);
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                String value = (eq < 0) ? s : s.substring(eq+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                compOpts.put(key, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            // call doclet for its options
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            // other arg starts with - is invalid
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            else if ( arg.startsWith("-") ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                int optionLength;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                optionLength = docletInvoker.optionLength(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                if (optionLength < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                    // error already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                    exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                } else if (optionLength == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                    // option not found
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    usageError("main.invalid_flag", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    // doclet added option
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    if ((i + optionLength) > argv.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                        usageError("main.requires_argument", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                    ListBuffer<String> args = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                    for (int j = 0; j < optionLength-1; ++j) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                        args.append(argv[++i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                    setOption(arg, args.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                javaNames.append(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        if (javaNames.isEmpty() && subPackages.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            usageError("main.No_packages_or_classes_specified");
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        if (!docletInvoker.validOptions(options.toList())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            // error message already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        JavadocTool comp = JavadocTool.make0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        if (comp == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        if (showAccess == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            setFilter(defaultFilter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        LanguageVersion languageVersion = docletInvoker.languageVersion();
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        RootDocImpl root = comp.getRootDocImpl(
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                docLocale, encoding, showAccess,
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                javaNames.toList(), options.toList(), breakiterator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                subPackages.toList(), excludedPackages.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                docClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                // legacy?
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1, quiet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        // pass off control to the doclet
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        boolean ok = root != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        if (ok) ok = docletInvoker.start(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        // We're done.
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        if (compOpts.get("-verbose") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            tm = System.currentTimeMillis() - tm;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            messager.notice("main.done_in", Long.toString(tm));
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    private void setDocletInvoker(String[] argv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        String docletClassName = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        String docletPath = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        // Parse doclet specifying arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            String arg = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            if (arg.equals("-doclet")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                if (docletClassName != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                    usageError("main.more_than_one_doclet_specified_0_and_1",
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                               docletClassName, argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                docletClassName = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            } else if (arg.equals("-docletpath")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                if (docletPath == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                    docletPath = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                    docletPath += File.pathSeparator + argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        if (docletClassName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            docletClassName = defaultDocletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        // attempt to find doclet
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        docletInvoker = new DocletInvoker(messager,
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   438
                                          docletClassName, docletPath,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   439
                                          docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    private void setFilter(long filterBits) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        if (showAccess != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            messager.error(null, "main.incompatible.access.flags");
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            usage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        showAccess = new ModifierFilter(filterBits);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
     * Set one arg option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
     * Error and exit if one argument is not provided.
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    private void oneArg(String[] args, int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        if ((index + 1) < args.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            setOption(args[index], args[index+1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            usageError("main.requires_argument", args[index]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    private void usageError(String key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        messager.error(null, key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        usage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    private void usageError(String key, String a1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        messager.error(null, key, a1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        usage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
    private void usageError(String key, String a1, String a2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        messager.error(null, key, a1, a2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        usage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     * indicate an option with no arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    private void setOption(String opt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        String[] option = { opt };
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
     * indicate an option with one argument was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    private void setOption(String opt, String argument) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        String[] option = { opt, argument };
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     * indicate an option with the specified list of arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    private void setOption(String opt, List<String> arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        String[] args = new String[arguments.length() + 1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        int k = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        args[k++] = opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            args[k++] = i.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        options = options.append(args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
}