KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > cmd > ArgHelperImpl


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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/cmd/ArgHelperImpl.java,v 1.3 2005/12/25 03:45:27 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:27 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.ListIterator JavaDoc;
35 import com.sun.cli.util.stringifier.SmartStringifier;
36
37 final class ParsedOption
38 {
39     final String JavaDoc mName;
40     final String JavaDoc [] mValues;
41     
42     ParsedOption( String JavaDoc name, String JavaDoc [] values )
43     {
44         assert( name.startsWith( "-" ) );
45         
46         mName = name;
47         mValues = values;
48     }
49 }
50
51
52 final class ParseState
53 {
54     final ListIterator JavaDoc mTokens;
55     final ArrayList JavaDoc mOptions;
56     boolean mDone;
57     
58     final ArgHelper.OptionsInfo mOptionInfo;
59     
60     ParseState(
61         final ListIterator JavaDoc tokens,
62         final ArgHelper.OptionsInfo optionInfo)
63     {
64         assert( tokens != null );
65         
66         mTokens = tokens;
67         mOptions = new ArrayList JavaDoc();
68         mDone = ! tokens.hasNext();
69         mOptionInfo = optionInfo;
70     }
71     
72         ParsedOption []
73     getParsedOptions()
74     {
75         assert( done() );
76         final ParsedOption [] options = new ParsedOption [ mOptions.size() ];
77         mOptions.toArray( options );
78         
79         return( options );
80     }
81     
82         String JavaDoc []
83     getOperands()
84     {
85         // put all the operands into one list
86
final ArrayList JavaDoc list = new ArrayList JavaDoc();
87         
88         while( mTokens.hasNext() )
89         {
90             list.add( mTokens.next() );
91         }
92         
93         String JavaDoc [] operands = new String JavaDoc [ list.size() ];
94         list.toArray( operands );
95         
96         return( operands );
97     }
98     
99         String JavaDoc
100     nextToken( )
101     {
102         return( (String JavaDoc)mTokens.next() );
103     }
104     
105         String JavaDoc
106     peekToken( )
107     {
108         final String JavaDoc result = (String JavaDoc)mTokens.next();
109         mTokens.previous();
110         
111         return( result );
112     }
113     
114         void
115     setDone()
116     {
117         mDone = true;
118     }
119     
120         boolean
121     done()
122     {
123         return( mDone || ! mTokens.hasNext() );
124     }
125 }
126     
127
128 public final class ArgHelperImpl implements ArgHelper
129 {
130     final ParsedOption [] mOptions;
131     final String JavaDoc [] mOperands;
132     
133     private final static String JavaDoc ARG_PREFIX = "--";
134     
135         private static void
136     dm( Object JavaDoc o )
137     {
138         System.out.println( SmartStringifier.toString( o ) );
139     }
140     
141
142         static void
143     numValuesFailure( final String JavaDoc optionName, final int numValues )
144         throws ArgHelper.IllegalOptionException
145     {
146         throw new ArgHelper.IllegalOptionException( "option \"" + optionName +
147             "\" requires " + numValues + " values" );
148     }
149     
150     
151         static void
152     booleanExpectedFailure( final String JavaDoc optionName )
153         throws ArgHelper.IllegalOptionException
154     {
155         throw new ArgHelper.IllegalOptionException( "option \"" + optionName +
156             "\" is a boolean option but has value(s)" );
157     }
158     
159     // values are comma-separated
160
static String JavaDoc []
161     extractValues( final String JavaDoc optionName, final String JavaDoc valueList, final int numValues )
162         throws ArgHelper.IllegalOptionException
163     {
164         // comma-separated list of args
165
// FIX: need to parse the line, not just split it
166
final String JavaDoc [] values = valueList.split( "," );
167         
168         if ( values.length != numValues )
169         {
170             numValuesFailure( optionName, numValues );
171         }
172
173         return( values );
174     }
175     
176     // values are in then tokens
177
static String JavaDoc []
178     extractValues( final ParseState ps, final String JavaDoc optionName, final int numValues )
179         throws ArgHelper.IllegalOptionException
180     {
181         final String JavaDoc [] values = new String JavaDoc [ numValues ];
182         
183         try
184         {
185             for( int valueIndex = 0; valueIndex < numValues; ++valueIndex )
186             {
187                 values[ valueIndex ] = ps.nextToken( );
188             }
189         }
190         catch( java.util.NoSuchElementException JavaDoc e )
191         {
192             numValuesFailure( optionName, numValues );
193         }
194         catch( ArrayIndexOutOfBoundsException JavaDoc e )
195         {
196             numValuesFailure( optionName, numValues );
197         }
198         
199         return( values );
200     }
201     
202         private static String JavaDoc
203     extractBoolean( final String JavaDoc optionName, final String JavaDoc value )
204         throws ArgHelper.IllegalOptionException
205     {
206         // presence of a boolean option implies true
207
String JavaDoc result = "true";
208         
209         if ( value != null )
210         {
211             // OK, check the value string is appropriate for a boolean
212
if ( value.equalsIgnoreCase( "true" ) ||
213                 value.equalsIgnoreCase( "false" ) )
214             {
215                 // OK
216
result = value;
217             }
218             else
219             {
220                 booleanExpectedFailure( optionName );
221             }
222             
223         }
224         return( result );
225     }
226     
227         static void
228     handleOption( final ParseState ps )
229         throws ArgHelper.IllegalOptionException
230     {
231         final String JavaDoc token = ps.nextToken();
232         final ArgHelper.OptionsInfo optionsInfo = ps.mOptionInfo;
233         
234         final String JavaDoc optionName = optionsInfo.tokenToOptionName( token );
235         final String JavaDoc optionExtra = optionsInfo.tokenToOptionData( token );
236         
237         final boolean isBooleanOption = optionsInfo.isBoolean( optionName );
238         final int numValues = optionsInfo.getNumValues( optionName );
239         
240         String JavaDoc [] values = null;
241         if ( isBooleanOption )
242         {
243             values = new String JavaDoc [] { extractBoolean( optionName, optionExtra ) };
244         }
245         else
246         {
247             if ( optionExtra != null )
248             {
249                 // comma-separated list of args
250
values = extractValues( optionName, optionExtra, numValues );
251             }
252             else
253             {
254                 // values are in token list
255
values = extractValues( ps, optionName, numValues );
256             }
257         }
258         
259         final String JavaDoc name = ArgHelperOptionsInfo.OptionDesc.mapName( optionName );
260         
261         ps.mOptions.add( new ParsedOption( name, values ) );
262     }
263     
264         void
265     handleNonOption( final ParseState ps )
266         throws ArgHelper.IllegalOptionException
267     {
268         final String JavaDoc token = ps.peekToken();
269         
270         if ( token.equals( ARG_PREFIX ) )
271         {
272             // "--" signifies the end of options
273
ps.setDone();
274         }
275         else if ( token.startsWith( "-" ) )
276         {
277             throw new ArgHelper.IllegalOptionException( "illegal option: " + token );
278         }
279         else
280         {
281             // it's our first operand
282
ps.setDone();
283         }
284     }
285     
286     
287         public
288     ArgHelperImpl(
289         final ListIterator JavaDoc tokens,
290         final ArgHelper.OptionsInfo optionInfo )
291             throws ArgHelper.IllegalOptionException
292     {
293         final ParseState ps = new ParseState( tokens, optionInfo );
294         
295         // put all the options into one list
296
while( ! ps.done() )
297         {
298             if ( optionInfo.isLegalOption( ps.peekToken() ) )
299             {
300                 handleOption( ps );
301             }
302             else
303             {
304                 handleNonOption( ps );
305             }
306         }
307         mOptions = ps.getParsedOptions();
308         
309         mOperands = ps.getOperands();
310         
311         //p( "options: " + SmartStringifier.DEFAULT.stringify( mOptions ) );
312
//p( "operands: " + SmartStringifier.DEFAULT.stringify( mOperands ) );
313
}
314     
315     
316     
317         private ParsedOption
318     findOption( final String JavaDoc name )
319     {
320         ParsedOption result = null;
321         
322         for( int i =0; i < mOptions.length; ++i )
323         {
324             if ( mOptions[ i ].mName.startsWith( name ) )
325             {
326                 result = mOptions[ i ];
327                 break;
328             }
329         }
330         return( result );
331     }
332     
333     
334     /*
335         Return the values associated with the option
336      */

337         public String JavaDoc []
338     getArgValues( final String JavaDoc name )
339          throws ArgHelper.IllegalOptionException
340     {
341         String JavaDoc [] results = null;
342         
343         String JavaDoc prefixedName = name;
344         // allow name to be either with or without leading "--" or "-"
345
// a single-letter name is assumed to be a single dash
346
if ( ! name.startsWith( "-" ) )
347         {
348             prefixedName = ArgHelperOptionsInfo.OptionDesc.mapName( name );
349         }
350         
351         
352         final ParsedOption option = findOption( prefixedName );
353         if ( option != null )
354         {
355             results = option.mValues;
356         }
357         
358         return( results );
359     }
360     
361         public String JavaDoc
362     getArgValue( final String JavaDoc name )
363         throws ArgHelper.IllegalOptionException
364     {
365         String JavaDoc value = null;
366         
367         final String JavaDoc [] values = getArgValues( name );
368         if ( values != null )
369         {
370             if ( values.length != 1 )
371             {
372                 throw new ArgHelper.IllegalOptionException( "option has more than one value: " + values.length );
373             }
374             
375             value = values[ 0 ];
376             assert( value != null );
377         }
378         
379         return( value );
380     }
381     
382     
383         public int
384     countOptions()
385     {
386         return( mOptions.length );
387     }
388     
389     
390         public String JavaDoc
391     getString( final String JavaDoc name, final String JavaDoc defaultValue)
392         throws ArgHelper.IllegalOptionException
393     {
394         String JavaDoc result = getArgValue( name );
395         if ( result == null )
396         {
397             result = defaultValue;
398         }
399         
400         return( result );
401     }
402     
403     
404         public Integer JavaDoc
405     getInteger( final String JavaDoc name )
406         throws ArgHelper.IllegalOptionException
407     {
408         final String JavaDoc value = getArgValue( name );
409         Integer JavaDoc result = null;
410         
411         if ( value != null )
412         {
413             result = new Integer JavaDoc( value );
414         }
415         
416         return( result );
417     }
418     
419         public Boolean JavaDoc
420     getBoolean( final String JavaDoc name, final Boolean JavaDoc defaultValue)
421         throws ArgHelper.IllegalOptionException
422     {
423         final String JavaDoc value = getArgValue( name );
424         
425         Boolean JavaDoc result = null;
426         
427         if ( value == null )
428         {
429             // not found
430
result = defaultValue;
431         }
432         else
433         {
434             // the --name=value form
435
result = new Boolean JavaDoc( value );
436         }
437         
438         return( result );
439     }
440     
441         public String JavaDoc []
442     getOperands( )
443     {
444         return( mOperands );
445     }
446 }
447
448
Popular Tags