KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > CLIPParser


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier;
24
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import com.sun.enterprise.logging.LogDomains;
29
30 public class CLIPParser {
31
32 // NOTE: the current design does not allow LongOptions to have optional arguments.
33
// They should either have or not have arguments
34

35
36     /**
37      * helper property to get to the localized strings
38      */

39     protected static final com.sun.enterprise.util.LocalStringManagerImpl smh =
40             StringManagerHelper.getLocalStringsManager();
41
42     /**
43      * argument values for options that take an argument are stored here.
44      */

45     protected String JavaDoc optarg;
46
47
48     /**
49      * When an unrecognized option is encountered, getopt will return a '?'
50      * and store the value of the invalid option here.
51      */

52     protected int optopt = '?';
53
54     /**
55      * The next character to be scanned in the option-element
56      * in which the last option character we returned was found.
57      * This allows us to pick up the scan where we left last.
58      * If cn is a null string, advance to next argv
59      */

60     protected String JavaDoc cn;
61
62     /**
63      * This is the string describing the valid short options.
64      */

65     protected String JavaDoc optstring;
66
67     /**
68      * array of LongOption objects describing the valid long options.
69      */

70     protected LongOption[] longopt;
71
72     /**
73      * Stores the index into the longopt array of the long option detected
74      */

75     protected int longind;
76
77     /**
78      * A flag which communicates whether or not processLOpt() did all
79      * necessary processing for the current option
80      */

81     protected boolean processed;
82
83     /**
84      * The index of the first non-option in argv[]
85      */

86     protected int fno = 1;
87
88     /**
89      * The index of the last non-option in argv[]
90      */

91     protected int lno = 1;
92
93     /**
94      * Index in argv of the next element to be scanned.
95      * This is used for returning to and from the driver
96      * and for returning between successive calls to getopt.
97      * On entry to getopt zero means this is the first call.
98      * When getopt returns -1 this is the index of the first of the
99      * non-option elements that the caller should itself scan.
100      * Otherwise optind returns from one call to the next
101      * how much of argv has been parsed so far.
102      */

103     protected int optind = 0;
104
105     /**
106      * The arguments to be CLIP parsed
107      */

108     protected String JavaDoc[] argv;
109
110     /**
111      * The utility name.
112      */

113     protected String JavaDoc progname;
114     
115     Logger JavaDoc logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER);
116
117     /**
118      * flag when set to true will result in getopt returning -1 the next time it is called.
119      */

120     private boolean endparse = false;
121
122     /**
123      * @param progname The name to display as the program name when printing errors
124      * @param argv String array passed as the command line to the program
125      * @param optstring String containing a description of the valid short options for this program
126      * @param longopt array of LongOption objects that describes the valid long options for this program
127      */

128     public CLIPParser(String JavaDoc progname, String JavaDoc[] argv, String JavaDoc optstring,
129                       LongOption[] longopt) {
130         if (optstring.length() == 0)
131             optstring = " "; // NOI18N
132

133         this.progname = progname;
134         this.argv = argv;
135         this.optstring = optstring;
136         this.longopt = longopt;
137     }
138
139     /**
140      * validity checker for long options.
141      *
142      * @return the corresponding short option character or ?.
143      */

144     protected int processLOpt() {
145         LongOption currlopt = null;
146         boolean ambiguous;
147         boolean exact;
148         int eoname;
149
150         processed = true;
151         ambiguous = false;
152         exact = false;
153         longind = -1;
154
155         eoname = cn.indexOf("="); // NOI18N
156
if (eoname == -1)
157             eoname = cn.length();
158
159         for (int i = 0; i < longopt.length; i++) {
160
161             if (longopt[i].getName().startsWith(cn.substring(0, eoname))) {
162                 if (longopt[i].getName().equals(cn.substring(0, eoname))) {
163                     // Exact match found
164
currlopt = longopt[i];
165                     longind = i;
166                     exact = true;
167                     break;
168                 } else if (currlopt == null) {
169                     // First nonexact match found
170
currlopt = longopt[i];
171                     longind = i;
172                 } else {
173                     // Second or subsequent nonexact match found
174
ambiguous = true;
175                 }
176             }
177         }
178
179         if (ambiguous && !exact) {
180             logger.log(Level.SEVERE, getClass().getName() + ".ambig",
181                     new Object JavaDoc[]{progname, argv[optind]});
182             cn = "";
183             optopt = 0;
184             ++optind;
185
186             return '?';
187         }
188
189         if (currlopt != null) {
190             ++optind;
191
192             if (eoname != cn.length()) {
193                 if (currlopt.argPresent) {
194                     if (cn.substring(eoname).length() > 1)
195                         optarg = cn.substring(eoname + 1);
196                     else
197                         optarg = "";
198                 } else {
199                     if (argv[optind - 1].startsWith("--")) { // NOI18N
200
logger.log(Level.SEVERE, getClass().getName() +
201                                 ".loptnoarg", // NOI18N
202
new Object JavaDoc[]{progname, currlopt.name});
203                     } else {
204                         logger.log(Level.SEVERE,
205                                 getClass().getName() + ".optnoarg", // NOI18N
206
new Object JavaDoc[]{progname,
207                                              new Character JavaDoc(
208                                                      argv[optind - 1].charAt(0)).toString(),
209                                              currlopt.name});
210                     }
211
212                     cn = "";
213                     optopt = currlopt.value;
214
215                     return '?';
216                 }
217             } else if (currlopt.argPresent) {
218                 if (optind < argv.length) {
219                     optarg = argv[optind];
220                     ++optind;
221                 } else {
222                     logger.log(Level.SEVERE, getClass().getName() + ".reqargs",
223                             new Object JavaDoc[]{progname, argv[optind - 1]});
224
225                     cn = "";
226                     optopt = currlopt.value;
227                     if (optstring.charAt(0) == ':')
228                         return ':';
229                     else
230                         return '?';
231                 }
232             }
233
234             cn = "";
235
236             if (currlopt.holder != null) {
237                 currlopt.holder.setLength(0);
238                 currlopt.holder.append(currlopt.value);
239
240                 return (0);
241             }
242
243             return currlopt.value;
244         }
245
246         processed = false;
247
248         return (0);
249     }
250
251     /**
252      * @return The character representing the current option that has been
253      * parsed from the command line. If the option takes an argument,
254      * then the member optarg is set to contain the value of the
255      * argument. If an invalid option is found CLIP parser prints an
256      * error message and a ? character is returned. Member variable
257      * optopt will as usual store the option character. A -1 is returned
258      * when end of option parsing is detected. Upon return optind will
259      * be pointing to the first non-option argument in argv
260      */

