Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

com/ohrasys/app/AppOption.java

Go to the documentation of this file.
00001 /* Copyright (C) 2004 Thomas N. Valine
00002  * tvaline@users.sourceforge.net
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017  * 02111-1307, USA. */
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++] + " " /*NOI18N*/);
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++] + " " /*NOI18N*/);
00207           }
00208           template.setValue(values.trim());
00209           options.add(template);
00210         }
00211       } // end while
00212     } catch(Exception e) {
00213       throw new AppException();
00214     }
00215 
00216     return ((AppOption[])options.toArray(new AppOption[options.size()]));
00217   } // end method parseCLArgs
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" /*NOI18N*/ +
00262           ((value.length() != 0) ? (":" /*NOI18N*/ + value) : ("" /*NOI18N*/)));
00263     } else {
00264       return (name +
00265           ((value.length() != 0) ? (":" /*NOI18N*/ + value) : ("" /*NOI18N*/)));
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 } // end class AppOption
00327 
00328 
00329 /* This material is distributed under the GNU General Public License.
00330  * For more information please go to http://www.gnu.org/copyleft/gpl.html */

Generated on Tue Nov 1 23:43:33 2005 for JavaBasicApplicationFrameworkAPI(JBAF) by  doxygen 1.4.2