1 23 package com.sun.enterprise.tools.verifier; 24 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import com.sun.enterprise.logging.LogDomains; 29 30 public class CLIPParser { 31 32 35 36 39 protected static final com.sun.enterprise.util.LocalStringManagerImpl smh = 40 StringManagerHelper.getLocalStringsManager(); 41 42 45 protected String optarg; 46 47 48 52 protected int optopt = '?'; 53 54 60 protected String cn; 61 62 65 protected String optstring; 66 67 70 protected LongOption[] longopt; 71 72 75 protected int longind; 76 77 81 protected boolean processed; 82 83 86 protected int fno = 1; 87 88 91 protected int lno = 1; 92 93 103 protected int optind = 0; 104 105 108 protected String [] argv; 109 110 113 protected String progname; 114 115 Logger logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER); 116 117 120 private boolean endparse = false; 121 122 128 public CLIPParser(String progname, String [] argv, String optstring, 129 LongOption[] longopt) { 130 if (optstring.length() == 0) 131 optstring = " "; 133 this.progname = progname; 134 this.argv = argv; 135 this.optstring = optstring; 136 this.longopt = longopt; 137 } 138 139 144 protected int processLOpt() { 145 LongOption currlopt = null; 146 boolean ambiguous; 147 boolean exact; 148 int eoname; 149 150 processed = true; 151 ambiguous = false; 152 exact = false; 153 longind = -1; 154 155 eoname = cn.indexOf("="); if (eoname == -1) 157 eoname = cn.length(); 158 159 for (int i = 0; i < longopt.length; i++) { 160 161 if (longopt[i].getName().startsWith(cn.substring(0, eoname))) { 162 if (longopt[i].getName().equals(cn.substring(0, eoname))) { 163 currlopt = longopt[i]; 165 longind = i; 166 exact = true; 167 break; 168 } else if (currlopt == null) { 169 currlopt = longopt[i]; 171 longind = i; 172 } else { 173 ambiguous = true; 175 } 176 } 177 } 178 179 if (ambiguous && !exact) { 180 logger.log(Level.SEVERE, getClass().getName() + ".ambig", 181 new Object []{progname, argv[optind]}); 182 cn = ""; 183 optopt = 0; 184 ++optind; 185 186 return '?'; 187 } 188 189 if (currlopt != null) { 190 ++optind; 191 192 if (eoname != cn.length()) { 193 if (currlopt.argPresent) { 194 if (cn.substring(eoname).length() > 1) 195 optarg = cn.substring(eoname + 1); 196 else 197 optarg = ""; 198 } else { 199 if (argv[optind - 1].startsWith("--")) { logger.log(Level.SEVERE, getClass().getName() + 201 ".loptnoarg", new Object []{progname, currlopt.name}); 203 } else { 204 logger.log(Level.SEVERE, 205 getClass().getName() + ".optnoarg", new Object []{progname, 207 new Character ( 208 argv[optind - 1].charAt(0)).toString(), 209 currlopt.name}); 210 } 211 212 cn = ""; 213 optopt = currlopt.value; 214 215 return '?'; 216 } 217 } else if (currlopt.argPresent) { 218 if (optind < argv.length) { 219 optarg = argv[optind]; 220 ++optind; 221 } else { 222 logger.log(Level.SEVERE, getClass().getName() + ".reqargs", 223 new Object []{progname, argv[optind - 1]}); 224 225 cn = ""; 226 optopt = currlopt.value; 227 if (optstring.charAt(0) == ':') 228 return ':'; 229 else 230 return '?'; 231 } 232 } 233 234 cn = ""; 235 236 if (currlopt.holder != null) { 237 currlopt.holder.setLength(0); 238 currlopt.holder.append(currlopt.value); 239 240 return (0); 241 } 242 243 return currlopt.value; 244 } 245 246 processed = false; 247 248 return (0); 249 } 250 251 261 public int getopt() { 262 optarg = null; 263 264 if (endparse == true) 265 return (-1); 266 267 if ((cn == null) || (cn.equals(""))) { 268 if (lno > optind) 269 lno = optind; 270 if (fno > optind) 271 fno = optind; 272 273 if ((optind != argv.length) && argv[optind].equals("--")) { optind++; 275 276 if (fno == lno) 277 fno = optind; 278 279 lno = argv.length; 280 optind = argv.length; 281 } 282 283 if (optind == argv.length) { 284 if (fno != lno) 287 optind = fno; 288 289 return -1; 290 } 291 292 if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 294 argv[optind].equals("-")) { return -1; 296 297 } 298 299 if (argv[optind].startsWith("--")) cn = argv[optind].substring(2); 302 else 303 cn = argv[optind].substring(1); 304 } 305 306 if ((longopt != null) && (argv[optind].startsWith("--"))) { int c = processLOpt(); 308 309 if (processed) 310 return c; 311 312 if (argv[optind].startsWith("--")) { logger.log(Level.SEVERE, getClass().getName() + ".loptunknown", 315 new Object []{progname, argv[optind]}); 316 } else { 317 logger.log(Level.SEVERE, getClass().getName() + ".optunknown", 318 new Object []{progname, 319 new Character (argv[optind].charAt(0)).toString(), 320 cn}); 321 } 322 323 cn = ""; 324 ++optind; 325 optopt = 0; 326 327 return '?'; 328 } 329 330 int c = cn.charAt(0); 332 if (cn.length() > 1) 333 cn = cn.substring(1); 334 else 335 cn = ""; 336 337 String ct = null; 338 if (optstring.indexOf(c) != -1) 339 ct = optstring.substring(optstring.indexOf(c)); 340 341 if (cn.equals("")) 342 ++optind; 343 344 if ((ct == null) || (c == ':')) { 345 350 351 optopt = c; 352 353 return '?'; 354 } 355 356 if ((ct.length() > 1) && (ct.charAt(1) == ':')) { 357 if ((ct.length() > 2) && (ct.charAt(2) == ':')) 358 { 360 if (!cn.equals("")) { 361 optarg = cn; 362 ++optind; 363 } else { 364 optarg = null; 365 } 366 367 cn = null; 368 } else { 369 if (!cn.equals("")) { 370 optarg = cn; 371 ++optind; 372 } else if (optind == argv.length) { 373 logger.log(Level.SEVERE, getClass().getName() + ".reqsarg", 374 new Object []{progname, 375 new Character ((char) c).toString()}); 376 377 optopt = c; 378 379 if (optstring.charAt(0) == ':') 380 return ':'; 381 else 382 return '?'; 383 } else { 384 optarg = argv[optind]; 385 ++optind; 386 } 387 388 cn = null; 389 } 390 } 391 392 return c; 393 } 394 395 public int getOptind() { 396 return optind; 397 } 398 399 400 public String getOptarg() { 401 return optarg; 402 } 403 404 public int getOptopt() { 405 return optopt; 406 } 407 408 public int getLongind() { 409 return longind; 410 } 411 412 } 413 414 415 | Popular Tags |