package dk.brics.xact.analysis;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import dk.brics.xact.analysis.config.StandardConfiguration;
import dk.brics.xact.compiler.SetupException;
/**
* Command-line interface to the program analysis for XACT.
*/
public class Main {
private Main() {}
/**
* Analyzes the given classes and reports warnings to standard output.
* The system property <tt>soot-classpath</tt> can be set to the class path
* for the classes to be analyzed.
*/
public static void main(String[] args) {
if (args.length == 0) {
printUsage();
return;
}
try {
BasicConfigurator.configure(); // initialize log4j for string analyzer
Logger.getRootLogger().setLevel(Level.ERROR);
String classpath = System.getProperty("soot-classpath");
StandardConfiguration config = new StandardConfiguration();
if (classpath == null)
classpath = "";
List<String> classes = new ArrayList<String>();
for (String arg : args) {
if (arg.startsWith("-dir=")) {
String dir = arg.substring("-dir=".length());
addClassFilesFromFolder(new File(dir), "", classes);
classpath += File.separator + dir;
}
else if (arg.startsWith("-basedir=")) {
config.setBasedir(arg.substring("-basedir=".length()));
}
else if (arg.startsWith("-cp=")) {
String cparg = arg.substring("-cp=".length());
classpath += File.separator + cparg;
}
else if (arg.startsWith("-")) {
throw new SetupException("Unrecognized argument " + arg);
}
else {
classes.add(arg);
}
}
if (classpath.startsWith(File.separator))
classpath = classpath.substring(1);
XMLAnalysis analysis = new XMLAnalysis(classpath, classes);
analysis.setConfiguration(config);
analysis.analyze();
analysis.printMessages();
} catch (SetupException ex) {
System.err.println(ex.getMessage());
printUsage();
}
}
public static void printUsage() {
System.err.println("Valid arguments are:");
System.err.println(" -dir=<path> Load all .class files from this directory.\n" +
" Also adds the directory to classpath.");
System.err.println(" -basedir=<path> Where to look for schema files.\n" +
" This argument is optional.");
System.err.println(" -cp=<path> Append this to classpath.");
System.err.println(" <classname> Load this particular class");
}
public static void addClassFilesFromFolder(File dir, String prefix, List<String> list) {
if (!dir.exists()) {
throw new SetupException(dir + " does not exist");
}
if (!dir.isDirectory()) {
throw new SetupException(dir + " is not a directory");
}
File[] files = dir.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".class") || pathname.isDirectory();
}
});
for (File file : files) {
if (file.isDirectory()) {
addClassFilesFromFolder(file, prefix + file.getName() + ".", list);
} else {
String classname = file.getName().substring(0, file.getName().length() - ".class".length());
list.add(prefix + classname);
}
}
}
}