00001 package commandlineprocessing;
00002
00011 public class OptionData {
00012
00013 private final static String CLASS = "OptionData";
00014
00015 private Options.Prefix prefix = null;
00016 private String key = null;
00017 private boolean detail = false;
00018 private Options.Separator separator = null;
00019 private boolean value = false;
00020 private Options.Multiplicity multiplicity = null;
00021 private java.util.regex.Pattern pattern = null;
00022 private int counter = 0;
00023 private java.util.ArrayList<String> values = null;
00024 private java.util.ArrayList<String> details = null;
00025
00030 OptionData(Options.Prefix prefix,
00031 String key,
00032 boolean detail,
00033 Options.Separator separator,
00034 boolean value,
00035 Options.Multiplicity multiplicity) {
00036
00037 if (prefix == null) throw new IllegalArgumentException(CLASS + ": prefix may not be null");
00038 if (key == null) throw new IllegalArgumentException(CLASS + ": key may not be null");
00039 if (separator == null) throw new IllegalArgumentException(CLASS + ": separator may not be null");
00040 if (multiplicity == null) throw new IllegalArgumentException(CLASS + ": multiplicity may not be null");
00041
00042
00043
00044 this.prefix = prefix;
00045 this.key = key;
00046 this.detail = detail;
00047 this.separator = separator;
00048 this.value = value;
00049 this.multiplicity = multiplicity;
00050
00051
00052
00053 if (value) {
00054 if (separator == Options.Separator.BLANK) {
00055 if (detail) {
00056 pattern = java.util.regex.Pattern.compile(prefix.getName() + key + "((\\w|\\.)+)$");
00057 } else {
00058 pattern = java.util.regex.Pattern.compile(prefix.getName() + key + "$");
00059 }
00060 } else {
00061 if (detail) {
00062 pattern = java.util.regex.Pattern.compile(prefix.getName() + key + "((\\w|\\.)+)" + separator.getName() + "(.+)$");
00063 } else {
00064 pattern = java.util.regex.Pattern.compile(prefix.getName() + key + separator.getName() + "(.+)$");
00065 }
00066 }
00067 } else {
00068 pattern = java.util.regex.Pattern.compile(prefix.getName() + key + "$");
00069 }
00070
00071
00072
00073 if (value) {
00074 values = new java.util.ArrayList<String>();
00075 if (detail)
00076 details = new java.util.ArrayList<String>();
00077 }
00078
00079 }
00080
00087 Options.Prefix getPrefix() {
00088 return prefix;
00089 }
00090
00097 String getKey() {
00098 return key;
00099 }
00100
00107 boolean useDetail() {
00108 return detail;
00109 }
00110
00117 Options.Separator getSeparator() {
00118 return separator;
00119 }
00120
00127 boolean useValue() {
00128 return value;
00129 }
00130
00137 Options.Multiplicity getMultiplicity() {
00138 return multiplicity;
00139 }
00140
00147 java.util.regex.Pattern getPattern() {
00148 return pattern;
00149 }
00150
00157 public int getResultCount() {
00158 if (value) {
00159 return values.size();
00160 } else {
00161 return counter;
00162 }
00163 }
00164
00177 public String getResultValue(int index) {
00178 if (!value) return null;
00179 if (index < 0 || index >= getResultCount()) throw new IllegalArgumentException(CLASS + ": illegal value for index");
00180 return values.get(index);
00181 }
00182
00195 public String getResultDetail(int index) {
00196 if (!detail) return null;
00197 if (index < 0 || index >= getResultCount()) throw new IllegalArgumentException(CLASS + ": illegal value for index");
00198 return details.get(index);
00199 }
00200
00205 void addResult(String valueData, String detailData) {
00206 if (value) {
00207 if (valueData == null) throw new IllegalArgumentException(CLASS + ": valueData may not be null");
00208 values.add(valueData);
00209 if (detail) {
00210 if (detailData == null) throw new IllegalArgumentException(CLASS + ": detailData may not be null");
00211 details.add(detailData);
00212 }
00213 }
00214 counter++;
00215 }
00216
00224 public String toString() {
00225
00226 StringBuffer sb = new StringBuffer();
00227
00228 sb.append("Prefix : ");
00229 sb.append(prefix);
00230 sb.append('\n');
00231 sb.append("Key : ");
00232 sb.append(key);
00233 sb.append('\n');
00234 sb.append("Detail : ");
00235 sb.append(detail);
00236 sb.append('\n');
00237 sb.append("Separator : ");
00238 sb.append(separator);
00239 sb.append('\n');
00240 sb.append("Value : ");
00241 sb.append(value);
00242 sb.append('\n');
00243 sb.append("Multiplicity: ");
00244 sb.append(multiplicity);
00245 sb.append('\n');
00246 sb.append("Pattern : ");
00247 sb.append(pattern);
00248 sb.append('\n');
00249
00250 sb.append("Results : ");
00251 sb.append(counter);
00252 sb.append('\n');
00253 if (value) {
00254 if (detail) {
00255 for (int i = 0; i < values.size(); i++) {
00256 sb.append(details.get(i));
00257 sb.append(" / ");
00258 sb.append(values.get(i));
00259 sb.append('\n');
00260 }
00261 } else {
00262 for (int i = 0; i < values.size(); i++) {
00263 sb.append(values.get(i));
00264 sb.append('\n');
00265 }
00266 }
00267 }
00268
00269 return sb.toString();
00270
00271 }
00272
00273 }
00274
00275