1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
|
/*
* Copyright (C) 2001-2013 Michael Fuchs
*
* This file is part of herold.
*
* herold is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* herold is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with herold. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dbdoclet.option;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.ResourceBundle;
import org.dbdoclet.service.ResourceServices;
/**
* Die Klasse <code>OptionList</code> verwaltet die bekannten Optionen und
* wertet die Kommandozeile aus.
*
* <div id="example_optionlist" title="Beispiel für OptionList">
*
* <pre>
*
* OptionList options = new OptionList(args);
* StringOption opt = new StringOption("program-name", "n");
* opt.setMediumName("name");
* opt.setPropertyName("program.name");
* opt.setDefault("PROGRAM");
* options.add(opt);
*
* if (options.validate() == false) {
* fail(options.getError());
* }
*
* options.setProperties(properties);
* println("Optionen:");
* println(options.list());
*
* </pre>
*
* </div>
*
* @author <a href="mailto:mfuchs@unico-consulting.com">Michael Fuchs</a>
* @version 1.0
*/
public class OptionList implements Iterable<Option<?>> {
private static ResourceBundle res = Option.getResourceBundle();
private ArrayList<String> remainingArgs;
private ArrayList<String> args;
private ArrayList<Option<?>> optList;
private HashMap<String, Option<?>> optMap;
private HashMap<String, Option<?>> deprecatedPropertiesMap;
private String error = "";
/**
* Erzeugt eine neue Instanz der Klasse <code>OptionList</code> ohne
* Argumente.
*/
public OptionList() {
String[] args = new String[0];
init(args, 0);
}
/**
* Erzeugt eine neue Instanz der Klasse <code>OptionList</code>.
*
* @param cmdline
* <code>String[][]</code>
*/
public OptionList(String[][] cmdline) {
String[] args = Arrays.stream(cmdline)
.flatMap(arr -> Arrays.stream(arr))
.toArray(String[]::new);
init(args, 0);
}
/**
* Erzeugt eine neue Instanz der Klasse <code>OptionList</code>.
*
* @param cmdline
* a <code>String[]</code> value
*/
public OptionList(String[] cmdline) {
if (cmdline == null)
throw new IllegalArgumentException("Variable args is null!");
init(cmdline, 0);
}
/**
* Erzeugt eine neue Instanz der Klasse <code>OptionList</code>.
*
* Der Parameter <code>offset</code> gibt an ab welchem Element der
* Kommandozeile die Argumente ausgewertet werden sollen. Damit ist möglich
* den Parametern Unterbefehle voranzustellen, z.B. wie in <code>cocs update
* --force</code>.
*
* @param cmdline
* a <code>String[]</code> value
*/
public OptionList(String[] cmdline, int offset) {
if (cmdline == null) {
throw new IllegalArgumentException(
"The argument cmdline may not be null!");
}
if (offset < 0) {
throw new IllegalArgumentException(
"The argument offset must be greater than 0!");
}
if (offset > cmdline.length) {
throw new IllegalArgumentException(
"The argument offset is greater than the lehgth of the command line!");
}
init(cmdline, offset);
}
/**
* Die Methode <code>getError</code> liefert die letzte Fehlermeldung an den
* Aufrufer zurück.
*
* @return <code>String</code>
*/
public String getError() {
if (error == null) {
error = "";
}
return String.join(" ", args) + "\n\n" + error;
}
/**
* Die Methode <code>findOption</code> sucht nach der angegebenen Option in
* der aktuellen Liste von Optionen.
*
* Falls die Option nicht gefunden werden kann, wird <code>null</code>
* zurückgegeben.
*
* @param option
* Die <code>Option</code> nach der gesucht wird.
* @return Die gefundene <code>Option</code> oder <code>null</code>.
*/
public Option<?> findOption(Option<?> option) {
Object obj;
int index = optList.indexOf(option);
if (index != -1) {
obj = optList.get(index);
if (obj != null && obj instanceof Option) {
return (Option<?>) obj;
}
}
return null;
}
/**
* The method <code>findOption</code> tries to find the given option by
* name. If the option can not be found, null is returned.
*
* @param name
* a <code>String</code> value
* @return The option or null if no option can be found.
*/
public Option<?> findOption(String name) {
if (name == null) {
throw new IllegalArgumentException(
" The argument name may not be null!");
}
return optMap.get(name);
}
public Option<?> findExistingOption(Option<?> option) {
if (option == null) {
throw new IllegalArgumentException(
" The argument option may not be null!");
}
Option<?> existing;
existing = optMap.get(option.getLongName());
if (existing != null) {
return existing;
}
existing = optMap.get(option.getMediumName());
if (existing != null) {
return existing;
}
existing = optMap.get(option.getShortName());
if (existing != null) {
return existing;
}
existing = optMap.get(option.getPropertyName());
if (existing != null) {
return existing;
}
throw new OptionException("Can't find existing option!");
}
/**
* The method <code>getOption</code> tries to find the given option by name.
* If the option can not be found, an OptionException is thrown.
*
* @param name
* a <code>String</code> value
* @return The option.
*/
public Option<?> getOption(String name) throws OptionException {
if (name == null) {
throw new IllegalArgumentException(
"The argument name may not be null!");
}
Option<?> option = optMap.get(name);
if (option == null) {
String msg = MessageFormat.format(
ResourceServices.getString(res, "C_ERROR_OPTION_UNKNOWN"),
name);
throw new OptionException(msg);
}
return option;
}
public BooleanOption getBooleanOption(String name) {
if (name == null) {
throw new IllegalArgumentException(
"The argument name may not be null!");
}
Option<?> option = getOption(name);
if (option instanceof BooleanOption) {
return (BooleanOption) option;
} else {
String msg = MessageFormat.format(ResourceServices.getString(res,
"C_ERROR_OPTION_INVALID_TYPE"), name, "boolean", option
.getClass().getName());
throw new OptionException(msg);
} // end of else
}
public void add(Option<?> option) throws OptionException {
if (option == null) {
throw new IllegalArgumentException(
" The argument option may not be null!");
}
if (option.isValid() == false) {
throw new OptionException(ResourceServices.getString(res,
"C_ERROR_INVALID_OPTION") + option.getUniqueName());
}
if (exists(option)) {
Option<?> existing = findExistingOption(option);
throw new OptionException(MessageFormat.format(ResourceServices
.getString(res, "C_ERROR_OPTION_ALREADY_IN_USE"), option
.getFQName(), existing.getFQName()));
}
optList.add(option);
Arrays.stream(option.getNames()).forEach(n -> optMap.put(n, option));
}
/**
* Die Methode <code>getRemainingArgs</code> liefert die nach der
* Validierung überzähligen Argumente. Also diejenigen Argumente, die nicht
* definiert waren.
*
* @return <code>String[]</code>
*/
public String[] getRemainingArgs() {
if (remainingArgs == null) {
return new String[0];
}
return remainingArgs.stream().toArray(String[]::new);
}
/**
* Describe <code>getString</code> method here.
*
* @param name
* a <code>String</code> value
* @return a <code>String</code> value
*/
public String getString(String name) {
if (name == null)
throw new IllegalArgumentException("Variable name is null!");
Option<?> option = optMap.get(name);
if (option == null) {
return "";
}
if (option.getValue() == null) {
return null;
}
return option.getValue().toString();
}
/**
* Describe <code>getString</code> method here.
*
* @param name
* a <code>String</code> value
* @param deflt
* a <code>String</code> value
* @return a <code>String</code> value
*/
public String getString(String name, String deflt) {
if (name == null)
throw new IllegalArgumentException("Variable name is null!");
Option<?> option = optMap.get(name);
if (option == null || option.isUnset())
return deflt;
if (option.getValue() == null) {
return deflt;
} else {
return option.getValue().toString();
}
}
public void addDeprecatedPropertyAlias(Option<?> option, String alias)
throws OptionException {
if (option == null) {
throw new IllegalArgumentException(
"The argument option may not be null!");
}
if (alias == null || alias.length() == 0 || alias.equals("!")) {
throw new IllegalArgumentException(
"The argument alias is invalid: " + alias);
}
if (deprecatedPropertiesMap == null) {
throw new IllegalStateException(
" The field deprecatedPropertiesMap may not be null!");
}
String name = alias;
if (name.startsWith("!")) {
name = name.substring(1);
} // end of if (name.startsWith("!"))
Option<?> opt = optMap.get(name);
if (opt != null && opt.equals(option) == false) {
throw new OptionException(MessageFormat.format(ResourceServices
.getString(res, "C_ERROR_OPTION_ALREADY_IN_USE"), name));
}
deprecatedPropertiesMap.put(alias, option);
}
public boolean setProperties(String fname) throws FileNotFoundException,
IOException {
if (fname == null) {
throw new IllegalArgumentException(
" The argument fname may not be null!");
}
Properties props = new Properties();
props.load(new FileInputStream(fname));
return setProperties(props);
}
public boolean setProperties(Properties props) throws OptionException {
if (props == null) {
throw new IllegalArgumentException(
" The argument props may not be null!");
}
Object obj;
Option<?> option;
String name;
String value;
String buffer = "";
// props.forEach(Property p -> System.out.println());
for (Iterator<Object> i = props.keySet().iterator(); i.hasNext();) {
obj = i.next();
if (obj instanceof String) {
name = (String) obj;
value = props.getProperty(name);
if (value == null) {
continue;
}
option = optMap.get(name);
if (option == null) {
option = deprecatedPropertiesMap.get(name);
if (option != null) {
buffer += MessageFormat.format(ResourceServices
.getString(res, "C_WARN_PROPERTY_DEPRECATED"),
name, option.getPropertyName())
+ "\n";
} else {
option = deprecatedPropertiesMap.get("!" + name);
if (option != null && (option instanceof BooleanOption)) {
value = value.trim();
boolean flag = false;
if (value.equals("1")
|| value.equalsIgnoreCase("yes")
|| value.equalsIgnoreCase("true")
|| value.equalsIgnoreCase("ja")
|| value.equalsIgnoreCase("wahr")) {
flag = true;
}
((BooleanOption) option).setValue(flag);
continue;
}
}
}
if (option == null) {
buffer += MessageFormat.format(ResourceServices.getString(
res, "C_ERROR_PROPERTY_UNKNOWN"), name)
+ "\n";
continue;
}
if (option != null && option.isUnset()) {
option.setValueFromString(value);
}
}
}
if (buffer != null && buffer.length() > 0) {
error = buffer;
return false;
} else {
return true;
}
}
public boolean validate() {
return validate(false);
}
/**
* Die Methode <code>validate</code> validiert die Argumente.
*
* Das Validieren prüft die Argumente auf ihre syntaktische Korrektheit.
* Zusätzlich werden die Optionen selbst bearbeitet. Gefundene Optionen
* werden als vorhanden markiert und mit den Werten aus den Argumenten
* initialisert. Überzählige Argumente werden in einem Container namens
* <code>remainingArgs</code> gespeichert. Je nach Wert des Parameters
* <code>ignoreUnknownOptions</code> führen überzählige Argumente zu einer
* Fehlermeldung oder sie können mit Hilfe der Methode
* <code>getRemainingArgs</code> ermittelt werden. Zusätzliche Argumente
* sind sinnvoll, wenn den Optionen z.B. eine URL- oder Pfadangabe folgt,
* welche nicht von einer Option eingeleitet wird.
*
* <div id="example_optionlist_validate"
* title="Kommandozeile mit überzähligen Argumenten">
*
* <pre>
* cocs checkout --verbose file:///repository
* </pre>
* <p>
* In diesem Beispiel wird die URL nicht von einer Option begleitet. Sie
* gilt als "überzähliges Argument".
* </p>
* </div>
*
* @param ignoreUnknownOptions
* <code>boolean</code>
* @return <code>boolean</code>
* @exception OptionException
*/
public boolean validate(boolean ignoreUnknownOptions)
throws OptionException {
Iterator<Option<?>> iterator = optList.iterator();
Option<?> option;
String arg;
String str;
String buffer = "";
int index = -1;
int counter = 0;
ArrayList<String> argList = new ArrayList<String>(args);
while (iterator.hasNext()) {
option = iterator.next();
option.isUnset(true);
index = searchArgs(argList, option);
if (index == -1 && option.isRequired()) {
buffer += MessageFormat.format(ResourceServices.getString(res,
"C_ERROR_OPTION_REQUIRED"), option.getUniqueName())
+ "\n";
continue;
}
counter = 0;
while (index != -1) {
if (option.hasArgument()) {
if (index == argList.size() - 1) {
buffer += MessageFormat.format(ResourceServices
.getString(res, "C_ERROR_OPTION_NEEDS_ARG"),
option.getUniqueName())
+ "\n";
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
arg = argList.get(index + 1);
if (arg.startsWith("-")) {
buffer += MessageFormat.format(ResourceServices
.getString(res, "C_ERROR_OPTION_NEEDS_ARG"),
option.getUniqueName())
+ "\n";
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
if (option instanceof SelectOption) {
SelectOption selopt = (SelectOption) option;
if (selopt.checkArgument(arg) == false) {
buffer += MessageFormat.format(ResourceServices
.getString(res,
"C_ERROR_OPTION_INVALID_VALUE"),
option.getUniqueName(), arg, selopt
.getListAsString())
+ "\n";
argList.remove(index + 1);
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
}
if (option instanceof PathOption) {
PathOption pathopt = (PathOption) option;
if (pathopt.checkArgument(arg) == false) {
buffer += MessageFormat.format(ResourceServices
.getString(res,
"C_ERROR_OPTION_INVALID_PATH"),
option.getUniqueName(), arg, pathopt
.getInvalidPathElement())
+ "\n";
argList.remove(index + 1);
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
}
if (option instanceof FileOption) {
FileOption fileopt = (FileOption) option;
if (fileopt.checkArgument(arg) == false) {
buffer += MessageFormat.format(ResourceServices
.getString(res,
"C_ERROR_OPTION_INVALID_FILE"),
option.getUniqueName(), arg)
+ "\n";
argList.remove(index + 1);
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
}
if (option instanceof DirectoryOption) {
DirectoryOption diropt = (DirectoryOption) option;
if (diropt.checkArgument(arg) == false) {
buffer += MessageFormat
.format(ResourceServices.getString(res,
"C_ERROR_OPTION_INVALID_DIRECTORY"),
option.getUniqueName(), arg)
+ "\n";
argList.remove(index + 1);
argList.remove(index);
index = searchArgs(argList, option);
continue;
}
}
if (option.isLowerCase()) {
arg = arg.toLowerCase();
}
if (option.isUnset() || option.isUnique()) {
option.setValueFromString(arg);
} else {
option.addValueFromString(arg);
}
option.isPresent(true);
argList.remove(index + 1);
argList.remove(index);
/* This option can have multiple arguments */
if (option.isUnique() == false) {
int i;
for (i = 0; i < argList.size(); i++) {
arg = argList.get(i);
if (arg.startsWith("-") == false) {
option.addValueFromString(arg);
} else {
break;
}
}
for (int j = 0; j < i; j++) {
argList.remove(0);
}
}
} else {
String value = "true";
if (index + 1 < argList.size()) {
String next = argList.get(index + 1);
if (next.startsWith("-") == false) {
value = next;
argList.remove(index + 1);
}
}
option.setValueFromString(value);
option.isPresent(true);
argList.remove(index);
}
// look for more appearances of this option.
index = searchArgs(argList, option);
if (option.isUnique() && index != -1 && counter == 0) {
buffer += MessageFormat.format(ResourceServices.getString(
res, "C_ERROR_OPTION_WAS_FOUND_MORE_THAN_ONCE"),
option.getUniqueName())
+ "\n";
}
counter++;
}
}
if (argList.size() > 0 && ignoreUnknownOptions == false) {
str = "";
for (int i = 0; i < argList.size(); i++) {
str += argList.get(i) + " ";
}
buffer += MessageFormat.format(
ResourceServices.getString(res, "C_ERROR_OPTION_UNKNOWN"),
str) + "\n";
}
remainingArgs = new ArrayList<String>(argList);
if (buffer != null && buffer.length() > 0) {
error = buffer;
return false;
} else {
return true;
}
}
public void write(PrintWriter writer) {
Option<?> option;
String name;
Object[] options = optList.toArray();
Arrays.sort(options);
for (int i = 0; i < options.length; i++) {
option = (Option<?>) options[i];
name = option.getPropertyName();
if (option.getValue() != null) {
if (name != null && name.length() > 0) {
writer.println(option.getPropertyName() + "="
+ escape(option.getValue().toString()));
}
}
}
}
public Iterator<Option<?>> iterator() {
return optList.iterator();
}
/**
* Describe <code>list</code> method here.
*
* @return a <code>String</code> value
*/
public String list() {
StringBuffer buffer = new StringBuffer();
Object[] options = optList.toArray();
Arrays.sort(options);
for (int i = 0; i < options.length; i++) {
buffer.append(((Option<?>) options[i]).toString() + "\n");
}
return buffer.toString();
}
/**
* Describe <code>init</code> method here.
*
* @param cmdline
* a <code>String[]</code> value
*/
private void init(String[] cmdline, int offset) {
if (cmdline == null) {
throw new IllegalArgumentException(
"The argument cmdline may not be null!");
}
if (offset < 0) {
throw new IllegalArgumentException(
"The argument offset must be greater than 0!");
}
if (offset > cmdline.length) {
throw new IllegalArgumentException(
"The argument offset is greater than the length of the command line!");
}
args = new ArrayList<String>();
String arg;
int pos;
for (int i = offset; i < cmdline.length; i++) {
arg = cmdline[i];
pos = arg.indexOf('=');
if (arg.startsWith("--") && pos > 0) {
args.add(arg.substring(0, pos));
if (pos + 1 < arg.length())
args.add(arg.substring(pos + 1));
else
args.add("");
} else {
args.add(cmdline[i]);
}
}
optList = new ArrayList<Option<?>>();
optMap = new HashMap<String, Option<?>>();
deprecatedPropertiesMap = new HashMap<String, Option<?>>();
}
/**
* Describe <code>searchArgs</code> method here.
*
* @param longName
* a <code>String</code> value
* @param shortName
* a <code>String</code> value
* @return an <code>int</code> value
*/
private int searchArgs(ArrayList<String> args, Option<?> option) {
if (args == null) {
throw new IllegalArgumentException(
" The argument args may not be null!");
}
if (option == null) {
throw new IllegalArgumentException(
" The argument option may not be null!");
}
String arg;
for (int i = 0; i < args.size(); i++) {
arg = args.get(i);
if (arg.equals("--" + option.getLongName())
|| arg.equals("-" + option.getMediumName())
|| arg.equals("-" + option.getShortName())) {
return i;
}
}
return -1;
}
/**
* Describe <code>exists</code> method here.
*
* @param longName
* a <code>String</code> value
* @param mediumName
* a <code>String</code> value
* @param shortName
* a <code>String</code> value
* @param propertyName
* a <code>String</code> value
* @return a <code>boolean</code> value
*/
private boolean exists(Option<?> lookFor) {
if (optList.contains(lookFor)) {
return true;
}
if (isOptionNameAvailable(lookFor.getLongName()) == false) {
return true;
}
if (isOptionNameAvailable(lookFor.getMediumName()) == false) {
return true;
}
if (isOptionNameAvailable(lookFor.getShortName()) == false) {
return true;
}
if (isOptionNameAvailable(lookFor.getPropertyName()) == false) {
return true;
}
return false;
}
private boolean isOptionNameAvailable(String name) {
if (name != null && name.length() > 0 && optMap.get(name) != null) {
return false;
}
return true;
}
private String escape(String str) {
if (str == null) {
return "";
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int n = (int) c;
if (c == '\r') {
continue;
}
if (n >= 0 && n < 128 && c != '\n' && c != '\\') {
buffer.append(c);
} else {
buffer.append("\\u");
String hex = Integer.toHexString(n);
for (int j = 0; j < 4 - hex.length(); j++) {
buffer.append("0");
}
buffer.append(hex);
}
}
return buffer.toString();
}
public boolean getFlag(String name, boolean def) {
Option<?> option = optMap.get(name);
if (option != null && option instanceof BooleanOption) {
return ((BooleanOption) option).getValue();
}
return def;
}
public Option<String> getTextOption(String name) {
Option<?> option = getOption(name);
if (option instanceof SelectOption) {
return (SelectOption) option;
}
if (option instanceof StringOption) {
return (StringOption) option;
}
return null;
}
}
/*
* $Log$
*/
|