1 package net.sourceforge.importscrubber; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.*; 6 7 10 public class ImportScrubber 11 { 12 public static final String START_DIRECTORY_KEY = "importscrubber.startDir"; 13 public static final String CLASS_DIRECTORY_KEY = "importscrubber.classDir"; 14 public static final String RECURSE = "importscrubber.recurse"; 15 public static final String SORT_STD_HIGH = "importscrubber.sortStdHigh"; 16 public static final String SYNC_CLASS_TO_SOURCE = "importscrubber.syncClassToSource"; 17 public static final String THRESHOLD = "importscrubber.threshold"; 18 public static final String THRESHOLD_STD_ONLY = "importscrubber.thresholdStdOnly"; 19 public static final String BREAK_STYLE = "importscrubber.breakStyle"; 20 21 public static final String FILE_SEPARATOR = System.getProperty("file.separator"); 22 public static final String LINE_SEPARATOR = System.getProperty("line.separator"); 23 public static boolean DEBUG = false; 24 private FileChooser _fileChooser; 25 private List _tasks = new ArrayList(); 26 private StatementFormat _format; 27 private String _encoding; 28 29 public ImportScrubber(String encoding) 30 { 31 _encoding = encoding; 32 } 33 34 public void setFileRoot(String fileName, String classRoot, boolean recurse) throws IOException 35 { 36 _fileChooser = new FileChooser(fileName, classRoot, recurse); 37 } 38 39 public static String getDirectory(String fileName) 40 { 41 File f = new File (fileName); 42 if (f.isDirectory()) { 43 return fileName; 44 } else { 45 return f.getParent(); 46 } 47 } 48 49 public void setFormat(StatementFormat format) 50 { 51 _format = format; 52 } 53 54 public void debugOff() 55 { 56 DEBUG = false; 57 } 58 59 public void debug() 60 { 61 DEBUG = true; 62 } 63 64 public int getTaskCount() 65 { 66 return _tasks.size(); 67 } 68 69 public Iterator getFilesIterator() 70 { 71 return _fileChooser; 72 } 73 74 public int buildTasks(Iterator iter) throws IOException 76 { 77 while (iter.hasNext()) { 78 FilePair pair = (FilePair)iter.next(); 79 _tasks.add(new ScrubTask(pair, _format, _encoding)); 80 } 81 82 return _tasks.size(); 83 } 84 85 public void runTasks(IProgressMonitor monitor) throws IOException 86 { 87 for (ListIterator iter = _tasks.listIterator(); iter.hasNext();) { 88 ScrubTask task = (ScrubTask)iter.next(); 89 monitor.taskStarted(task); 90 task.run(); 91 monitor.taskComplete(task); 92 } 93 _tasks.clear(); 94 } 95 96 public static void main(String [] args) 97 { 98 if (argExists("g",args)) { 99 new ImportScrubberGUI(); 100 return; 101 } 102 103 String encoding = null; 104 if (argExists("encoding", args)) { 105 encoding = findArg("encoding", args); 106 } else { 107 encoding = System.getProperty("file.encoding"); 108 } 109 110 if (!argExists("root", args)) { 111 usage(); 112 System.exit(0); 113 } 114 String root = findArg("root",args); 115 if (!(new File (root).exists())) { 116 System.out.println("Root: " + root + " does not exist"); 117 usage(); 118 System.exit(0); 119 } 120 boolean recurse = argExists("recurse", args); 121 122 StatementFormat format = StatementFormat.getFormat(args); 123 124 try { 125 ImportScrubber scrubber = new ImportScrubber(encoding); 126 127 if(argExists("classRoot", args) ) { 128 String classesRootStr= findArg( "classRoot", args ); 129 scrubber.setFileRoot(root, classesRootStr, recurse); 130 } else { 131 scrubber.setFileRoot(root, getDirectory(root), recurse); 132 } 133 134 scrubber.setFormat(format); 135 System.out.println("Building tasks"); 136 scrubber.buildTasks(scrubber.getFilesIterator()); 137 scrubber.runTasks(new ConsoleProgressMonitor()); 138 System.out.println(LINE_SEPARATOR + "All done!"); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 } 143 144 public static boolean argExists(String argFlag, String [] args) 145 { 146 for (int i=0; i<args.length; i++) { 147 if (args[i].equalsIgnoreCase("-"+argFlag)) { 148 return true; 149 } 150 } 151 return false; 152 } 153 154 public static String findArg(String argFlag, String [] args) 155 { 156 for (int i=0; i<args.length; i++) { 157 if (args[i].equalsIgnoreCase("-"+argFlag)) { 158 return args[i+1]; 159 } 160 } 161 throw new IllegalArgumentException ("Couldn't find " + argFlag); 162 } 163 164 private static void usage() 165 { 166 System.out.println("Usage: java net.sourceforge.importscrubber.ImportScrubber\n" 167 + "-g\n" 168 + "| -root <dir|file> [-classroot <dir>] [-recurse] [-encoding charsetname] [formatargs]\n" 169 + "formatargs:\n" 170 + StatementFormat.getUsage()); 171 System.out.println("Ex: java net.sourceforge.importscrubber.ImportScrubber -root /home/me/myproject/src -recurse"); 172 System.out.println("\nOR, TO USE THE GUI:\n"); 173 System.out.println("Ex: java net.sourceforge.importscrubber.ImportScrubber -g"); 174 } 175 } 176 | Popular Tags |