001: package org.incava.jagol;
002:
003: import java.io.*;
004: import java.util.*;
005: import org.incava.lang.StringExt;
006:
007: /**
008: * Represents a list of objects that comprise this option.
009: */
010: public class ListOption extends Option {
011: private List value;
012:
013: /**
014: * Creates the option.
015: */
016: public ListOption(String longName, String description) {
017: this (longName, description, new ArrayList());
018: }
019:
020: /**
021: * Creates the option, with a default list.
022: */
023: public ListOption(String longName, String description, List value) {
024: super (longName, description);
025: this .value = value;
026: }
027:
028: /**
029: * Returns the value. This is empty by default.
030: */
031: public List getValue() {
032: return value;
033: }
034:
035: /**
036: * Sets the value.
037: */
038: public void setValue(List value) {
039: this .value = value;
040: }
041:
042: /**
043: * Sets the value from the string, for a list type. Assumes whitespace or
044: * comma delimiter
045: */
046: public void setValue(String value) throws InvalidTypeException {
047: tr.Ace.log("value: '" + value + "'");
048: parse(value);
049: }
050:
051: /**
052: * Sets from a list of command-line arguments. Returns whether this option
053: * could be set from the current head of the list. Assumes whitespace or
054: * comma delimiter.
055: */
056: public boolean set(String arg, List args) throws OptionException {
057: tr.Ace.log("arg: " + arg + "; args: " + args);
058:
059: if (arg.equals("--" + longName)) {
060: tr.Ace.log("matched long name");
061:
062: if (args.size() == 0) {
063: throw new InvalidTypeException(longName
064: + " expects following argument");
065: } else {
066: String value = (String) args.remove(0);
067: setValue(value);
068: }
069: } else if (arg.startsWith("--" + longName + "=")) {
070: tr.Ace.log("matched long name + equals");
071:
072: // args.remove(0);
073: int pos = ("--" + longName + "=").length();
074: tr.Ace.log("position: " + pos);
075: if (pos >= arg.length()) {
076: throw new InvalidTypeException(longName
077: + " expects argument");
078: } else {
079: String value = arg.substring(pos);
080: setValue(value);
081: }
082: } else if (shortName != 0 && arg.equals("-" + shortName)) {
083: tr.Ace.log("matched short name");
084:
085: if (args.size() == 0) {
086: throw new InvalidTypeException(shortName
087: + " expects following argument");
088: } else {
089: String value = (String) args.remove(0);
090: setValue(value);
091: }
092: } else {
093: tr.Ace.log("not a match");
094: return false;
095: }
096: return true;
097: }
098:
099: /**
100: * Parses the value into the value list. If subclasses want to convert the
101: * string to their own data type, override the <code>convert</code> method.
102: *
103: * @see ListOption#convert(String)
104: */
105: protected void parse(String str) throws InvalidTypeException {
106: List list = StringExt.listify(str);
107: Iterator it = list.iterator();
108: while (it.hasNext()) {
109: String s = (String) it.next();
110: if (!s.equals("+=")) {
111: value.add(convert(s));
112: }
113: }
114: }
115:
116: /**
117: * Returns the string, possibly converted to a different Object type.
118: * Subclasses can convert the string to their own data type.
119: */
120: protected Object convert(String str) throws InvalidTypeException {
121: return str;
122: }
123:
124: public String toString() {
125: StringBuffer buf = new StringBuffer();
126: Iterator it = value.iterator();
127: boolean isFirst = true;
128: while (it.hasNext()) {
129: if (isFirst) {
130: isFirst = false;
131: } else {
132: buf.append(", ");
133: }
134: buf.append(it.next());
135: }
136: return buf.toString();
137: }
138:
139: }
|