KickJava   Java API By Example, From Geeks To Geeks.

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


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/ArgHelperOptionsInfo.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 package com.sun.cli.jmx.cmd;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33  
34
35
36 public class ArgHelperOptionsInfo implements ArgHelper.OptionsInfo
37 {
38     final HashMap JavaDoc mOptionDescriptions;
39     
40         public
41     ArgHelperOptionsInfo( )
42         throws ArgHelper.IllegalOptionException
43     {
44         mOptionDescriptions = new HashMap JavaDoc();
45     }
46     
47         public
48     ArgHelperOptionsInfo( String JavaDoc options )
49         throws ArgHelper.IllegalOptionException
50     {
51         mOptionDescriptions = new HashMap JavaDoc();
52         
53         addOptions( options );
54     }
55     
56         public String JavaDoc
57     tokenToOptionName( String JavaDoc token )
58     {
59         final int delimIndex = token.indexOf( '=' );
60         
61         String JavaDoc name = token;
62         
63         if ( delimIndex > 0 )
64         {
65             name = token.substring( 0, delimIndex );
66         }
67         
68         return( name );
69     }
70     
71         public String JavaDoc
72     tokenToOptionData( String JavaDoc token )
73     {
74         final int delimIndex = token.indexOf( '=' );
75         
76         String JavaDoc data = null; // return null if no data eg "--foo"
77

78         if ( delimIndex > 0 )
79         {
80             // note: form "--foo=" is valid and should result in an empty string
81
data = token.substring( delimIndex + 1, token.length() );
82         }
83         
84         return( data );
85     }
86     
87         private static void
88     dm( Object JavaDoc o )
89     {
90         System.out.println( o.toString() );
91     }
92     
93         OptionDesc
94     findOptionDesc( String JavaDoc token )
95     {
96         final String JavaDoc optionName = tokenToOptionName( OptionDesc.mapName( token ) );
97         
98         return( (OptionDesc)mOptionDescriptions.get( optionName ) );
99     }
100     
101         public boolean
102     isLegalOption( String JavaDoc token )
103     {
104         if ( ! token.startsWith( "-" ) )
105         {
106             return( false );
107         }
108
109         final boolean isLegal = (findOptionDesc( token ) != null);
110         
111         return( isLegal );
112     }
113     
114         public void
115     checkLegalOption( String JavaDoc token )
116     {
117         if ( ! isLegalOption( token ) )
118         {
119             throw new IllegalArgumentException JavaDoc( "illegal option: " + token );
120         }
121     }
122     
123         public int
124     getNumValues( String JavaDoc token )
125     {
126         checkLegalOption( token );
127         
128         final OptionDesc info = findOptionDesc( token );
129         
130         return( info.mNumValues );
131     }
132     
133         public boolean
134     isBoolean( String JavaDoc token )
135     {
136         checkLegalOption( token );
137         
138         final OptionDesc info = findOptionDesc( token );
139         
140         return( info.mIsBoolean );
141     }
142     
143         public void
144     foundIllegalOption( final String JavaDoc token )
145         throws ArgHelper.IllegalOptionException
146     {
147         throw new ArgHelper.IllegalOptionException( "illegal option: " + token );
148     }
149     
150         boolean
151     exists( final String JavaDoc optionName )
152     {
153         return( mOptionDescriptions.get( optionName ) != null );
154     }
155     
156         void
157     checkExists( final String JavaDoc optionName )
158     {
159         if ( exists( optionName ) )
160         {
161             throw new IllegalArgumentException JavaDoc( "can't add same option twice: " + optionName );
162         }
163     }
164     
165         void
166     add( final OptionDesc desc )
167     {
168         checkExists( desc.mName );
169         
170         mOptionDescriptions.put( desc.mName, desc );
171     }
172
173         public void
174     addBoolean( final String JavaDoc name )
175         throws ArgHelper.IllegalOptionException
176     {
177         add( new OptionDesc( name ) );
178     }
179     
180     
181         public void
182     addNonBoolean( final String JavaDoc name, final int numValues )
183         throws ArgHelper.IllegalOptionException
184     {
185         add( new OptionDesc( name, numValues ) );
186     }
187     
188     
189     public final static char MULTIPLE_DELIM = ' ';
190     public final static char NUMVALUES_DELIM = ',';
191     
192         public void
193     addOptions( String JavaDoc list )
194         throws ArgHelper.IllegalOptionException
195     {
196         final String JavaDoc [] names = list.split(" ");
197         
198         for( int i = 0; i < names.length; ++i )
199         {
200             final String JavaDoc [] data = names[ i ].split( "" + NUMVALUES_DELIM );
201             
202             if ( data.length == 1 )
203             {
204                 addBoolean( data[ 0 ] );
205             }
206             else
207             {
208                 addNonBoolean( data[ 0 ], new Integer JavaDoc( data[ 1 ] ).intValue() );
209             }
210         }
211     }
212         
213         
214     
215     public final static class OptionDesc
216     {
217         public String JavaDoc mName; // include "-" or "--" prefix
218
public int mNumValues;
219         public boolean mIsBoolean;
220         
221             
222             boolean
223         isLegalOptionNameChar( final char theChar )
224         {
225             return( (theChar >= 'a' && theChar <= 'z') ||
226                     (theChar >= 'A' && theChar <= 'Z') ||
227                     (theChar >= '0' && theChar <= '9') ||
228                     ( theChar == '-' || theChar == '_' || theChar == '.' )
229                 );
230         }
231
232             void
233         validateName( String JavaDoc name )
234             throws ArgHelper.IllegalOptionException
235         {
236             final boolean isLongOption = name.startsWith( "--" );
237             final boolean isShortOption = (! isLongOption) && name.startsWith( "-" );
238             final int length = name.length();
239             
240             if ( ! (isLongOption || isShortOption) )
241             {
242                 throw new ArgHelper.IllegalOptionException( "invalid option name: " + name );
243             }
244             
245             if ( isShortOption && length != 2 )
246             {
247                 throw new ArgHelper.IllegalOptionException( "invalid short option name: " + name );
248             }
249             
250             for( int i = 0; i < length; ++i )
251             {
252                 final char theChar = name.charAt( i );
253                 
254                 if ( ! isLegalOptionNameChar( theChar ) )
255                 {
256                     throw new ArgHelper.IllegalOptionException( "invalid character '" +
257                         theChar + "' in option name: " + name );
258                 }
259             }
260         }
261         
262         OptionDesc( String JavaDoc name, int numValues )
263             throws ArgHelper.IllegalOptionException
264         {
265             mName = mapName( name );
266             
267             validateName( mName );
268             
269             if ( numValues == 0 )
270             {
271                 throw new IllegalArgumentException JavaDoc( "use OptionDesc( name ) for boolean options" );
272             }
273             
274             mNumValues = numValues;
275             mIsBoolean = false;
276         }
277         
278         OptionDesc( String JavaDoc name )
279             throws ArgHelper.IllegalOptionException
280         {
281             mName = mapName( name );
282             
283             validateName( mName );
284             
285             mIsBoolean = true;
286             mNumValues = 1;
287         }
288         
289             static String JavaDoc
290         mapName( String JavaDoc name )
291         {
292             String JavaDoc mappedName = name;
293             
294             if ( ! name.startsWith( "-" ) )
295             {
296                 final String JavaDoc prefix = (name.length() == 1) ? "-" : "--";
297                 
298                 mappedName = prefix + name;
299             }
300             return( mappedName );
301         }
302         
303             public String JavaDoc
304         toString()
305         {
306             if ( mIsBoolean )
307             {
308                 return( mName + ":boolean" );
309             }
310             return( mName + ":" + mNumValues + " values" );
311         }
312     }
313
314 }
315
316
Popular Tags