261     public int getopt() {
262         optarg = null;
263
264         if (endparse == true)
265             return (-1);
266
267         if ((cn == null) || (cn.equals(""))) {
268             if (lno > optind)
269                 lno = optind;
270             if (fno > optind)
271                 fno = optind;
272
273             if ((optind != argv.length) && argv[optind].equals("--")) { // NOI18N
274
optind++;
275
276                 if (fno == lno)
277                     fno = optind;
278
279                 lno = argv.length;
280                 optind = argv.length;
281             }
282
283             if (optind == argv.length) {
284                 // Set the next arg index to point at the non-options
285
// that we previously skipped.
286
if (fno != lno)
287                     optind = fno;
288
289                 return -1;
290             }
291
292             // If we have come to a non-option stop the scan
293
if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') ||
294                     argv[optind].equals("-")) { // NOI18N
295
return -1;
296
297             }
298
299             // We have found another.
300
if (argv[optind].startsWith("--")) // NOI18N
301
cn = argv[optind].substring(2);
302             else
303                 cn = argv[optind].substring(1);
304         }
305
306         if ((longopt != null) && (argv[optind].startsWith("--"))) { // NOI18N
307
int c = processLOpt();
308
309             if (processed)
310                 return c;
311
312             // unrecognized long/short options
313
if (argv[optind].startsWith("--")) { // NOI18N
314
logger.log(Level.SEVERE, getClass().getName() + ".loptunknown",
315                         new Object JavaDoc[]{progname, argv[optind]});
316             } else {
317                 logger.log(Level.SEVERE, getClass().getName() + ".optunknown",
318                         new Object JavaDoc[]{progname,
319                                      new Character JavaDoc(argv[optind].charAt(0)).toString(),
320                                      cn});
321             }
322
323             cn = "";
324             ++optind;
325             optopt = 0;
326
327             return '?';
328         }
329
330         // handle the next short option-character
331
int c = cn.charAt(0);
332         if (cn.length() > 1)
333             cn = cn.substring(1);
334         else
335             cn = "";
336
337         String JavaDoc ct = null;
338         if (optstring.indexOf(c) != -1)
339             ct = optstring.substring(optstring.indexOf(c));
340
341         if (cn.equals(""))
342             ++optind;
343
344         if ((ct == null) || (c == ':')) {
345             /* if ( c != '?') {
346              log(smh.getLocalString(getClass().getName() + ".invalidopt",
347                      "{0}: invalid option [-{1} ].",
348                       new Object[] {progname, new Character((char)c).toString()}));
349              }*/

350
351             optopt = c;
352
353             return '?';
354         }
355
356         if ((ct.length() > 1) && (ct.charAt(1) == ':')) {
357             if ((ct.length() > 2) && (ct.charAt(2) == ':'))
358             // option that accepts an optional argument
359
{
360                 if (!cn.equals("")) {
361                     optarg = cn;
362                     ++optind;
363                 } else {
364                     optarg = null;
365                 }
366
367                 cn = null;
368             } else {
369                 if (!cn.equals("")) {
370                     optarg = cn;
371                     ++optind;
372                 } else if (optind == argv.length) {
373                     logger.log(Level.SEVERE, getClass().getName() + ".reqsarg",
374                             new Object JavaDoc[]{progname,
375                                          new Character JavaDoc((char) c).toString()});
376
377                     optopt = c;
378
379                     if (optstring.charAt(0) == ':')
380                         return ':';
381                     else
382                         return '?';
383                 } else {
384                     optarg = argv[optind];
385                     ++optind;
386                 }
387
388                 cn = null;
389             }
390         }
391
392         return c;
393     }
394
395     public int getOptind() {
396         return optind;
397     }
398
399
400     public String JavaDoc getOptarg() {
401         return optarg;
402     }
403
404     public int getOptopt() {
405         return optopt;
406     }
407
408     public int getLongind() {
409         return longind;
410     }
411
412 }
413
414
415
Popular Tags