KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > importscrubber > ImportScrubber


1 package net.sourceforge.importscrubber;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.*;
6
7 /**
8  * This class encapsulates the import scrubber controller
9  */

10 public class ImportScrubber
11 {
12     public static final String JavaDoc START_DIRECTORY_KEY = "importscrubber.startDir";
13     public static final String JavaDoc CLASS_DIRECTORY_KEY = "importscrubber.classDir";
14     public static final String JavaDoc RECURSE = "importscrubber.recurse";
15     public static final String JavaDoc SORT_STD_HIGH = "importscrubber.sortStdHigh";
16     public static final String JavaDoc SYNC_CLASS_TO_SOURCE = "importscrubber.syncClassToSource";
17     public static final String JavaDoc THRESHOLD = "importscrubber.threshold";
18     public static final String JavaDoc THRESHOLD_STD_ONLY = "importscrubber.thresholdStdOnly";
19     public static final String JavaDoc BREAK_STYLE = "importscrubber.breakStyle";
20
21     public static final String JavaDoc FILE_SEPARATOR = System.getProperty("file.separator");
22     public static final String JavaDoc 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 JavaDoc _encoding;
28
29     public ImportScrubber(String JavaDoc encoding)
30     {
31         _encoding = encoding;
32     }
33
34     public void setFileRoot(String JavaDoc fileName, String JavaDoc classRoot, boolean recurse) throws IOException JavaDoc
35     {
36         _fileChooser = new FileChooser(fileName, classRoot, recurse);
37     }
38
39     public static String JavaDoc getDirectory(String JavaDoc fileName)
40     {
41         File JavaDoc f = new File JavaDoc(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     // Returns number of files to work on, allows getFiles to be called just once.
75
public int buildTasks(Iterator iter) throws IOException JavaDoc
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 JavaDoc
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 JavaDoc[] args)
97     {
98         if (argExists("g",args)) {
99             new ImportScrubberGUI();
100             return;
101         }
102
103         String JavaDoc 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 JavaDoc root = findArg("root",args);
115         if (!(new File JavaDoc(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 JavaDoc 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 JavaDoc e) {
140             e.printStackTrace();
141         }
142     }
143
144     public static boolean argExists(String JavaDoc argFlag, String JavaDoc[] 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 JavaDoc findArg(String JavaDoc argFlag, String JavaDoc[] 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 JavaDoc("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