1 16 19 20 package org.apache.xalan.xsltc.cmdline.getopt; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 26 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 27 28 29 43 public class GetOpt{ 44 public GetOpt(String [] args, String optString){ 45 theOptions = new ArrayList (); 46 int currOptIndex = 0; 47 theCmdArgs = new ArrayList (); 48 theOptionMatcher = new OptionMatcher(optString); 49 for(int i=0; i<args.length; i++){ 51 String token = args[i]; 52 int tokenLength = token.length(); 53 if(token.equals("--")){ currOptIndex = i+1; break; } 57 else if(token.startsWith("-") && tokenLength == 2){ 58 theOptions.add(new Option(token.charAt(1))); 60 } 61 else if(token.startsWith("-") && tokenLength > 2){ 62 for(int j=1; j<tokenLength; j++){ 66 theOptions.add(new Option(token.charAt(j))); 67 } 68 } 69 else if(!token.startsWith("-")){ 70 if(theOptions.size() == 0){ 73 currOptIndex = i; 74 break; } 76 else { 77 int indexoflast=0; 81 indexoflast = theOptions.size()-1; 82 Option op = (Option)theOptions.get(indexoflast); 83 char opLetter = op.getArgLetter(); 84 if(!op.hasArg() && theOptionMatcher.hasArg(opLetter)){ 85 op.setArg(token); 86 } 87 else{ 88 currOptIndex = i; 94 break; } 96 } 97 } } 100 theOptionsIterator = theOptions.listIterator(); 102 103 for(int i=currOptIndex; i<args.length; i++){ 105 String token = args[i]; 106 theCmdArgs.add(token); 107 } 108 } 109 110 111 114 public void printOptions(){ 115 for(ListIterator it=theOptions.listIterator(); it.hasNext();){ 116 Option opt = (Option)it.next(); 117 System.out.print("OPT =" + opt.getArgLetter()); 118 String arg = opt.getArgument(); 119 if(arg != null){ 120 System.out.print(" " + arg); 121 } 122 System.out.println(); 123 } 124 } 125 126 140 public int getNextOption() throws IllegalArgumentException , 141 MissingOptArgException 142 { 143 int retval = -1; 144 if(theOptionsIterator.hasNext()){ 145 theCurrentOption = (Option)theOptionsIterator.next(); 146 char c = theCurrentOption.getArgLetter(); 147 boolean shouldHaveArg = theOptionMatcher.hasArg(c); 148 String arg = theCurrentOption.getArgument(); 149 if(!theOptionMatcher.match(c)) { 150 ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_CMDLINE_OPTION_ERR, 151 new Character (c)); 152 throw (new IllegalArgumentException (msg.toString())); 153 } 154 else if(shouldHaveArg && (arg == null)) { 155 ErrorMsg msg = new ErrorMsg(ErrorMsg.CMDLINE_OPT_MISSING_ARG_ERR, 156 new Character (c)); 157 throw (new MissingOptArgException(msg.toString())); 158 } 159 retval = c; 160 } 161 return retval; 162 } 163 164 171 public String getOptionArg(){ 172 String retval = null; 173 String tmp = theCurrentOption.getArgument(); 174 char c = theCurrentOption.getArgLetter(); 175 if(theOptionMatcher.hasArg(c)){ 176 retval = tmp; 177 } 178 return retval; 179 } 180 181 190 public String [] getCmdArgs(){ 191 String [] retval = new String [theCmdArgs.size()]; 192 int i=0; 193 for(ListIterator it=theCmdArgs.listIterator(); it.hasNext();){ 194 retval[i++] = (String )it.next(); 195 } 196 return retval; 197 } 198 199 200 private Option theCurrentOption = null; 201 private ListIterator theOptionsIterator; 202 private List theOptions = null; 203 private List theCmdArgs = null; 204 private OptionMatcher theOptionMatcher = null; 205 206 212 class Option{ 214 private char theArgLetter; 215 private String theArgument = null; 216 public Option(char argLetter) { theArgLetter = argLetter; } 217 public void setArg(String arg) { 218 theArgument = arg; 219 } 220 public boolean hasArg() { return (theArgument != null); } 221 public char getArgLetter() { return theArgLetter; } 222 public String getArgument() { return theArgument; } 223 } 225 226 class OptionMatcher{ 230 public OptionMatcher(String optString){ 231 theOptString = optString; 232 } 233 public boolean match(char c){ 234 boolean retval = false; 235 if(theOptString.indexOf(c) != -1){ 236 retval = true; 237 } 238 return retval; 239 } 240 public boolean hasArg(char c){ 241 boolean retval = false; 242 int index = theOptString.indexOf(c)+1; 243 if (index == theOptString.length()){ 244 retval = false; 246 } 247 else if(theOptString.charAt(index) == ':'){ 248 retval = true; 249 } 250 return retval; 251 } 252 private String theOptString = null; 253 } } 256 | Popular Tags |