package com.sun.tools.internal.ws.wscompile;
import com.sun.tools.internal.ws.resources.WscompileMessages;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Options {
public boolean verbose;
public boolean quiet;
public boolean keep;
public File destDir = new File(".");
public File sourceDir;
public String classpath = System.getProperty("java.class.path");
public boolean nocompile;
public enum Target {
V2_0, V2_1;
public boolean isLaterThan(Target t) {
return this.ordinal() >= t.ordinal();
}
public static Target parse(String token) {
if (token.equals("2.0"))
return Target.V2_0;
else if (token.equals("2.1"))
return Target.V2_1;
return null;
}
public String getVersion(){
switch(this){
case V2_0:
return "2.0";
case V2_1:
return "2.1";
default:
return null;
}
}
}
public Target target = Target.V2_1;
public static final int STRICT = 1;
public static final int EXTENSION = 2;
public int compatibilityMode = STRICT;
public boolean isExtensionMode() {
return compatibilityMode == EXTENSION;
}
public File targetDir = new File(".");
public boolean debug = false;
public boolean debugMode = false;
private final List<File> generatedFiles = new ArrayList<File>();
private ClassLoader classLoader;
public void addGeneratedFile(File file) {
generatedFiles.add(file);
}
public void removeGeneratedFiles(){
for(File file : generatedFiles){
if (file.getName().endsWith(".java")) {
file.delete();
}
}
generatedFiles.clear();
}
public Iterable<File> getGeneratedFiles() {
return generatedFiles;
}
public void deleteGeneratedFiles() {
synchronized (generatedFiles) {
for (File file : generatedFiles) {
if (file.getName().endsWith(".java")) {
file.delete();
}
}
generatedFiles.clear();
}
}
public void parseArguments( String[] args ) throws BadCommandLineException {
for (int i = 0; i < args.length; i++) {
if(args[i].length()==0)
throw new BadCommandLineException();
if (args[i].charAt(0) == '-') {
int j = parseArguments(args,i);
if(j==0)
throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i]));
i += (j-1);
} else {
addFile(args[i]);
}
}
if(destDir == null)
destDir = new File(".");
if(sourceDir == null)
sourceDir = destDir;
}
protected void addFile(String arg) throws BadCommandLineException {}
protected int parseArguments(String[] args, int i) throws BadCommandLineException {
if (args[i].equals("-g")) {
debug = true;
return 1;
} else if (args[i].equals("-Xdebug")) {
debugMode = true;
return 1;
} else if (args[i].equals("-Xendorsed")) {
return 1;
} else if (args[i].equals("-verbose")) {
verbose = true;
return 1;
} else if (args[i].equals("-quiet")) {
quiet = true;
return 1;
} else if (args[i].equals("-keep")) {
keep = true;
return 1;
} else if (args[i].equals("-target")) {
String token = requireArgument("-target", args, ++i);
target = Target.parse(token);
if(target == null)
throw new BadCommandLineException(WscompileMessages.WSIMPORT_ILLEGAL_TARGET_VERSION(token));
return 2;
}else if (args[i].equals("-d")) {
destDir = new File(requireArgument("-d", args, ++i));
if (!destDir.exists())
throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(destDir.getPath()));
return 2;
} else if (args[i].equals("-s")) {
sourceDir = new File(requireArgument("-s", args, ++i));
keep = true;
if (!sourceDir.exists()) {
throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(sourceDir.getPath()));
}
return 2;
} else if (args[i].equals("-extension")) {
compatibilityMode = EXTENSION;
return 1;
} else if (args[i].startsWith("-help")) {
WeAreDone done = new WeAreDone();
done.initOptions(this);
throw done;
} else if (args[i].equals("-Xnocompile")) {
nocompile = true;
keep = true;
return 1;
}
return 0;
}
public String requireArgument(String optionName, String[] args, int i) throws BadCommandLineException {
if (args[i].startsWith("-")) {
throw new BadCommandLineException(WscompileMessages.WSCOMPILE_MISSING_OPTION_ARGUMENT(optionName));
}
return args[i];
}
public static final class WeAreDone extends BadCommandLineException {}
public ClassLoader getClassLoader() {
if (classLoader == null) {
classLoader =
new URLClassLoader(pathToURLs(classpath),
this.getClass().getClassLoader());
}
return classLoader;
}
public static URL[] pathToURLs(String path) {
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
URL[] urls = new URL[st.countTokens()];
int count = 0;
while (st.hasMoreTokens()) {
URL url = fileToURL(new File(st.nextToken()));
if (url != null) {
urls[count++] = url;
}
}
if (urls.length != count) {
URL[] tmp = new URL[count];
System.arraycopy(urls, 0, tmp, 0, count);
urls = tmp;
}
return urls;
}
public static URL fileToURL(File file) {
String name;
try {
name = file.getCanonicalPath();
} catch (IOException e) {
name = file.getAbsolutePath();
}
name = name.replace(File.separatorChar, '/');
if (!name.startsWith("/")) {
name = "/" + name;
}
if (!file.isFile()) {
name = name + "/";
}
try {
return new URL("file", "", name);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("file");
}
}
}