1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.util.Vector ; 27 import java.util.List ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.Enumeration ; 32 import java.util.regex.Pattern ; 33 import java.util.regex.Matcher ; 34 35 41 abstract public class Command implements ICommand 42 { 43 44 private static final char REPLACE_START_CHAR = '{'; 45 private static final char REPLACE_END_CHAR = '}'; 46 private static final char VARIABLE_START_CHAR = '$'; 47 private static final char OPERAND_START_CHAR = '#'; 48 private static final String PATTERN_MATCHING = "\\{([\\$\\#])(\\w[\\w\\-]*)\\}"; 49 51 protected String name = null; 52 protected HashMap clOptions; 54 protected HashMap envOptions; 55 protected Vector operands; 56 protected String usageStr = null; 57 protected Hashtable properties; 58 private List booleanOptionsList = null; 59 60 61 public Command() 62 { 63 operands = new Vector (); 64 clOptions = new HashMap (); 66 envOptions = new HashMap (); 67 } 68 69 73 abstract public void runCommand() 74 throws CommandException, CommandValidationException; 75 76 83 abstract public boolean validateOptions() throws CommandValidationException; 84 85 86 90 public String getName() 91 { 92 return name; 93 } 94 95 99 public void setName(String name) 100 { 101 this.name = name; 102 } 103 104 108 public Vector getOperands() 109 { 110 return operands; 111 } 112 113 117 public void setOperands(Vector operands) 118 { 119 this.operands = operands; 120 } 121 122 126 public HashMap getOptions() 127 { 128 HashMap options = new HashMap (clOptions); 129 Iterator optionNames = envOptions.keySet().iterator(); 130 while (optionNames.hasNext()) 131 { 132 final String optionKey = (String ) optionNames.next(); 133 if (!options.containsKey(optionKey)) 134 options.put(optionKey, (String ) envOptions.get(optionKey)); 135 } 136 return options; 137 } 138 139 140 144 public HashMap getCLOptions() 145 { 146 return clOptions; 147 } 148 149 153 public HashMap getENVOptions() 154 { 155 return envOptions; 156 } 157 158 162 168 169 170 174 public void setCLOptions(HashMap clOptions) 175 { 176 this.clOptions = clOptions; 177 } 178 179 183 public void setEnvOptions(HashMap envOptions) 184 { 185 this.envOptions = envOptions; 186 } 187 188 192 public String getOption(String optionName) 193 { 194 if (!optionNameExist(optionName)) return null; 195 return (clOptions.get(optionName) != null)? 196 (String ) clOptions.get(optionName) : 197 (String ) envOptions.get(optionName); 198 } 199 200 201 205 public String getCLOption(String optionName) 206 { 207 return ((String ) clOptions.get(optionName)); 208 } 209 210 211 215 public String getENVOption(String optionName) 216 { 217 return ((String ) envOptions.get(optionName)); 218 } 219 220 221 226 public void setOption(String optionName, String optionValue) 227 { 228 clOptions.put(optionName, optionValue); 229 } 230 231 232 240 public void setBooleanOptions(List booleanOptions) 241 { 242 booleanOptionsList = booleanOptions; 243 } 244 245 246 250 protected boolean getBooleanOption(String optionName) 251 { 252 return new Boolean (getOption(optionName)).booleanValue(); 253 } 254 255 256 260 protected int getIntegerOption(String optionName) 261 { 262 assert(!optionNameExist(optionName)); 264 return (new Integer (getOption(optionName)).intValue()); 265 } 266 267 268 272 private boolean optionNameExist(String optionName) 273 { 274 return (clOptions.containsKey(optionName) || envOptions.containsKey(optionName)); 275 } 276 277 278 282 public String getUsageText() 283 { 284 return usageStr; 285 } 286 287 291 public void setUsageText(String usageText) 292 { 293 this.usageStr = usageText; 294 } 295 296 301 public Object getProperty(String key) 302 { 303 return properties.get(key); 304 } 305 306 307 311 public void setProperty(String key, Object value) 312 { 313 properties.put(key, value); 314 } 315 316 317 321 protected Hashtable getProperties(String key) 322 { 323 return properties; 324 } 325 326 327 331 protected void setProperties(Hashtable properties) 332 { 333 this.properties = properties; 334 } 335 336 337 344 protected String getLocalizedString(String key) 345 { 346 LocalStringsManager lsm = null; 347 try 348 { 349 lsm = LocalStringsManagerFactory.getCommandLocalStringsManager(); 350 } 351 catch (CommandValidationException cve) 352 { 353 return LocalStringsManager.DEFAULT_STRING_VALUE; 354 } 355 return lsm.getString(key); 356 } 357 358 359 367 protected String getLocalizedString(String key, Object [] toInsert) 368 { 369 LocalStringsManager lsm = null; 370 try 371 { 372 lsm = LocalStringsManagerFactory.getCommandLocalStringsManager(); 373 return lsm.getString(key, toInsert); 374 } 375 catch (CommandValidationException cve) 376 { 377 return LocalStringsManager.DEFAULT_STRING_VALUE; 378 } 379 } 380 381 382 386 public String toString() 387 { 388 StringBuffer strbuf = new StringBuffer (); 389 390 strbuf.append(getName()); 391 392 Iterator optionNames = clOptions.keySet().iterator(); 393 while (optionNames.hasNext()) 394 { 395 final String optionKey = (String ) optionNames.next(); 396 strbuf.append(" --" + optionKey ); 397 if (booleanOptionsList.contains(optionKey)) 398 strbuf.append("="); 399 else 400 strbuf.append(" "); 401 strbuf.append((String ) clOptions.get(optionKey)); 402 } 403 for (int ii=0; ii<operands.size(); ii++) 404 { 405 strbuf.append(" "+ operands.get(ii).toString()); 406 } 407 return strbuf.toString(); 408 } 409 410 411 417 public String replacePattern(String replaceValue) 418 throws CommandException 419 { 420 if (replaceValue == null) return null; 421 final Pattern patt = Pattern.compile(PATTERN_MATCHING); 422 final Matcher match = patt.matcher(replaceValue); 423 String outstr = replaceValue; 424 425 try 426 { 427 if (match.find()) 428 { 429 StringBuffer strbuf = new StringBuffer (); 430 do 431 { 432 String value = findPatternStringValue(match.group(1), 433 match.group(2)); 434 if (value == null) 436 return value; 437 value = prepareStringForAppend(value); 438 match.appendReplacement(strbuf, value); 439 CLILogger.getInstance().printDebugMessage("strbuf = " + strbuf); 440 } while (match.find()); 441 match.appendTail(strbuf); 442 outstr = strbuf.toString(); 443 } 444 } 445 catch (java.lang.IllegalArgumentException iae) 446 { 447 try 448 { 449 final LocalStringsManager lsm = 450 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 451 throw new CommandException(lsm.getString("RequireEscapeChar"), iae); 452 } 453 catch (CommandValidationException cve) 454 { 455 throw new CommandException(cve); 456 } 457 } 458 catch (Exception e) 459 { 460 throw new CommandException(e); 461 } 462 return (outstr.length()<1)?null:outstr; 463 } 464 465 466 475 private String findPatternStringValue(String pattern, String key) 476 throws CommandException 477 { 478 String value = null; 479 try 480 { 481 if (pattern.equals(String.valueOf(OPERAND_START_CHAR))) 482 { 483 if (operands.size() > 0) 484 value = (String )getOperands().get(Integer.parseInt(key)-1); 485 } 486 else if (pattern.equals(String.valueOf(VARIABLE_START_CHAR))) 487 value = getOption(key); 488 } 489 catch(Exception e) 490 { 491 throw new CommandException(e); 492 } 493 return value; 497 } 498 499 500 509 private String prepareStringForAppend(String str) 510 { 511 final String strTmp = escapeTheEscape(str); 512 return addEscapeToLiteral(strTmp); 513 } 514 515 516 523 private String escapeTheEscape(String strToEscape) 524 { 525 StringBuffer strbuf = new StringBuffer (); 526 int previousIndex =0; 527 int index =strToEscape.indexOf("\\"); 528 while (index > -1) 529 { 530 if (index<strToEscape.length()-1 && strToEscape.charAt(index+1) == '$') 531 strbuf.append(strToEscape.substring(previousIndex, index)); 532 else 533 strbuf.append(strToEscape.substring(previousIndex, index)+"\\\\"); 534 previousIndex = index+1; 535 index = strToEscape.indexOf("\\", index+1); 536 } 537 strbuf.append(strToEscape.substring(previousIndex)); 538 return strbuf.toString(); 539 } 540 541 542 554 private String addEscapeToLiteral(String strToAdd) 555 { 556 StringBuffer strbuf = new StringBuffer (); 557 int previousIndex =0; 558 int index = strToAdd.indexOf('$'); 559 while (index > -1) 560 { 561 strbuf.append(strToAdd.substring(previousIndex, index)+"\\$"); 562 previousIndex = index+1; 563 index = strToAdd.indexOf('$', index+1); 564 } 565 strbuf.append(strToAdd.substring(previousIndex)); 566 return strbuf.toString(); 567 568 } 569 570 571 578 protected int getDelimeterIndex(String searchStr, String delimeter, 579 int fromIndex) 580 { 581 return searchStr.indexOf(delimeter, fromIndex); 582 } 583 } 584 | Popular Tags |