KickJava   Java API By Example, From Geeks To Geeks.

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


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/ArgHelperOptionInfoImplBase.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.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33  
34
35
36 public class ArgHelperOptionInfoImplBase implements ArgHelper.OptionsInfo
37 {
38     final ArrayList JavaDoc mOptionDescriptions;
39     
40         public
41     ArgHelperOptionInfoImplBase( )
42         throws ArgHelper.IllegalOptionException
43     {
44         mOptionDescriptions = new ArrayList JavaDoc();
45     }
46     
47         public
48     ArgHelperOptionInfoImplBase( String JavaDoc options )
49         throws ArgHelper.IllegalOptionException
50     {
51         mOptionDescriptions = new ArrayList 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         OptionDesc info = null;
97         
98         final String JavaDoc optionName = tokenToOptionName( OptionDesc.mapName( token ) );
99         
100         final Iterator JavaDoc iter = mOptionDescriptions.iterator();
101         while( iter.hasNext() )
102         {
103             final OptionDesc optionDesc = (OptionDesc)iter.next();
104             
105             if ( optionDesc.mName.equalsIgnoreCase( optionName ) )
106             {
107                 info = optionDesc;
108                 break;
109             }
110         }
111         return( info );
112     }
113     
114         public boolean
115     isLegalOption( String JavaDoc token )
116     {
117         final boolean isLegal = (findOptionDesc( token ) != null);
118         
119         return( isLegal );
120     }
121     
122         public void
123     checkLegalOption( String JavaDoc token )
124     {
125         if ( ! isLegalOption( token ) )
126         {
127             throw new IllegalArgumentException JavaDoc( "illegal option: " + token );
128         }
129     }
130     
131         public int
132     getNumValues( String JavaDoc token )
133     {
134         checkLegalOption( token );
135         
136         final OptionDesc info = findOptionDesc( token );
137         
138         return( info.mNumValues );
139     }
140     
141         public boolean
142     isBoolean( String JavaDoc token )
143     {
144         checkLegalOption( token );
145         
146         final OptionDesc info = findOptionDesc( token );
147         
148         return( info.mIsBoolean );
149     }
150     
151         public void
152     foundIllegalOption( String JavaDoc token )
153         throws ArgHelper.IllegalOptionException
154     {
155         throw new ArgHelper.IllegalOptionException( "illegal option: " + token );
156     }
157     
158         public void
159     addBoolean( String JavaDoc name )
160         throws ArgHelper.IllegalOptionException
161     {
162         mOptionDescriptions.add( new OptionDesc( name ) );
163     }
164     
165     
166         public void
167     addNonBoolean( String JavaDoc name, int numValues )
168         throws ArgHelper.IllegalOptionException
169
170     {
171         mOptionDescriptions.add( new OptionDesc( name, numValues ) );
172     }
173     
174     
175     public final static char MULTIPLE_DELIM = ' ';
176     public final static char NUMVALUES_DELIM = ',';
177     
178         public void
179     addOptions( String JavaDoc list )
180         throws ArgHelper.IllegalOptionException
181     {
182         final String JavaDoc [] names = list.split(" ");
183         
184         for( int i = 0; i < names.length; ++i )
185         {
186             final String JavaDoc [] data = names[ i ].split( "" + NUMVALUES_DELIM );
187             
188             if ( data.length == 1 )
189             {
190                 addBoolean( data[ 0 ] );
191             }
192             else
193             {
194                 addNonBoolean( data[ 0 ], new Integer JavaDoc( data[ 1 ] ).intValue() );
195             }
196         }
197     }
198         
199         
200     
201     public final static class OptionDesc
202     {
203         public String JavaDoc mName; // include "-" or "--" prefix
204
public int mNumValues;
205         public boolean mIsBoolean;
206         
207             void
208         validateName( String JavaDoc name )
209             throws ArgHelper.IllegalOptionException
210         {
211             if ( ! name.startsWith( "-" ) )
212             {
213                 throw new ArgHelper.IllegalOptionException( "invalid option name: " + name );
214             }
215         }
216         
217         OptionDesc( String JavaDoc name, int numValues )
218             throws ArgHelper.IllegalOptionException
219         {
220             mName = mapName( name );
221             
222             validateName( mName );
223             
224             if ( numValues == 0 )
225             {
226                 throw new IllegalArgumentException JavaDoc( "use OptionDesc( name ) for boolean options" );
227             }
228             
229             mNumValues = numValues;
230             mIsBoolean = false;
231         }
232         
233         OptionDesc( String JavaDoc name )
234             throws ArgHelper.IllegalOptionException
235         {
236             mName = mapName( name );
237             
238             validateName( mName );
239             
240             mIsBoolean = true;
241             mNumValues = 1;
242         }
243         
244             static String JavaDoc
245         mapName( String JavaDoc name )
246         {
247             String JavaDoc mappedName = name;
248             
249             if ( ! name.startsWith( "-" ) )
250             {
251                 final String JavaDoc prefix = (name.length() == 1) ? "-" : "--";
252                 
253                 mappedName = prefix + name;
254             }
255             return( mappedName );
256         }
257         
258             public String JavaDoc
259         toString()
260         {
261             if ( mIsBoolean )
262             {
263                 return( mName + ":boolean" );
264             }
265             return( mName + ":" + mNumValues + " values" );
266         }
267     }
268
269 }
270
271
Popular Tags