1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.util.Vector ; 27 import java.util.Iterator ; 28 import java.util.Hashtable ; 29 import java.util.Enumeration ; 30 import java.io.Serializable ; 31 32 36 public class ValidCommand implements ICommand, Serializable 37 { 38 private String name; 40 private String numberOfOperands; 42 private Vector validOptions; 44 private Vector requiredOptions; 46 private Vector deprecatedOptions; 48 private String className; 50 private String usageText = null; 52 private Hashtable properties; 54 private String defaultOperand = null; 56 57 private transient static final String DEFAULTOPERAND_QUANTIFIER_REGEXP = "[\\*\\?]"; 59 60 63 public ValidCommand() 64 { 65 validOptions = new Vector (); 66 requiredOptions = new Vector (); 67 deprecatedOptions = new Vector (); 68 properties = new Hashtable (); 69 } 70 71 72 82 public ValidCommand(String name, String numberOfOperands, 83 Vector validOptions, Vector requiredOptions, 84 Vector deprecatedOptions, String usageText) 85 { 86 this.name = name; 87 this.numberOfOperands = numberOfOperands; 88 this.validOptions = validOptions; 89 this.requiredOptions = requiredOptions; 90 this.deprecatedOptions = deprecatedOptions; 91 this.usageText = usageText; 92 } 93 94 98 public String getName() 99 { 100 return name; 101 } 102 103 107 public void setName(String name) 108 { 109 this.name = name; 110 } 111 112 116 public String getNumberOfOperands() 117 { 118 return numberOfOperands; 119 } 120 121 127 public String getDefaultOperand() 128 { 129 return this.defaultOperand; 130 } 131 132 133 137 public void setNumberOfOperands(String numberOfOperands) 138 { 139 this.numberOfOperands = numberOfOperands; 140 } 141 142 149 public void setDefaultOperand(String defaultOperand) 150 { 151 if (defaultOperand != null && 152 numberOfOperands.matches(DEFAULTOPERAND_QUANTIFIER_REGEXP)) 153 { 154 this.defaultOperand = defaultOperand; 155 } 156 else 157 this.defaultOperand = null; 158 } 159 160 161 165 public Vector getValidOptions() 166 { 167 return validOptions; 168 } 169 170 174 public void setValidOptions(Vector validOptions) 175 { 176 this.validOptions = validOptions; 177 } 178 179 183 public Vector getRequiredOptions() 184 { 185 return requiredOptions; 186 } 187 188 192 public void setRequiredOptions(Vector requiredOptions) 193 { 194 this.requiredOptions = requiredOptions; 195 } 196 197 201 public Vector getDeprecatedOptions() 202 { 203 return deprecatedOptions; 204 } 205 206 210 public void setDeprecatedOptions(Vector deprecatedOptions) 211 { 212 this.deprecatedOptions = deprecatedOptions; 213 } 214 215 219 public String getClassName() 220 { 221 return className; 222 } 223 224 228 public void setClassName(String className) 229 { 230 this.className = className; 231 } 232 233 237 public String getUsageText() 238 { 239 return usageText; 240 } 241 242 246 public void setUsageText(String usageText) 247 { 248 this.usageText = usageText; 249 } 250 251 257 public ValidOption getOption(String longOptionName) 258 { 259 Vector allOptions = this.getOptions(); 260 if (longOptionName != null) { 261 for (int ii=0; ii<allOptions.size(); ii++) 262 { 263 ValidOption option = (ValidOption)allOptions.get(ii); 264 if ((option.getName()).equals(longOptionName)) 265 return option; 266 } 267 } 268 return null; 269 } 270 271 277 public ValidOption getAlternateDeprecatedOption(String optionName) 278 { 279 Vector allOptions = new Vector (validOptions); 280 allOptions.addAll(requiredOptions); 281 boolean hasAltDeprecatedOption = false; 282 ValidOption altDeprecatedOption = null; 283 for (int i = 0; (i < allOptions.size()) && !hasAltDeprecatedOption; i++) 284 { 285 ValidOption validOption = (ValidOption)allOptions.get(i); 286 final String deprecatedOptionStr = validOption.getDeprecatedOption(); 287 if ((deprecatedOptionStr != null) && 288 (deprecatedOptionStr.equals(optionName))) 289 { 290 hasAltDeprecatedOption = true; 291 altDeprecatedOption = validOption; 292 } 293 } 294 return altDeprecatedOption; 295 } 296 297 301 public Vector getOptions() 302 { 303 Vector options = new Vector (validOptions); 304 options.addAll(requiredOptions); 305 options.addAll(deprecatedOptions); 306 return options; 307 } 308 309 313 public Hashtable getProperties() 314 { 315 return properties; 316 } 317 318 322 public Object getProperty(String key) 323 { 324 return properties.get(key); 325 } 326 327 332 public void setProperty(String key, Object value) 333 { 334 properties.put(key, value); 335 } 336 337 341 public void addValidOption(ValidOption option) 342 { 343 if ((option != null) && !hasValidOption(option.getName())) 344 { 345 validOptions.add(option); 346 } 347 } 348 349 353 public void addRequiredOption(ValidOption option) 354 { 355 if ((option != null) && !hasRequiredOption(option.getName())) 356 { 357 requiredOptions.add(option); 358 } 359 } 360 361 365 public void addDeprecatedOption(ValidOption option) 366 { 367 if ((option != null) && !hasDeprecatedOption(option.getName())) 368 { 369 deprecatedOptions.add(option); 370 } 371 } 372 373 377 public void deleteOption(ValidOption option) 378 { 379 if (option != null) 380 { 381 validOptions.remove(option); 382 } 383 } 384 385 390 public boolean hasValidOption(String optionName) 391 { 392 boolean hasValidOption = false; 393 if (optionName != null) { 394 for (int i = 0; (i < validOptions.size()) && !hasValidOption; i++) 395 { 396 if (((ValidOption)validOptions.get(i)).getName().equals(optionName)) 397 hasValidOption = true; 398 } 399 } 400 return hasValidOption; 401 } 402 403 408 public boolean hasValidOption(ValidOption option) 409 { 410 return validOptions.contains(option); 411 } 412 413 418 public boolean hasRequiredOption(String optionName) 419 { 420 boolean hasRequiredOption = false; 421 for (int i = 0; (i < requiredOptions.size()) && !hasRequiredOption; i++) 422 { 423 if (((ValidOption)requiredOptions.get(i)).getName().equals(optionName)) 424 hasRequiredOption = true; 425 } 426 return hasRequiredOption; 427 } 428 429 434 public boolean hasRequiredOption(ValidOption option) 435 { 436 return requiredOptions.contains(option); 437 } 438 439 444 public boolean hasDeprecatedOption(String optionName) 445 { 446 boolean hasDeprecatedOption = false; 447 for (int i = 0; (i < deprecatedOptions.size()) && !hasDeprecatedOption; i++) 448 { 449 if (((ValidOption)deprecatedOptions.get(i)).getName().equals(optionName)) 450 hasDeprecatedOption = true; 451 } 452 return hasDeprecatedOption; 453 } 454 455 460 public boolean hasDeprecatedOption(ValidOption option) 461 { 462 return deprecatedOptions.contains(option); 463 } 464 465 470 public boolean hasAlternateDeprecatedOption(String optionName) 471 { 472 Vector allOptions = new Vector (validOptions); 473 allOptions.addAll(requiredOptions); 474 boolean hasAltDeprecatedOption = false; 475 for (int i = 0; (i < allOptions.size()) && !hasAltDeprecatedOption; i++) 476 { 477 String deprecatedOptionStr = 478 ((ValidOption)allOptions.get(i)).getDeprecatedOption(); 479 if ((deprecatedOptionStr != null) && 480 (deprecatedOptionStr.equals(optionName))) 481 { 482 hasAltDeprecatedOption = true; 483 } 484 } 485 return hasAltDeprecatedOption; 486 } 487 488 489 494 public boolean hasProperty(String propertyName) 495 { 496 return properties.containsKey(propertyName); 497 } 498 499 500 504 public void replaceAllOptions(ValidOption replaceOption) 505 { 506 if (replaceOption != null) 507 { 508 final String replaceOptionName = replaceOption.getName(); 509 if (hasValidOption(replaceOptionName)) 510 { 511 replaceOptionsList(validOptions, replaceOption); 512 } 513 if (hasRequiredOption(replaceOptionName)) 514 { 515 replaceOptionsList(requiredOptions, replaceOption); 516 } 517 if (hasDeprecatedOption(replaceOptionName)) 518 { 519 replaceOptionsList(deprecatedOptions, replaceOption); 520 } 521 } 522 } 523 524 525 528 private void replaceOptionsList(Vector optionsList, ValidOption replaceOption) 529 { 530 final String replaceOptionName = replaceOption.getName(); 531 for (int ii = 0; ii<optionsList.size(); ii++) 532 { 533 try { 534 if (((ValidOption)optionsList.get(ii)).getName().equals(replaceOptionName)) 535 { 536 if (((ValidOption)optionsList.get(ii)).hasDeprecatedOption()) { 537 final String deprecatedOption=((ValidOption)optionsList.get(ii)).getDeprecatedOption(); 538 replaceOption.setDeprecatedOption(deprecatedOption); 539 } 540 541 optionsList.set(ii, replaceOption); 542 break; 543 } 544 } 545 catch (Exception e) 546 { 547 e.printStackTrace(); 548 } 549 } 550 return; 551 } 552 553 554 555 556 560 public String toString() 561 { 562 String validOptionsStr = "{"; 563 for (int i = 0; i < validOptions.size(); i++) 564 { 565 validOptionsStr += validOptions.get(i).toString() + ","; 566 } 567 validOptionsStr += "}"; 568 569 String requiredOptionsStr = ""; 570 for (int i = 0; i < requiredOptions.size(); i++) 571 { 572 requiredOptionsStr += requiredOptions.get(i).toString() + ","; 573 } 574 requiredOptionsStr += "}"; 575 576 String deprecatedOptionsStr = ""; 577 for (int i = 0; i < deprecatedOptions.size(); i++) 578 { 579 deprecatedOptionsStr += deprecatedOptions.get(i).toString() + ","; 580 } 581 deprecatedOptionsStr += "}"; 582 583 String propertiesStr = "{"; 584 for (Enumeration propertyNames = properties.keys() ; propertyNames.hasMoreElements() ;) 585 { 586 String name = (String ) propertyNames.nextElement(); 587 Vector value = (Vector ) properties.get(name); 588 propertiesStr += "|" + name + " , " + value; 589 } 590 propertiesStr += "}"; 591 592 return (name + " " + numberOfOperands + " | " + validOptionsStr + 593 " | " + requiredOptionsStr + " | " + 594 " | " + deprecatedOptionsStr + " | " + usageText + " " + 595 propertiesStr); 596 } 597 } 598 | Popular Tags |