KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > launcher > Option


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

17
18 package org.sablecc.sablecc.launcher;
19
20 import java.io.PushbackReader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.SortedMap JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 import org.sablecc.sablecc.exception.InternalException;
26 import org.sablecc.sablecc.launcher.syntax3.lexer.Lexer;
27 import org.sablecc.sablecc.launcher.syntax3.node.ALongOption;
28 import org.sablecc.sablecc.launcher.syntax3.node.ALongOptionArgument;
29 import org.sablecc.sablecc.launcher.syntax3.node.AShortOption;
30 import org.sablecc.sablecc.launcher.syntax3.node.AShortOptionsArgument;
31 import org.sablecc.sablecc.launcher.syntax3.node.Start;
32 import org.sablecc.sablecc.launcher.syntax3.parser.Parser;
33
34 public enum Option {
35
36     CHECK_ONLY("c", "check-only", null, "do not generate files"),
37     DESTINATION("d", "destination", "directory", "set destination directory"),
38     LENIENT("l", "lenient", null, "do not report non-fatal errors (implies -c)"),
39     QUIET("q", "quiet", null, "be as quiet as possible"),
40     VERBOSE("v", "verbose", null, "display as much information as possible"),
41     VERSION(null, "version", null, "display version information and exit"),
42     HELP("h", "help", null, "display help information and exit");
43
44     private String JavaDoc shortName;
45
46     private String JavaDoc longName;
47
48     private String JavaDoc operandName;
49
50     private String JavaDoc helpMessage;
51
52     private static final SortedMap JavaDoc<String JavaDoc, Option> shortNameMap = new TreeMap JavaDoc<String JavaDoc, Option>();
53
54     private static final SortedMap JavaDoc<String JavaDoc, Option> longNameMap = new TreeMap JavaDoc<String JavaDoc, Option>();
55
56     static {
57         for (Option option : Option.values()) {
58
59             if (option.shortName != null) {
60
61                 if (shortNameMap.containsKey(option.shortName)) {
62                     throw new InternalException("duplicate short name: "
63                             + option.shortName);
64                 }
65
66                 shortNameMap.put(option.shortName, option);
67             }
68
69             if (option.longName != null) {
70
71                 if (longNameMap.containsKey(option.longName)) {
72                     throw new InternalException("duplicate long name: "
73                             + option.longName);
74                 }
75
76                 longNameMap.put(option.longName, option);
77             }
78         }
79     }
80
81     private Option(
82             String JavaDoc shortName,
83             String JavaDoc longName,
84             String JavaDoc operandName,
85             String JavaDoc helpMessage) {
86
87         if (shortName == null && longName == null) {
88             throw new InternalException(
89                     "at least one of shortName and longName must not be null");
90         }
91
92         if (shortName != null) {
93             validateShortName(shortName);
94         }
95
96         if (longName != null) {
97
98             if (longName.length() < 2) {
99                 throw new InternalException(
100                         "longName must be at least two characters long");
101             }
102
103             validateLongName(longName);
104         }
105
106         if (helpMessage == null) {
107             throw new InternalException("helpMessage may not be null");
108         }
109
110         this.shortName = shortName;
111         this.longName = longName;
112         this.operandName = operandName;
113         this.helpMessage = helpMessage;
114     }
115
116     private void validateShortName(
117             String JavaDoc shortName) {
118
119         // validate by parsing "-name"
120
String JavaDoc argument = "-" + shortName;
121
122         try {
123             Start ast = new Parser(new Lexer(new PushbackReader JavaDoc(
124                     new StringReader JavaDoc(argument), 1024))).parse();
125
126             // argument should be short options
127
AShortOptionsArgument shortOptionsArgument = (AShortOptionsArgument) ast
128                     .getPArgument();
129
130             // single
131
if (shortOptionsArgument.getShortOptions().size() != 1) {
132                 throw new InternalException("invalid shortName");
133             }
134
135             AShortOption shortOption = (AShortOption) shortOptionsArgument
136                     .getShortOptions().getFirst();
137
138             // no operand
139
if (shortOption.getOperand() != null) {
140                 throw new InternalException("invalid shortName");
141             }
142         }
143         catch (Exception JavaDoc e) {
144             throw new InternalException("invalid shortName", e);
145         }
146     }
147
148     private void validateLongName(
149             String JavaDoc longName) {
150
151         // validate by parsing "--name"
152
String JavaDoc argument = "--" + longName;
153
154         try {
155             Start ast = new Parser(new Lexer(new PushbackReader JavaDoc(
156                     new StringReader JavaDoc(argument), 1024))).parse();
157
158             // argument should be long option
159
ALongOptionArgument longOptionArgument = (ALongOptionArgument) ast
160                     .getPArgument();
161
162             ALongOption longOption = (ALongOption) longOptionArgument
163                     .getLongOption();
164
165             // no operand
166
if (longOption.getOperand() != null) {
167                 throw new InternalException("invalid longName");
168             }
169         }
170         catch (Exception JavaDoc e) {
171             throw new InternalException("invalid longName", e);
172         }
173     }
174
175     public String JavaDoc getShortName() {
176
177         return this.shortName;
178     }
179
180     public String JavaDoc getLongName() {
181
182         return this.longName;
183     }
184
185     public String JavaDoc getOperandName() {
186
187         return this.operandName;
188     }
189
190     public String JavaDoc getHelpMessage() {
191
192         return this.helpMessage;
193     }
194
195     public boolean hasOperand() {
196
197         return this.operandName != null;
198     }
199
200     public static Option getShortOption(
201             String JavaDoc shortName) {
202
203         return shortNameMap.get(shortName);
204     }
205
206     public static Option getLongOption(
207             String JavaDoc longName) {
208
209         return longNameMap.get(longName);
210     }
211
212     public static String JavaDoc getShortHelpMessage() {
213
214         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
215
216         boolean first = true;
217
218         {
219             boolean hasShortOptions = false;
220             for (Option option : Option.values()) {
221                 if (option.shortName != null && option.operandName == null) {
222
223                     if (!hasShortOptions) {
224
225                         sb.append("[-");
226                         hasShortOptions = true;
227                     }
228
229                     sb.append(option.shortName);
230                 }
231             }
232
233             if (hasShortOptions) {
234                 sb.append("]");
235                 first = false;
236             }
237         }
238
239         for (Option option : Option.values()) {
240             if (option.shortName == null || option.operandName != null) {
241
242                 if (first) {
243                     first = false;
244                 }
245                 else {
246                     sb.append(" ");
247                 }
248
249                 if (option.shortName != null) {
250                     sb.append("[-");
251                     sb.append(option.shortName);
252                 }
253                 else {
254                     sb.append("[--");
255                     sb.append(option.longName);
256                 }
257
258                 if (option.operandName != null) {
259                     sb.append(" ");
260                     sb.append(option.operandName);
261                 }
262
263                 sb.append("]");
264             }
265         }
266
267         return sb.toString();
268     }
269
270     public static String JavaDoc getLongHelpMessage() {
271
272         int longestPrefixLength = 0;
273
274         for (Option option : Option.values()) {
275
276             int prefixLength = 0;
277
278             if (option.shortName != null) {
279                 prefixLength += 2 + option.shortName.length();
280
281                 if (option.operandName != null) {
282                     prefixLength += 1 + option.operandName.length();
283                 }
284             }
285
286             if (option.longName != null) {
287
288                 if (option.shortName != null) {
289                     prefixLength += 4;
290                 }
291                 else {
292                     prefixLength += 3;
293                 }
294
295                 prefixLength += option.longName.length();
296
297                 if (option.operandName != null) {
298                     prefixLength += 1 + option.operandName.length();
299                 }
300             }
301
302             if (prefixLength > longestPrefixLength) {
303                 longestPrefixLength = prefixLength;
304             }
305         }
306
307         String JavaDoc lineSeparator = System.getProperty("line.separator");
308
309         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
310         boolean first = true;
311
312         for (Option option : Option.values()) {
313
314             StringBuilder JavaDoc line = new StringBuilder JavaDoc();
315
316             if (option.shortName != null) {
317                 line.append(" -");
318                 line.append(option.shortName);
319
320                 if (option.operandName != null) {
321                     line.append(" ");
322                     line.append(option.operandName);
323                 }
324             }
325
326             if (option.longName != null) {
327
328                 if (option.shortName != null) {
329                     line.append(", --");
330                 }
331                 else {
332                     line.append(" --");
333                 }
334
335                 line.append(option.longName);
336
337                 if (option.operandName != null) {
338                     line.append("=");
339                     line.append(option.operandName);
340                 }
341             }
342
343             for (int i = line.toString().length(); i < longestPrefixLength; i++) {
344                 line.append(" ");
345             }
346
347             line.append(" : ");
348             line.append(option.helpMessage);
349
350             if (first) {
351                 first = false;
352             }
353             else {
354                 sb.append(lineSeparator);
355             }
356
357             sb.append(line);
358         }
359
360         return sb.toString();
361     }
362 }
363
Popular Tags