001: package net.sourceforge.importscrubber;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.*;
006:
007: /**
008: * This class encapsulates the import scrubber controller
009: */
010: public class ImportScrubber {
011: public static final String START_DIRECTORY_KEY = "importscrubber.startDir";
012: public static final String CLASS_DIRECTORY_KEY = "importscrubber.classDir";
013: public static final String RECURSE = "importscrubber.recurse";
014: public static final String SORT_STD_HIGH = "importscrubber.sortStdHigh";
015: public static final String SYNC_CLASS_TO_SOURCE = "importscrubber.syncClassToSource";
016: public static final String THRESHOLD = "importscrubber.threshold";
017: public static final String THRESHOLD_STD_ONLY = "importscrubber.thresholdStdOnly";
018: public static final String BREAK_STYLE = "importscrubber.breakStyle";
019:
020: public static final String FILE_SEPARATOR = System
021: .getProperty("file.separator");
022: public static final String LINE_SEPARATOR = System
023: .getProperty("line.separator");
024: public static boolean DEBUG = false;
025: private FileChooser _fileChooser;
026: private List _tasks = new ArrayList();
027: private StatementFormat _format;
028: private String _encoding;
029:
030: public ImportScrubber(String encoding) {
031: _encoding = encoding;
032: }
033:
034: public void setFileRoot(String fileName, String classRoot,
035: boolean recurse) throws IOException {
036: _fileChooser = new FileChooser(fileName, classRoot, recurse);
037: }
038:
039: public static String getDirectory(String fileName) {
040: File f = new File(fileName);
041: if (f.isDirectory()) {
042: return fileName;
043: } else {
044: return f.getParent();
045: }
046: }
047:
048: public void setFormat(StatementFormat format) {
049: _format = format;
050: }
051:
052: public void debugOff() {
053: DEBUG = false;
054: }
055:
056: public void debug() {
057: DEBUG = true;
058: }
059:
060: public int getTaskCount() {
061: return _tasks.size();
062: }
063:
064: public Iterator getFilesIterator() {
065: return _fileChooser;
066: }
067:
068: // Returns number of files to work on, allows getFiles to be called just once.
069: public int buildTasks(Iterator iter) throws IOException {
070: while (iter.hasNext()) {
071: FilePair pair = (FilePair) iter.next();
072: _tasks.add(new ScrubTask(pair, _format, _encoding));
073: }
074:
075: return _tasks.size();
076: }
077:
078: public void runTasks(IProgressMonitor monitor) throws IOException {
079: for (ListIterator iter = _tasks.listIterator(); iter.hasNext();) {
080: ScrubTask task = (ScrubTask) iter.next();
081: monitor.taskStarted(task);
082: task.run();
083: monitor.taskComplete(task);
084: }
085: _tasks.clear();
086: }
087:
088: public static void main(String[] args) {
089: if (argExists("g", args)) {
090: new ImportScrubberGUI();
091: return;
092: }
093:
094: String encoding = null;
095: if (argExists("encoding", args)) {
096: encoding = findArg("encoding", args);
097: } else {
098: encoding = System.getProperty("file.encoding");
099: }
100:
101: if (!argExists("root", args)) {
102: usage();
103: System.exit(0);
104: }
105: String root = findArg("root", args);
106: if (!(new File(root).exists())) {
107: System.out.println("Root: " + root + " does not exist");
108: usage();
109: System.exit(0);
110: }
111: boolean recurse = argExists("recurse", args);
112:
113: StatementFormat format = StatementFormat.getFormat(args);
114:
115: try {
116: ImportScrubber scrubber = new ImportScrubber(encoding);
117:
118: if (argExists("classRoot", args)) {
119: String classesRootStr = findArg("classRoot", args);
120: scrubber.setFileRoot(root, classesRootStr, recurse);
121: } else {
122: scrubber.setFileRoot(root, getDirectory(root), recurse);
123: }
124:
125: scrubber.setFormat(format);
126: System.out.println("Building tasks");
127: scrubber.buildTasks(scrubber.getFilesIterator());
128: scrubber.runTasks(new ConsoleProgressMonitor());
129: System.out.println(LINE_SEPARATOR + "All done!");
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133: }
134:
135: public static boolean argExists(String argFlag, String[] args) {
136: for (int i = 0; i < args.length; i++) {
137: if (args[i].equalsIgnoreCase("-" + argFlag)) {
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: public static String findArg(String argFlag, String[] args) {
145: for (int i = 0; i < args.length; i++) {
146: if (args[i].equalsIgnoreCase("-" + argFlag)) {
147: return args[i + 1];
148: }
149: }
150: throw new IllegalArgumentException("Couldn't find " + argFlag);
151: }
152:
153: private static void usage() {
154: System.out
155: .println("Usage: java net.sourceforge.importscrubber.ImportScrubber\n"
156: + "-g\n"
157: + "| -root <dir|file> [-classroot <dir>] [-recurse] [-encoding charsetname] [formatargs]\n"
158: + "formatargs:\n" + StatementFormat.getUsage());
159: System.out
160: .println("Ex: java net.sourceforge.importscrubber.ImportScrubber -root /home/me/myproject/src -recurse");
161: System.out.println("\nOR, TO USE THE GUI:\n");
162: System.out
163: .println("Ex: java net.sourceforge.importscrubber.ImportScrubber -g");
164: }
165: }
|