00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package com.ohrasys.app;
00020 import java.util.*;
00021
00028 public class AppOption {
00030 public static final int FLAG_TYPE = 0;
00031
00033 public static final int OPTION_TYPE = 1;
00034
00036 public static final int LIST_TYPE = 2;
00037
00039 public static final int UNKNOWN = -1;
00040
00042 public static final int UNLIMITED_LENGTH = -1;
00043
00045 private int len;
00046
00048 private String name;
00049
00051 private int type;
00052
00054 private String value;
00055
00069 private AppOption(int type, String name, int numvalues) {
00070 if((type > LIST_TYPE) || (type < FLAG_TYPE)) {
00071 throw new IllegalArgumentException();
00072 }
00073 this.type = type;
00074 this.name = name;
00075 this.len = numvalues;
00076 this.value = new String();
00077 }
00078
00086 public static AppOption findListOption(AppOption options[]) {
00087 for(int i = 0;i < options.length;i++) {
00088 if(options[i].getType() == LIST_TYPE){return options[i];}
00089 }
00090
00091 return null;
00092 }
00093
00102 public static AppOption findOption(String name, AppOption options[]) {
00103 for(int i = 0;i < options.length;i++) {
00104 if(options[i].getName() == name){return options[i];}
00105 }
00106
00107 return null;
00108 }
00109
00118 public static AppOption getFlag(String name) {
00119 return new AppOption(FLAG_TYPE, name, 0);
00120 }
00121
00128 public static AppOption getList() {
00129 return new AppOption(LIST_TYPE, new String(), UNLIMITED_LENGTH);
00130 }
00131
00145 public static AppOption getOption(String name) {
00146 return new AppOption(OPTION_TYPE, name, 1);
00147 }
00148
00164 public static AppOption getOption(String name, int numvalues) {
00165 return new AppOption(OPTION_TYPE, name, numvalues);
00166 }
00167
00180 public static AppOption[] parseCLArgs(String args[], AppOption templates[])
00181 throws AppException {
00182 int i = 0;
00183 List<AppOption> options = new ArrayList<AppOption>(args.length);
00184 try {
00185 while(i < args.length) {
00186 String name = args[i++];
00187 AppOption template = findTemplate(name, templates);
00188 if(template == null){throw new AppException();}
00189 if(template.getType() == AppOption.LIST_TYPE) {
00190 if(hasMoreFlags(args, --i, templates)){throw new AppException();}
00191 else {
00192 String values = new String();
00193 while(i < args.length) {
00194 values += (args[i++] + " " );
00195 }
00196 template.setValue(values.trim());
00197 AppOption result[] = new AppOption[options.size() + 1];
00198 result = ((AppOption[])options.toArray(result));
00199 result[result.length - 1] = template;
00200
00201 return result;
00202 }
00203 } else {
00204 String values = new String();
00205 for(int j = 0;j < template.getLen();j++) {
00206 values += (args[i++] + " " );
00207 }
00208 template.setValue(values.trim());
00209 options.add(template);
00210 }
00211 }
00212 } catch(Exception e) {
00213 throw new AppException();
00214 }
00215
00216 return ((AppOption[])options.toArray(new AppOption[options.size()]));
00217 }
00218
00224 public int getLen(){return this.len;}
00225
00231 public String getName(){return this.name;}
00232
00238 public int getType(){return this.type;}
00239
00245 public String getValue(){return this.value;}
00246
00252 public void setValue(String value){this.value = value;}
00253
00259 public String toString() {
00260 if(this.type == LIST_TYPE) {
00261 return ("INT_LIST" +
00262 ((value.length() != 0) ? (":" + value) : ("" )));
00263 } else {
00264 return (name +
00265 ((value.length() != 0) ? (":" + value) : ("" )));
00266 }
00267 }
00268
00279 private static AppOption findTemplate(String name, AppOption templates[]) {
00280 boolean listAllowed = false;
00281 AppOption listOption = null;
00282 for(int i = 0;i < templates.length;i++) {
00283 if(templates[i].getName().equals(name)){return templates[i];}
00284 if(templates[i].getType() == AppOption.LIST_TYPE) {
00285 listAllowed = true;
00286 listOption = templates[i];
00287 }
00288 }
00289 if(listAllowed){return listOption;}
00290
00291 return null;
00292 }
00293
00309 private static boolean hasMoreFlags(
00310 String args[], int start,
00311 AppOption templates[]) {
00312 for(int i = start;i < args.length;i++) {
00313 AppOption template = findTemplate(args[i], templates);
00314 if(((template == null) || (template.getType() != AppOption.LIST_TYPE))) {
00315 return true;
00316 } else {
00317 if(i == (args.length - 1)){return false;}
00318 else {
00319 continue;
00320 }
00321 }
00322 }
00323
00324 return true;
00325 }
00326 }
00327
00328
00329
00330