KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > util > tools > ToolBase


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.util.tools;
17
18 import com.google.gwt.dev.About;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.TreeMap JavaDoc;
29
30 /**
31  * A base class for a GWT related command-line application. To use this:
32  * <ol>
33  * <li>Derive a class for this class.</li>
34  * <li>In your constructor, call {@link #registerHandler(ArgHandler)}
35  * repeatedly to register particular command line arguments and options.</li>
36  * <li>Write a main that looks like this:
37  *
38  * <pre>
39  * public static void main(String[] args) {
40  * MyShell myShell = new MyShell();
41  * if (myShell.processArgs(args)) {
42  * // main program operation
43  * }
44  * System.exit(1);
45  * }
46  * </pre>
47  *
48  * </li>
49  * <li>Create launch config whose main class is MyShell.</li>
50  * </ol>
51  */

52 public abstract class ToolBase {
53
54   static {
55     String JavaDoc installPath = Utility.getInstallPath();
56     try {
57       // try to make absolute
58
installPath = new File JavaDoc(installPath).getCanonicalPath();
59     } catch (IOException JavaDoc e) {
60       // ignore problems, failures will occur when the libs try to load
61
}
62     System.setProperty("swt.library.path", installPath + '/');
63   }
64
65   // Use a tree map to sort the order.
66
//
67
private final Map JavaDoc argHandlers = new TreeMap JavaDoc();
68
69   // Use a list to preserve the declared order for help printing.
70
//
71
private final List JavaDoc orderedArgHandlers = new ArrayList JavaDoc();
72
73   protected void printHelp() {
74     System.err.println(About.GWT_VERSION);
75
76     ArgHandler nullHandler = null;
77     int widest = 0;
78     for (Iterator JavaDoc iter = orderedArgHandlers.iterator(); iter.hasNext();) {
79       ArgHandler handler = (ArgHandler) iter.next();
80       if (handler.isUndocumented()) {
81         continue;
82       }
83       String JavaDoc tag = handler.getTag();
84       if (tag != null) {
85         if (tag.length() > widest) {
86           widest = tag.length();
87         }
88       } else {
89         nullHandler = handler;
90         int len = nullHandler.getTagArgs()[0].length();
91         if (len > widest) {
92           widest = len;
93         }
94       }
95     }
96
97     // Print the name.
98
//
99
String JavaDoc name = getClass().getName();
100     int i = name.lastIndexOf('.');
101     if (i != -1) {
102       name = name.substring(i + 1);
103     }
104     System.err.print(name);
105
106     // Print the command-line template.
107
//
108
for (Iterator JavaDoc iter = orderedArgHandlers.iterator(); iter.hasNext();) {
109       ArgHandler handler = (ArgHandler) iter.next();
110       if (handler.isUndocumented()) {
111         continue;
112       }
113       String JavaDoc tag = handler.getTag();
114       if (tag != null) {
115         System.err.print(handler.isRequired() ? " " : " [");
116         System.err.print(tag);
117         String JavaDoc[] tagArgs = handler.getTagArgs();
118         for (int j = 0; j < tagArgs.length; j++) {
119           if (handler.isRequired()) {
120             System.err.print(" " + tagArgs[j]);
121           } else {
122             System.err.print(" " + tagArgs[j]);
123           }
124         }
125         System.err.print(handler.isRequired() ? "" : "]");
126       }
127     }
128
129     // Print the flagless args.
130
//
131
if (nullHandler != null && !nullHandler.isUndocumented()) {
132       String JavaDoc[] tagArgs = nullHandler.getTagArgs();
133       for (int j = 0; j < tagArgs.length; j++) {
134         System.err.print(nullHandler.isRequired() ? " " : " [");
135         System.err.print(tagArgs[j]);
136         System.err.print(nullHandler.isRequired() ? " " : "]");
137       }
138       System.err.println();
139     }
140
141     System.err.println();
142
143     System.err.println("where ");
144
145     // Print the details.
146
//
147
for (Iterator JavaDoc iter = orderedArgHandlers.iterator(); iter.hasNext();) {
148       ArgHandler handler = (ArgHandler) iter.next();
149       if (handler.isUndocumented()) {
150         continue;
151       }
152       String JavaDoc tag = handler.getTag();
153       if (tag != null) {
154         int len = tag.length();
155         System.err.print(" ");
156         System.err.print(tag);
157         for (i = len; i < widest; ++i) {
158           System.err.print(' ');
159         }
160         System.err.print(" ");
161         System.err.print(handler.getPurpose());
162         System.err.println();
163       }
164     }
165
166     // And details for the "extra" args, if any.
167
//
168
if (nullHandler != null && !nullHandler.isUndocumented()) {
169       System.err.println("and ");
170       String JavaDoc tagArg = nullHandler.getTagArgs()[0];
171       int len = tagArg.length();
172       System.err.print(" ");
173       System.err.print(tagArg);
174       for (i = len; i < widest; ++i) {
175         System.err.print(' ');
176       }
177       System.err.print(" ");
178       System.err.print(nullHandler.getPurpose());
179       System.err.println();
180     }
181   }
182
183   protected boolean processArgs(String JavaDoc[] args) {
184     if (args.length > 0) {
185       boolean help = false;
186       if ("-help".equalsIgnoreCase(args[0])) {
187         help = true;
188       } else if ("-?".equals(args[0])) {
189         help = true;
190       }
191
192       if (help) {
193         printHelp();
194         return false;
195       }
196     }
197
198     Set JavaDoc defs = new HashSet JavaDoc(argHandlers.values());
199     int extraArgCount = 0;
200
201     Set JavaDoc receivedArg = new HashSet JavaDoc();
202
203     // Let the args drive the handlers.
204
//
205
ArgHandler nullHandler = (ArgHandler) argHandlers.get("");
206     for (int i = 0; i < args.length; i++) {
207       String JavaDoc arg = args[i];
208       ArgHandler handler;
209       if (arg.startsWith("-")) {
210         // Use the handler registered for this flag.
211
//
212
handler = (ArgHandler) argHandlers.get(arg);
213       } else {
214         // Use the handler that doesn't have a leading flag.
215
//
216
handler = nullHandler;
217         ++extraArgCount;
218       }
219
220       if (handler == null) {
221         System.err.println("Unknown argument: " + arg);
222         printHelp();
223         return false;
224       }
225
226       int addtlConsumed = handler.handle(args, i);
227       if (addtlConsumed == -1) {
228         printHelp();
229         return false;
230       }
231
232       i += addtlConsumed;
233
234       // We don't need to use this as a default handler.
235
//
236
defs.remove(handler);
237
238       // Record that this handler saw a value
239
//
240
receivedArg.add(handler);
241     }
242
243     // See if any handler didn't get its required argument(s).
244
//
245
for (Iterator JavaDoc iter = argHandlers.values().iterator(); iter.hasNext();) {
246       ArgHandler argHandler = (ArgHandler) iter.next();
247       if (argHandler.isRequired() && !receivedArg.contains(argHandler)) {
248         System.err.print("Missing required argument '");
249         String JavaDoc tag = argHandler.getTag();
250         if (tag != null) {
251           System.err.print(tag);
252           System.err.print(" ");
253         }
254
255         String JavaDoc tagArg = argHandler.getTagArgs()[0];
256         System.err.print(tagArg);
257         System.err.println("'");
258
259         printHelp();
260         return false;
261       }
262     }
263     if (extraArgCount == 0 && nullHandler != null && nullHandler.isRequired()) {
264       System.err.print("Missing required argument '");
265       String JavaDoc tagArg = nullHandler.getTagArgs()[0];
266       System.err.print(tagArg);
267       System.err.println("'");
268       printHelp();
269       return false;
270     }
271
272     // Set if there are any remaining unused handlers with default arguments.
273
// Allow the default handlers to pretend there were other arguments.
274
//
275
for (Iterator JavaDoc iter = defs.iterator(); iter.hasNext();) {
276       ArgHandler def = (ArgHandler) iter.next();
277       String JavaDoc[] defArgs = def.getDefaultArgs();
278       if (defArgs != null) {
279         if (def.handle(defArgs, 0) == -1) {
280           return false;
281         }
282       }
283     }
284
285     return true;
286   }
287
288   protected void registerHandler(ArgHandler handler) {
289     String JavaDoc tag = handler.getTag();
290     orderedArgHandlers.add(handler);
291     argHandlers.put(tag != null ? tag : "", handler);
292   }
293 }
294
Popular Tags