OptionHelper.java revision 3195:88a874f33d6d
170656Sobrien/*
270656Sobrien * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
370656Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
470656Sobrien *
570656Sobrien * This code is free software; you can redistribute it and/or modify it
670656Sobrien * under the terms of the GNU General Public License version 2 only, as
770656Sobrien * published by the Free Software Foundation.  Oracle designates this
870656Sobrien * particular file as subject to the "Classpath" exception as provided
970656Sobrien * by Oracle in the LICENSE file that accompanied this code.
1070656Sobrien *
1170656Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1270656Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1370656Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1470656Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1570656Sobrien * accompanied this code).
1670656Sobrien *
1770656Sobrien * You should have received a copy of the GNU General Public License version
1870656Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1970656Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2070656Sobrien *
2170656Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2270656Sobrien * or visit www.oracle.com if you need additional information or have any
2370656Sobrien * questions.
2470656Sobrien */
25216338Sdim
26216338Sdimpackage com.sun.tools.sjavac.options;
27216338Sdim
28216338Sdimimport java.nio.file.Path;
2970656Sobrienimport java.nio.file.Paths;
3070656Sobrienimport java.util.Arrays;
3170656Sobrienimport java.util.List;
32209869Snwhitehorn
33209869Snwhitehornimport com.sun.tools.javac.main.CommandLine;
3470656Sobrienimport com.sun.tools.sjavac.Transformer;
35218824Snwhitehorn
36209869Snwhitehorn/**
37218824Snwhitehorn * This class is used to decode sjavac options.
38209869Snwhitehorn * See com.sun.tools.sjavac.options.Options for example usage.
39209869Snwhitehorn *
40218824Snwhitehorn *  <p><b>This is NOT part of any supported API.
41209869Snwhitehorn *  If you write code that depends on this, you do so at your own risk.
4270656Sobrien *  This code and its internal interfaces are subject to change or
43209869Snwhitehorn *  deletion without notice.</b>
4470656Sobrien */
4570656Sobrienpublic abstract class OptionHelper {
4670656Sobrien
4770656Sobrien    /** Handle error */
48209869Snwhitehorn    public abstract void reportError(String msg);
49209869Snwhitehorn
5070656Sobrien    /** Record a package exclusion pattern */
51218824Snwhitehorn    public abstract void exclude(String excl);
52209869Snwhitehorn
53218824Snwhitehorn    /** Record a package inclusion pattern */
54209869Snwhitehorn    public abstract void include(String incl);
55209869Snwhitehorn
56218824Snwhitehorn    /** Record a root of sources to be compiled */
57209869Snwhitehorn    public abstract void sourceRoots(List<Path> path);
5870656Sobrien
59209869Snwhitehorn    /** Record a suffix + transformer */
60217399Skib    public abstract void addTransformer(String suffix, Transformer tr);
61217399Skib
62    /** Record a sourcepath to be used */
63    public abstract void sourcepath(List<Path> path);
64
65    /** Record a modulepath to be used */
66    public abstract void modulepath(List<Path> path);
67
68    /** Record a classpath to be used */
69    public abstract void classpath(List<Path> path);
70
71    /** Record the number of cores */
72    public abstract void numCores(int parseInt);
73
74    /** Record desired log level */
75    public abstract void logLevel(String level);
76
77    /** Record path for reference source list */
78    public abstract void compareFoundSources(Path referenceList);
79
80    /** Record a single permitted artifact */
81    public abstract void permitArtifact(String f);
82
83    /** Record the fact that unidentified artifacts are permitted */
84    public abstract void permitUnidentifiedArtifacts();
85
86    /** Record the fact that sources in the default package are permitted */
87    public abstract void permitDefaultPackage();
88
89    /** Record server configuration parameters */
90    public abstract void serverConf(String serverConf);
91
92    /** Record server launch configuration parameters */
93    public abstract void startServerConf(String serverConf);
94
95    /** Record some arguments to be passed on to javac */
96    public abstract void javacArg(String... arg);
97
98    /** Sets the destination directory for the compilation */
99    public abstract void destDir(Path dir);
100
101    /** Sets the directory for generated sources */
102    public abstract void generatedSourcesDir(Path genSrcDir);
103
104    /** Sets the directory for generated headers */
105    public abstract void headerDir(Path dir);
106
107    /** Sets the directory for state and log files generated by sjavac */
108    public abstract void stateDir(Path dir);
109
110    /** Sets the implicit policy */
111    public abstract void implicit(String policy);
112
113
114    /**
115     * Traverses an array of arguments and performs the appropriate callbacks.
116     *
117     * @param args the arguments to traverse.
118     */
119    void traverse(String[] args) {
120        try {
121            args = CommandLine.parse(args); // Detect @file and load it as a command line.
122        } catch (java.io.IOException e) {
123            throw new IllegalArgumentException("Problem reading @"+e.getMessage());
124        }
125        ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args));
126
127        nextArg:
128        while (argIter.hasNext()) {
129
130            String arg = argIter.next();
131
132            if (arg.startsWith("-")) {
133                for (Option opt : Option.values()) {
134                    if (opt.processCurrent(argIter, this))
135                        continue nextArg;
136                }
137
138                javacArg(arg);
139
140                // Does this javac argument take an argument? If so, don't
141                // let it pass on to sjavac as a source root directory.
142                for (com.sun.tools.javac.main.Option javacOpt : com.sun.tools.javac.main.Option.values()) {
143                    if (javacOpt.matches(arg)) {
144                        boolean takesArgument = javacOpt.hasArg();
145                        boolean separateToken = !arg.contains(":") && !arg.contains("=");
146                        if (takesArgument && separateToken)
147                            javacArg(argIter.next());
148                    }
149                }
150            } else {
151                sourceRoots(Arrays.asList(Paths.get(arg)));
152            }
153        }
154    }
155
156    public static String unescapeCmdArg(String arg) {
157        return arg.replaceAll("%20", " ")
158                  .replaceAll("%2C", ",");
159    }
160}
161