1 23 package com.sun.enterprise.cli.framework; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Vector ; 30 import java.util.Properties ; 31 import java.util.Enumeration ; 32 import java.util.ListIterator ; 33 import java.util.Arrays ; 34 import java.util.NoSuchElementException ; 35 36 41 public class CommandLineParser 42 { 43 44 private String commandName = null; 46 47 private HashMap optionsList = new HashMap (); 49 50 private Vector Operands = new Vector (); 52 53 private ValidCommand validCommand = null; 55 56 private static final String ANY_SHORT_OPTION_REGEXP = "^-[^-.*]+"; 58 59 private static final String SHORT_OPTION_REGEXP = "^-[\\w?]+"; 61 62 private static final String SHORT_OPTION_ARGUMENT_REGEXP = "^-[\\w?](=.*)"; 64 65 private final String LONG_OPTION_REGEXP = "^--\\w[-\\w]*(=.*)*"; 68 69 private static final String COMMAND_NAME_REGEXP = "^[a-z\\-][a-z0-9\\-\\_\\ ]*$"; 71 72 private static final String SHORT_OPTION_HELP_REGEXP = "-\\w*[?]\\w*"; 75 76 private static final String HELP_OPTION = "--help"; 78 79 private static final String BOOLEAN_NO_OPTION = "--no-"; 81 82 private static final String BOOLEAN = "boolean"; 84 85 private static final String TRUE = "true"; 87 88 private static final String FALSE = "false"; 90 91 92 93 public CommandLineParser() 94 { 95 } 96 97 98 102 public CommandLineParser(ValidCommand validCommand) 103 { 104 this.validCommand = validCommand; 105 } 106 107 108 114 public CommandLineParser(String [] args, ValidCommand validCommand) 115 throws CommandValidationException, HelpException 116 { 117 this.validCommand = validCommand; 118 if (validCommand != null) 119 { 120 parseCommandLine(args); 121 } 122 123 } 124 125 126 127 131 public void parseCommandLine(final String [] args) 132 throws CommandValidationException, HelpException 133 { 134 commandName = args[0]; 135 136 137 if (this.validCommand == null) 138 { 139 throw new CommandValidationException(getLocalizedString("InvalidCommand", 140 new Object [] {commandName} )); 141 } 142 143 for (int ii=1; ii<args.length; ii++) 145 { 146 if (args[ii].equals(HELP_OPTION) || 149 args[ii].matches(SHORT_OPTION_HELP_REGEXP)) 150 throw new HelpException(commandName); 151 152 153 if (args[ii].matches(ANY_SHORT_OPTION_REGEXP) ) 155 { 156 ii = verifyShortOptions(args, ii); 157 } 158 159 else if (args[ii].matches(LONG_OPTION_REGEXP)) 165 { 166 ii = insertLongOption(args, ii); 167 } 168 169 else 171 { 172 ii = insertOperands(Arrays.asList(args).listIterator(ii)); 173 } 174 } 175 insertPrefFileOptions(); 177 insertDefaultOptions(); 178 replaceAlternativeOptions(); 179 } 180 181 182 192 private int verifyShortOptions(final String [] args, int ii) 193 throws CommandValidationException 194 { 195 int index; 196 if (args[ii].matches(SHORT_OPTION_REGEXP) || 197 args[ii].matches(SHORT_OPTION_ARGUMENT_REGEXP)) { 198 index = insertShortOption(args, ii); 199 } else { 200 throw new CommandValidationException(getLocalizedString("NoSuchOption", 201 new Object [] {args[ii]})); 202 } 203 return index; 204 } 205 206 207 211 private void insertShortOptionsGroup(final String sOptions) 212 throws CommandValidationException 213 { 214 for (int ii=1; ii<sOptions.length(); ii++) 215 { 216 final String optionName = findOptionName(sOptions.charAt(ii)); 217 insertBooleanOption(optionName); 218 } 219 } 220 221 222 228 private void insertBooleanOption(String optionName) 229 throws CommandValidationException 230 { 231 if (checkOptionIsBoolean(optionName)) 232 { 233 optionsList.put(optionName, TRUE); 236 } 237 else 238 { 239 optionsList.put(optionName, null); 240 } 241 } 242 243 244 250 private int insertShortOption(final String [] args, int ii) 251 throws CommandValidationException 252 { 253 254 if (args[ii].length() > 2) 257 { 258 final int index = args[ii].indexOf('='); 259 if (index == -1) 261 { 262 insertShortOptionsGroup(args[ii]); 263 } 264 else 265 { 266 insertOptionWithEqualSign(findOptionName(args[ii].charAt(1)), 267 args[ii].substring(index+1)); 268 } 269 } 270 else 271 { 272 if (ii+1 < args.length) 274 { 275 final String optionName = findOptionName(args[ii].charAt(1)); 276 277 if (args[ii+1].startsWith("-") || 282 checkOptionIsBoolean(optionName) ) 283 { 284 insertBooleanOption(optionName); 285 } 286 else 287 { 288 optionsList.put(findOptionName(args[ii].charAt(1)), args[ii+1]); 289 ii++; 290 } 291 } 292 else 293 { 294 insertBooleanOption(findOptionName(args[ii].charAt(1))); 295 } 296 } 297 return ii; 298 } 299 300 301 307 private int insertLongOption(final String [] args, int ii) 308 throws CommandValidationException 309 { 310 final int index = args[ii].indexOf('='); 311 if (index == -1) 313 { 314 if (args[ii].startsWith(BOOLEAN_NO_OPTION) && 316 checkOptionIsBoolean(args[ii].substring(BOOLEAN_NO_OPTION.length())) ) 317 { 318 optionsList.put(args[ii].substring(BOOLEAN_NO_OPTION.length()), FALSE); 319 } 320 else if (checkOptionIsBoolean(args[ii].substring(2))) 321 { 322 optionsList.put(args[ii].substring(2), TRUE); 324 } 325 else if (ii+1<args.length) 326 { 327 if (args[ii+1].startsWith("-") ) 329 { 330 optionsList.put(args[ii].substring(2), null); 331 } 332 else 333 { 334 optionsList.put(args[ii].substring(2), args[ii+1]); 335 ii++; 336 } 337 } 338 else 339 optionsList.put(args[ii].substring(2), null); 340 } 341 else 343 { 344 insertOptionWithEqualSign(args[ii].substring(2, index), 345 args[ii].substring(index+1)); 346 } 347 return ii; 348 } 349 350 351 357 private void insertOptionWithEqualSign(String optionName, String optionValue) 358 throws CommandValidationException 359 { 360 if (checkOptionIsBoolean(optionName)) 361 { 362 if (optionValue!=null && 364 (optionValue.compareToIgnoreCase(TRUE)==0 || 365 optionValue.compareToIgnoreCase(FALSE)==0)) 366 { 367 optionsList.put(optionName, optionValue); 368 } 369 else 370 throw new CommandValidationException(getLocalizedString("OptionIsBoolean", 371 new Object [] {optionName} )); 372 } 373 else 374 optionsList.put(optionName, optionValue); 375 } 376 377 378 381 private void insertDefaultOptions() 382 { 383 final Vector allOptions = validCommand.getOptions(); 384 if (allOptions != null) 385 { 386 for (int ii=0; ii<allOptions.size(); ii++) 387 { 388 final ValidOption currentOption = (ValidOption)allOptions.get(ii); 389 if (currentOption != null && 390 !optionsList.containsKey(currentOption.getName()) && 391 !isEnvironmentOptionExists(currentOption.getName()) && 392 !validCommand.hasDeprecatedOption(currentOption) && 393 currentOption.getDefaultValue()!=null) 394 { 395 CLILogger.getInstance().printDebugMessage( 396 "**** insert Default Options " + 397 currentOption.getName() + " " + 398 currentOption.getDefaultValue()); 399 optionsList.put(currentOption.getName(), 400 currentOption.getDefaultValue()); 401 } 402 } 403 } 404 } 405 406 407 410 private void insertPrefFileOptions() 411 { 412 try{ 413 String homedir = System.getProperty("user.home"); 414 CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance(); 415 String filename = cdr.getEnvironmentFileName(); 416 String envPrefix = cdr.getEnvironmentPrefix(); 417 FileInputStream prefsFile = new 418 FileInputStream (homedir+File.separator+filename); 419 Properties p = new Properties (); 420 p.load(prefsFile); 421 final Vector allOptions = validCommand.getOptions(); 422 for (int ii=0; ii<allOptions.size(); ii++) 423 { 424 final ValidOption currentOption = (ValidOption)allOptions.get(ii); 425 final String optionName = currentOption.getName(); 426 String envVarName = envPrefix+optionName.toUpperCase(); 427 String value = p.getProperty(envVarName); 428 if(!optionsList.containsKey(optionName) && 429 !isEnvironmentOptionExists(currentOption.getName()) && 430 (value != null)) 431 { 432 optionsList.put(optionName, value); 433 } 434 } 435 } 436 catch(IOException e){ 437 } 439 } 440 441 442 445 private void insertEnvironmentOptions() 446 { 447 final HashMap envOptions = CommandEnvironment.getInstance().getEnvironments(); 448 final Vector allOptions = validCommand.getOptions(); 449 if (envOptions != null) 450 { 451 for (int ii=0; ii<allOptions.size(); ii++) 452 { 453 final ValidOption currentOption = (ValidOption)allOptions.get(ii); 454 final String optionName = currentOption.getName(); 455 if (currentOption != null && 456 !optionsList.containsKey(currentOption.getName()) && 457 envOptions.containsKey(optionName)) 458 { 459 CLILogger.getInstance().printDebugMessage( 460 "**** insert Environment Options " + 461 optionName + " " + 462 (String )envOptions.get(optionName) ); 463 optionsList.put(optionName, (String )envOptions.get(optionName)); 464 } 465 } 466 } 467 } 468 469 470 474 public HashMap getValidEnvironmentOptions() 475 { 476 final HashMap envOptions = CommandEnvironment.getInstance().getEnvironments(); 477 HashMap validEnvOptions = new HashMap (); 478 final Vector allOptions = validCommand.getOptions(); 479 if (envOptions != null) 480 { 481 for (int ii=0; ii<allOptions.size(); ii++) 482 { 483 final ValidOption currentOption = (ValidOption)allOptions.get(ii); 484 final String optionName = currentOption.getName(); 485 if (currentOption != null && 486 envOptions.containsKey(optionName)) 487 { 488 CLILogger.getInstance().printDebugMessage( 489 "**** valid Environment Options " + 490 optionName + " " + 491 (String )envOptions.get(optionName) ); 492 validEnvOptions.put(optionName, (String )envOptions.get(optionName)); 493 } 494 } 495 } 496 return validEnvOptions; 497 } 498 499 500 505 public boolean isEnvironmentOptionExists(String optionName) 506 { 507 final HashMap envOptions = CommandEnvironment.getInstance().getEnvironments(); 508 if (envOptions.containsKey(optionName)) 509 return true; 510 else 511 return false; 512 } 513 514 515 519 private void replaceAlternativeOptions() 520 { 521 final Vector optionNames = new Vector (optionsList.keySet()); 522 523 for (int ii=0; ii<optionNames.size(); ii++) 524 { 525 final String optionName = (String )optionNames.get(ii); 526 if (validCommand.hasAlternateDeprecatedOption(optionName)) 527 { 528 531 final String alternateOptionName = 532 validCommand.getAlternateDeprecatedOption(optionName).getName(); 533 534 optionsList.put(alternateOptionName,(String )optionsList.get(optionName)); 535 536 CLILogger.getInstance().printWarning(getLocalizedString("OptionDeprecatedUseNew", 537 new Object [] {optionName, alternateOptionName})); 538 optionsList.remove(optionName); 539 } 540 } 541 542 } 543 544 545 549 private int insertOperands(final ListIterator operandIter) 550 throws CommandValidationException 551 { 552 try 553 { 554 if (!((String )operandIter.next()).equals("--")) 556 { 557 Operands.add((String )operandIter.previous()); 558 operandIter.next(); 560 } 561 562 while (operandIter.hasNext()) 563 { 564 Operands.add((String )operandIter.next()); 565 } 566 } 567 catch (NoSuchElementException nsee) 568 { 569 throw new CommandValidationException(nsee); 570 } 571 return operandIter.nextIndex(); 572 } 573 574 575 580 private String findOptionName(final char shortOption) 581 throws CommandValidationException 582 { 583 final Vector allOptions = validCommand.getOptions(); 585 if (allOptions != null) 586 { 587 for (int ii=0; ii<allOptions.size(); ii++) 588 { 589 Vector shortOptions = 590 ((ValidOption)allOptions.get(ii)).getShortNames(); 591 592 if (shortOptions.contains(String.valueOf(shortOption))) 593 { 594 return ((ValidOption)allOptions.get(ii)).getName(); 595 } 596 } 597 } 598 throw new CommandValidationException(getLocalizedString("NoSuchOption", 599 new Object [] {String.valueOf(shortOption)} )); 600 } 601 602 603 607 private boolean checkOptionIsBoolean(final String optionName) 608 { 609 final ValidOption option = validCommand.getOption(optionName); 610 if (option != null) 611 { 612 if ((option.getType().compareToIgnoreCase(BOOLEAN)) == 0) 613 return true; 614 else 615 return false; 616 } 617 else 618 { 619 return false; 620 } 621 } 622 623 627 642 643 646 public HashMap getOptionsList() 647 { 648 return optionsList; 649 } 650 651 652 655 public Vector getOperands() 656 { 657 return Operands; 658 } 659 660 667 protected String getLocalizedString(String key, Object [] toInsert) 668 { 669 try 670 { 671 final LocalStringsManager lsm = 672 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 673 return lsm.getString(key, toInsert); 674 } 675 catch (CommandValidationException cve) 676 { 677 return LocalStringsManager.DEFAULT_STRING_VALUE; 678 } 679 } 680 681 682 public String toString() 683 { 684 return "\n**********\nname = " + commandName + 685 "\nOptions = " + optionsList + 686 "\nOperands = " + Operands + "\n**********\n"; 687 } 688 689 } 690 | Popular Tags |