1 package org.sapia.console; 2 3 import java.io.File ; 4 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 11 102 public class CmdLine implements Cloneable { 103 static final char SPACE = ' '; 104 static final char QUOTE = '\"'; 105 static final char ESCAPE = '\\'; 106 static final char DASH = '-'; 107 private List _elems = new ArrayList (); 108 private Map _options = new HashMap (); 109 private int _index; 110 111 public CmdLine() { 112 } 113 114 private CmdLine(List elems, Map options) { 115 _elems = elems; 116 _options = options; 117 } 118 119 124 public CmdLine addArg(String name) { 125 return addArg(new Arg(name)); 126 } 127 128 134 public CmdLine addArg(int index, String name) { 135 return addArg(index, new Arg(name)); 136 } 137 138 143 public CmdLine addArg(Arg arg) { 144 _elems.add(arg); 145 146 return this; 147 } 148 149 155 public CmdLine addArg(int index, Arg arg) { 156 _elems.add(index, arg); 157 158 return this; 159 } 160 161 167 public CmdLine addOpt(String name, String value) { 168 Option opt = new Option(name, value); 169 170 return addOpt(opt); 171 } 172 173 178 public CmdLine addOpt(Option opt) { 179 _elems.add(opt); 180 _options.put(opt.getName(), opt); 181 182 return this; 183 } 184 185 191 public CmdElement get(int i) { 192 return (CmdElement) _elems.get(i); 193 } 194 195 199 public CmdElement last() { 200 return (CmdElement) _elems.get(_elems.size() - 1); 201 } 202 203 207 public CmdElement first() { 208 return (CmdElement) _elems.get(0); 209 } 210 211 217 public boolean hasNext() { 218 return _index < _elems.size(); 219 } 220 221 227 public CmdElement next() { 228 return (CmdElement) _elems.get(_index++); 229 } 230 231 246 public boolean isNextArg() { 247 return _elems.get(_index) instanceof Arg; 248 } 249 250 259 public Arg assertNextArg() throws InputException { 260 if (!hasNext() || !isNextArg()) { 261 throw new InputException("argument expected"); 262 } 263 264 return (Arg) _elems.get(_index++); 265 } 266 267 282 public CmdLine filterArgs() { 283 List args = new ArrayList (); 284 285 for (int i = 0; i < _elems.size(); i++) { 286 if (_elems.get(i) instanceof Arg) { 287 args.add((Arg) _elems.get(i)); 288 } 289 } 290 291 return new CmdLine(args, _options); 292 } 293 294 304 public Arg assertNextArg(String [] argNames) throws InputException { 305 if (!hasNext() || !isNextArg()) { 306 throw new InputException("argument expected"); 307 } 308 309 Arg arg = (Arg) _elems.get(_index++); 310 311 for (int i = 0; i < argNames.length; i++) { 312 if (arg.getName().equals(argNames[i])) { 313 return arg; 314 } 315 } 316 317 StringBuffer buf = new StringBuffer (); 318 319 for (int i = 0; i < argNames.length; i++) { 320 buf.append(argNames[i]); 321 322 if (i < (argNames.length - 1)) { 323 buf.append(" | "); 324 } 325 } 326 327 throw new InputException("one of the following arguments expected: " + 328 buf.toString() + "."); 329 } 330 331 338 public boolean isNextOption() { 339 return _elems.get(_index) instanceof Option; 340 } 341 342 354 public Option assertOption(String optName, boolean hasValue) 355 throws InputException { 356 if (!containsOption(optName, hasValue)) { 357 throw new InputException("option '" + optName + "' not specified."); 358 } 359 360 return (Option) _options.get(optName); 361 } 362 363 375 public Option assertOption(String optName, String value) 376 throws InputException { 377 Option opt = assertOption(optName, true); 378 379 if (!opt.getValue().equals(value)) { 380 throw new InputException("option '" + optName + "' expects value '" + 381 value + "'."); 382 } 383 384 return opt; 385 } 386 387 399 public Option assertOption(String optName, String [] values) 400 throws InputException { 401 Option opt = assertOption(optName, true); 402 403 for (int i = 0; i < values.length; i++) { 404 if (opt.getValue().equals(values[i])) { 405 return opt; 406 } 407 } 408 409 StringBuffer buf = new StringBuffer (); 410 411 for (int i = 0; i < values.length; i++) { 412 buf.append(values[i]); 413 414 if (i < (values.length - 1)) { 415 buf.append(" | "); 416 } 417 } 418 419 throw new InputException("option '" + optName + 420 "' expects one of the following values: " + buf.toString() + "."); 421 } 422 423 433 public boolean containsOption(String name, boolean hasValue) { 434 Option opt = (Option) _options.get(name); 435 436 if (opt == null) { 437 return false; 438 } 439 440 if ((opt.getValue() == null) && hasValue) { 441 return false; 442 } 443 444 return true; 445 } 446 447 455 public boolean containsOption(String name, String value) { 456 Option opt = (Option) _options.get(name); 457 458 if (opt == null) { 459 return false; 460 } 461 462 return (opt.getValue() != null) && opt.getValue().equals(value); 463 } 464 465 468 public void reset() { 469 _index = 0; 470 } 471 472 477 public void addElement(CmdElement elem) { 478 if (elem instanceof Option) { 479 addOpt((Option) elem); 480 } else if (elem instanceof Arg) { 481 addArg((Arg) elem); 482 } else { 483 throw new IllegalArgumentException ("argument must be an instance of " + 484 Option.class.getName() + " or " + Arg.class.getName()); 485 } 486 } 487 488 491 public CmdElement chop() { 492 CmdElement elem = (CmdElement) _elems.remove(0); 493 494 if (_index > 0) { 495 _index--; 496 } 497 498 return elem; 499 } 500 501 505 public Arg chopArg() { 506 Object elem = _elems.remove(0); 507 508 if (_index > 0) { 509 _index--; 510 } 511 512 if (elem instanceof Arg) { 513 return (Arg) elem; 514 } else { 515 return null; 516 } 517 } 518 519 522 public int size() { 523 return _elems.size(); 524 } 525 526 532 public String [] toArray() { 533 List all = new ArrayList (); 534 Option opt; 535 String name; 536 String value; 537 538 for (int i = 0; i < _elems.size(); i++) { 539 if (_elems.get(i) instanceof Arg) { 540 Arg arg = (Arg) _elems.get(i); 541 542 if (arg.getName() != null) { 543 all.add(arg.getName()); 544 } 545 } else { 546 opt = (Option) _elems.get(i); 547 548 if (opt.getName() != null) { 549 all.add("-" + opt.getName()); 550 551 if (opt.getValue() != null) { 552 all.add(opt.getValue()); 553 } 554 } 555 } 556 } 557 558 return (String []) all.toArray(new String [all.size()]); 559 } 560 561 568 public String [] toArrayOptionsLast() { 569 List all = new ArrayList (); 570 List options = new ArrayList (); 571 Option opt; 572 String name; 573 String value; 574 575 for (int i = 0; i < _elems.size(); i++) { 576 if (_elems.get(i) instanceof Arg) { 577 all.add(((Arg) _elems.get(i)).getName()); 578 } else { 579 opt = (Option) _elems.get(i); 580 options.add("-" + opt.getName()); 581 582 if (opt.getValue() != null) { 583 options.add(opt.getValue()); 584 } 585 } 586 } 587 588 all.addAll(options); 589 590 return (String []) all.toArray(new String [all.size()]); 591 } 592 593 600 public String [] toArrayOptionsFirst() { 601 List all = new ArrayList (); 602 List options = new ArrayList (); 603 Option opt; 604 String name; 605 String value; 606 607 for (int i = 0; i < _elems.size(); i++) { 608 if (_elems.get(i) instanceof Arg) { 609 all.add(((Arg) _elems.get(i)).getName()); 610 } else { 611 opt = (Option) _elems.get(i); 612 options.add("-" + opt.getName()); 613 614 if (opt.getValue() != null) { 615 options.add(opt.getValue()); 616 } 617 } 618 } 619 620 options.addAll(all); 621 622 return (String []) options.toArray(new String [options.size()]); 623 } 624 625 630 public Object clone() { 631 return new CmdLine(_elems, _options); 632 } 633 634 639 public String toString() { 640 CmdElement elem; 641 StringBuffer buf = new StringBuffer (); 642 643 for (int i = 0; i < _elems.size(); i++) { 644 elem = (CmdElement) _elems.get(i); 645 buf.append(elem.toString()); 646 647 if (i < (_elems.size() - 1)) { 648 buf.append(SPACE); 649 } 650 } 651 652 return buf.toString(); 653 } 654 655 661 public static CmdLine parse(String [] args) { 662 StringBuffer buf = new StringBuffer (); 663 664 for (int i = 0; i < args.length; i++) { 665 buf.append(args[i]).append(' '); 666 } 667 668 return parse(buf.toString().trim()); 669 } 670 671 677 public ExecHandle exec() { 678 CmdLineThread th = new CmdLineThread(this); 679 th.start(); 680 681 return new ExecHandle(th); 682 } 683 684 692 public ExecHandle exec(String [] env) { 693 CmdLineThread th = new CmdLineThread(this, env); 694 th.start(); 695 696 return new ExecHandle(th); 697 } 698 699 708 public ExecHandle exec(File processDir, String [] env) { 709 CmdLineThread th = new CmdLineThread(this, processDir, env); 710 th.start(); 711 712 return new ExecHandle(th); 713 } 714 715 721 public static CmdLine parse(String line) { 722 CmdLine cmd = new CmdLine(); 723 724 char c; 725 StringBuffer buf = new StringBuffer (); 726 boolean quote = false; 727 728 for (int i = 0; i < line.length(); i++) { 729 c = line.charAt(i); 730 731 if (c == SPACE) { 732 if (quote) { 733 buf.append(c); 734 } else { 735 addElem(cmd, buf); 736 } 737 } else if (c == QUOTE) { 738 if ((i > 0) && (line.charAt(i - 1) == ESCAPE)) { 739 buf.append(c); 740 } else if (quote) { 741 quote = false; 742 } else { 743 quote = true; 744 } 745 } else { 746 buf.append(c); 747 } 748 } 749 750 addElem(cmd, buf); 751 752 return cmd; 753 } 754 755 private static void addElem(CmdLine cmd, StringBuffer buf) { 756 if ((buf.length() > 0) && (buf.toString().charAt(0) == DASH)) { 757 StringBuffer buf2 = new StringBuffer (buf.toString().substring(1)); 758 Option opt = new Option(buf2.toString()); 759 cmd.addOpt(opt); 760 } else if (buf.length() > 0) { 761 if (cmd.size() > 0) { 762 if (cmd.last() instanceof Option) { 763 Option opt = (Option) cmd.last(); 764 765 if (opt.getValue() == null) { 766 opt.setValue(buf.toString()); 767 } else { 768 cmd.addArg(new Arg(buf.toString())); 769 } 770 } else { 771 cmd.addArg(new Arg(buf.toString())); 772 } 773 } else { 774 cmd.addArg(new Arg(buf.toString())); 775 } 776 } 777 778 buf.delete(0, buf.length()); 779 } 780 } 781 | Popular Tags |