1 23 24 29 30 package com.sun.cli.jmx.cmd; 31 32 33 import java.util.ArrayList ; 34 import java.util.ListIterator ; 35 import com.sun.cli.util.stringifier.SmartStringifier; 36 37 final class ParsedOption 38 { 39 final String mName; 40 final String [] mValues; 41 42 ParsedOption( String name, String [] 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 mTokens; 55 final ArrayList mOptions; 56 boolean mDone; 57 58 final ArgHelper.OptionsInfo mOptionInfo; 59 60 ParseState( 61 final ListIterator tokens, 62 final ArgHelper.OptionsInfo optionInfo) 63 { 64 assert( tokens != null ); 65 66 mTokens = tokens; 67 mOptions = new ArrayList (); 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 [] 83 getOperands() 84 { 85 final ArrayList list = new ArrayList (); 87 88 while( mTokens.hasNext() ) 89 { 90 list.add( mTokens.next() ); 91 } 92 93 String [] operands = new String [ list.size() ]; 94 list.toArray( operands ); 95 96 return( operands ); 97 } 98 99 String 100 nextToken( ) 101 { 102 return( (String )mTokens.next() ); 103 } 104 105 String 106 peekToken( ) 107 { 108 final String result = (String )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 [] mOperands; 132 133 private final static String ARG_PREFIX = "--"; 134 135 private static void 136 dm( Object o ) 137 { 138 System.out.println( SmartStringifier.toString( o ) ); 139 } 140 141 142 static void 143 numValuesFailure( final String 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 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 static String [] 161 extractValues( final String optionName, final String valueList, final int numValues ) 162 throws ArgHelper.IllegalOptionException 163 { 164 final String [] values = valueList.split( "," ); 167 168 if ( values.length != numValues ) 169 { 170 numValuesFailure( optionName, numValues ); 171 } 172 173 return( values ); 174 } 175 176 static String [] 178 extractValues( final ParseState ps, final String optionName, final int numValues ) 179 throws ArgHelper.IllegalOptionException 180 { 181 final String [] values = new String [ 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 e ) 191 { 192 numValuesFailure( optionName, numValues ); 193 } 194 catch( ArrayIndexOutOfBoundsException e ) 195 { 196 numValuesFailure( optionName, numValues ); 197 } 198 199 return( values ); 200 } 201 202 private static String 203 extractBoolean( final String optionName, final String value ) 204 throws ArgHelper.IllegalOptionException 205 { 206 String result = "true"; 208 209 if ( value != null ) 210 { 211 if ( value.equalsIgnoreCase( "true" ) || 213 value.equalsIgnoreCase( "false" ) ) 214 { 215 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 token = ps.nextToken(); 232 final ArgHelper.OptionsInfo optionsInfo = ps.mOptionInfo; 233 234 final String optionName = optionsInfo.tokenToOptionName( token ); 235 final String optionExtra = optionsInfo.tokenToOptionData( token ); 236 237 final boolean isBooleanOption = optionsInfo.isBoolean( optionName ); 238 final int numValues = optionsInfo.getNumValues( optionName ); 239 240 String [] values = null; 241 if ( isBooleanOption ) 242 { 243 values = new String [] { extractBoolean( optionName, optionExtra ) }; 244 } 245 else 246 { 247 if ( optionExtra != null ) 248 { 249 values = extractValues( optionName, optionExtra, numValues ); 251 } 252 else 253 { 254 values = extractValues( ps, optionName, numValues ); 256 } 257 } 258 259 final String 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 token = ps.peekToken(); 269 270 if ( token.equals( ARG_PREFIX ) ) 271 { 272 ps.setDone(); 274 } 275 else if ( token.startsWith( "-" ) ) 276 { 277 throw new ArgHelper.IllegalOptionException( "illegal option: " + token ); 278 } 279 else 280 { 281 ps.setDone(); 283 } 284 } 285 286 287 public 288 ArgHelperImpl( 289 final ListIterator tokens, 290 final ArgHelper.OptionsInfo optionInfo ) 291 throws ArgHelper.IllegalOptionException 292 { 293 final ParseState ps = new ParseState( tokens, optionInfo ); 294 295 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 } 314 315 316 317 private ParsedOption 318 findOption( final String 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 337 public String [] 338 getArgValues( final String name ) 339 throws ArgHelper.IllegalOptionException 340 { 341 String [] results = null; 342 343 String prefixedName = name; 344 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 362 getArgValue( final String name ) 363 throws ArgHelper.IllegalOptionException 364 { 365 String value = null; 366 367 final String [] 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 391 getString( final String name, final String defaultValue) 392 throws ArgHelper.IllegalOptionException 393 { 394 String result = getArgValue( name ); 395 if ( result == null ) 396 { 397 result = defaultValue; 398 } 399 400 return( result ); 401 } 402 403 404 public Integer 405 getInteger( final String name ) 406 throws ArgHelper.IllegalOptionException 407 { 408 final String value = getArgValue( name ); 409 Integer result = null; 410 411 if ( value != null ) 412 { 413 result = new Integer ( value ); 414 } 415 416 return( result ); 417 } 418 419 public Boolean 420 getBoolean( final String name, final Boolean defaultValue) 421 throws ArgHelper.IllegalOptionException 422 { 423 final String value = getArgValue( name ); 424 425 Boolean result = null; 426 427 if ( value == null ) 428 { 429 result = defaultValue; 431 } 432 else 433 { 434 result = new Boolean ( value ); 436 } 437 438 return( result ); 439 } 440 441 public String [] 442 getOperands( ) 443 { 444 return( mOperands ); 445 } 446 } 447 448 | Popular Tags |