1 /* 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.util.jar.pack; 27 28 import java.io.BufferedInputStream; 29 import java.io.BufferedOutputStream; 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.io.PrintStream; 37 import java.text.MessageFormat; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.HashMap; 41 import java.util.Iterator; 42 import java.util.List; 43 import java.util.ListIterator; 44 import java.util.Map; 45 import java.util.Properties; 46 import java.util.ResourceBundle; 47 import java.util.SortedMap; 48 import java.util.TreeMap; 49 import java.util.jar.JarFile; 50 import java.util.jar.JarOutputStream; 51 import java.util.jar.Pack200; 52 import java.util.zip.GZIPInputStream; 53 import java.util.zip.GZIPOutputStream; 54 55 /** Command line interface for Pack200. 56 */ 57 class Driver { 58 private static final ResourceBundle RESOURCE = 59 ResourceBundle.getBundle("com.sun.java.util.jar.pack.DriverResource"); 60 61 public static void main(String[] ava) throws IOException { 62 List<String> av = new ArrayList<>(Arrays.asList(ava)); 63 64 boolean doPack = true; 65 boolean doUnpack = false; 66 boolean doRepack = false; 67 boolean doZip = true; 68 String logFile = null; 69 String verboseProp = Utils.DEBUG_VERBOSE; 70 71 { 72 // Non-standard, undocumented "--unpack" switch enables unpack mode. 73 String arg0 = av.isEmpty() ? "" : av.get(0); 74 switch (arg0) { 75 case "--pack": 76 av.remove(0); 77 break; 78 case "--unpack": 79 av.remove(0); 80 doPack = false; 81 doUnpack = true; 82 break; 83 } 84 } 85 86 // Collect engine properties here: 87 Map<String,String> engProps = new HashMap<>(); 88 engProps.put(verboseProp, System.getProperty(verboseProp)); 89 90 String optionMap; 91 String[] propTable; 92 if (doPack) { 93 optionMap = PACK200_OPTION_MAP; 94 propTable = PACK200_PROPERTY_TO_OPTION; 95 } else { 96 optionMap = UNPACK200_OPTION_MAP; 97 propTable = UNPACK200_PROPERTY_TO_OPTION; 98 } 99 100 // Collect argument properties here: 101 Map<String,String> avProps = new HashMap<>(); 102 try { 103 for (;;) { 104 String state = parseCommandOptions(av, optionMap, avProps); 105 // Translate command line options to Pack200 properties: 106 eachOpt: 107 for (Iterator<String> opti = avProps.keySet().iterator(); 108 opti.hasNext(); ) { 109 String opt = opti.next(); 110 String prop = null; 111 for (int i = 0; i < propTable.length; i += 2) { 112 if (opt.equals(propTable[1+i])) { 113 prop = propTable[0+i]; 114 break; 115 } 116 } 117 if (prop != null) { 118 String val = avProps.get(opt); 119 opti.remove(); // remove opt from avProps 120 if (!prop.endsWith(".")) { 121 // Normal string or boolean. 122 if (!(opt.equals("--verbose") 123 || opt.endsWith("="))) { 124 // Normal boolean; convert to T/F. 125 boolean flag = (val != null); 126 if (opt.startsWith("--no-")) 127 flag = !flag; 128 val = flag? "true": "false"; 129 } 130 engProps.put(prop, val); 131 } else if (prop.contains(".attribute.")) { 132 for (String val1 : val.split("\0")) { 133 String[] val2 = val1.split("=", 2); 134 engProps.put(prop+val2[0], val2[1]); 135 } 136 } else { 137 // Collection property: pack.pass.file.cli.NNN 138 int idx = 1; 139 for (String val1 : val.split("\0")) { 140 String prop1; 141 do { 142 prop1 = prop+"cli."+(idx++); 143 } while (engProps.containsKey(prop1)); 144 engProps.put(prop1, val1); 145 } 146 } 147 } 148 } 149 150 // See if there is any other action to take. 151 if ("--config-file=".equals(state)) { 152 String propFile = av.remove(0); 153 InputStream propIn = new FileInputStream(propFile); 154 Properties fileProps = new Properties(); 155 fileProps.load(new BufferedInputStream(propIn)); 156 if (engProps.get(verboseProp) != null) 157 fileProps.list(System.out); 158 propIn.close(); 159 for (Map.Entry<Object,Object> me : fileProps.entrySet()) { 160 engProps.put((String) me.getKey(), (String) me.getValue()); 161 } 162 } else if ("--version".equals(state)) { 163 System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.VERSION), Driver.class.getName(), "1.31, 07/05/05")); 164 return; 165 } else if ("--help".equals(state)) { 166 printUsage(doPack, true, System.out); 167 System.exit(1); 168 return; 169 } else { 170 break; 171 } 172 } 173 } catch (IllegalArgumentException ee) { 174 System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_ARGUMENT), ee)); 175 printUsage(doPack, false, System.err); 176 System.exit(2); 177 return; 178 } 179 180 // Deal with remaining non-engine properties: 181 for (String opt : avProps.keySet()) { 182 String val = avProps.get(opt); 183 switch (opt) { 184 case "--repack": 185 doRepack = true; 186 break; 187 case "--no-gzip": 188 doZip = (val == null); 189 break; 190 case "--log-file=": 191 logFile = val; 192 break; 193 default: 194 throw new InternalError(MessageFormat.format( 195 RESOURCE.getString(DriverResource.BAD_OPTION), 196 opt, avProps.get(opt))); 197 } 198 } 199 200 if (logFile != null && !logFile.equals("")) { 201 if (logFile.equals("-")) { 202 System.setErr(System.out); 203 } else { 204 OutputStream log = new FileOutputStream(logFile); 205 //log = new BufferedOutputStream(out); 206 System.setErr(new PrintStream(log)); 207 } 208 } 209 210 boolean verbose = (engProps.get(verboseProp) != null); 211 212 String packfile = ""; 213 if (!av.isEmpty()) 214 packfile = av.remove(0); 215 216 String jarfile = ""; 217 if (!av.isEmpty()) 218 jarfile = av.remove(0); 219 220 String newfile = ""; // output JAR file if --repack 221 String bakfile = ""; // temporary backup of input JAR 222 String tmpfile = ""; // temporary file to be deleted 223 if (doRepack) { 224 // The first argument is the target JAR file. 225 // (Note: *.pac is nonstandard, but may be necessary 226 // if a host OS truncates file extensions.) 227 if (packfile.toLowerCase().endsWith(".pack") || 228 packfile.toLowerCase().endsWith(".pac") || 229 packfile.toLowerCase().endsWith(".gz")) { 230 System.err.println(MessageFormat.format( 231 RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT), 232 packfile)); 233 printUsage(doPack, false, System.err); 234 System.exit(2); 235 } 236 newfile = packfile; 237 // The optional second argument is the source JAR file. 238 if (jarfile.equals("")) { 239 // If only one file is given, it is the only JAR. 240 // It serves as both input and output. 241 jarfile = newfile; 242 } 243 tmpfile = createTempFile(newfile, ".pack").getPath(); 244 packfile = tmpfile; 245 doZip = false; // no need to zip the temporary file 246 } 247 248 if (!av.isEmpty() 249 // Accept jarfiles ending with .jar or .zip. 250 // Accept jarfile of "-" (stdout), but only if unpacking. 251 || !(jarfile.toLowerCase().endsWith(".jar") 252 || jarfile.toLowerCase().endsWith(".zip") 253 || (jarfile.equals("-") && !doPack))) { 254 printUsage(doPack, false, System.err); 255 System.exit(2); 256 return; 257 } 258 259 if (doRepack) 260 doPack = doUnpack = true; 261 else if (doPack) 262 doUnpack = false; 263 264 Pack200.Packer jpack = Pack200.newPacker(); 265 Pack200.Unpacker junpack = Pack200.newUnpacker(); 266 267 jpack.properties().putAll(engProps); 268 junpack.properties().putAll(engProps); 269 if (doRepack && newfile.equals(jarfile)) { 270 String zipc = getZipComment(jarfile); 271 if (verbose && zipc.length() > 0) 272 System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.DETECTED_ZIP_COMMENT), zipc)); 273 if (zipc.indexOf(Utils.PACK_ZIP_ARCHIVE_MARKER_COMMENT) >= 0) { 274 System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_REPACKED), jarfile)); 275 doPack = false; 276 doUnpack = false; 277 doRepack = false; 278 } 279 } 280 281 try { 282 283 if (doPack) { 284 // Mode = Pack. 285 JarFile in = new JarFile(new File(jarfile)); 286 OutputStream out; 287 // Packfile must be -, *.gz, *.pack, or *.pac. 288 if (packfile.equals("-")) { 289 out = System.out; 290 // Send warnings, etc., to stderr instead of stdout. 291 System.setOut(System.err); 292 } else if (doZip) { 293 if (!packfile.endsWith(".gz")) { 294 System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACK_FILE), packfile)); 295 printUsage(doPack, false, System.err); 296 System.exit(2); 297 } 298 out = new FileOutputStream(packfile); 299 out = new BufferedOutputStream(out); 300 out = new GZIPOutputStream(out); 301 } else { 302 if (!packfile.toLowerCase().endsWith(".pack") && 303 !packfile.toLowerCase().endsWith(".pac")) { 304 System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WIRTE_PACKGZ_FILE),packfile)); 305 printUsage(doPack, false, System.err); 306 System.exit(2); 307 } 308 out = new FileOutputStream(packfile); 309 out = new BufferedOutputStream(out); 310 } 311 jpack.pack(in, out); 312 //in.close(); // p200 closes in but not out 313 out.close(); 314 } 315 316 if (doRepack && newfile.equals(jarfile)) { 317 // If the source and destination are the same, 318 // we will move the input JAR aside while regenerating it. 319 // This allows us to restore it if something goes wrong. 320 File bakf = createTempFile(jarfile, ".bak"); 321 // On Windows target must be deleted see 4017593 322 bakf.delete(); 323 boolean okBackup = new File(jarfile).renameTo(bakf); 324 if (!okBackup) { 325 throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED),bakfile)); 326 } else { 327 // Open jarfile recovery bracket. 328 bakfile = bakf.getPath(); 329 } 330 } 331 332 if (doUnpack) { 333 // Mode = Unpack. 334 InputStream in; 335 if (packfile.equals("-")) 336 in = System.in; 337 else 338 in = new FileInputStream(new File(packfile)); 339 BufferedInputStream inBuf = new BufferedInputStream(in); 340 in = inBuf; 341 if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) { 342 in = new GZIPInputStream(in); 343 } 344 String outfile = newfile.equals("")? jarfile: newfile; 345 OutputStream fileOut; 346 if (outfile.equals("-")) 347 fileOut = System.out; 348 else 349 fileOut = new FileOutputStream(outfile); 350 fileOut = new BufferedOutputStream(fileOut); 351 JarOutputStream out = new JarOutputStream(fileOut); 352 junpack.unpack(in, out); 353 //in.close(); // p200 closes in but not out 354 out.close(); 355 // At this point, we have a good jarfile (or newfile, if -r) 356 } 357 358 if (!bakfile.equals("")) { 359 // On success, abort jarfile recovery bracket. 360 new File(bakfile).delete(); 361 bakfile = ""; 362 } 363 364 } finally { 365 // Close jarfile recovery bracket. 366 if (!bakfile.equals("")) { 367 File jarFile = new File(jarfile); 368 jarFile.delete(); // Win32 requires this, see above 369 new File(bakfile).renameTo(jarFile); 370 } 371 // In all cases, delete temporary *.pack. 372 if (!tmpfile.equals("")) 373 new File(tmpfile).delete(); 374 } 375 } 376 377 static private 378 File createTempFile(String basefile, String suffix) throws IOException { 379 File base = new File(basefile); 380 String prefix = base.getName(); 381 if (prefix.length() < 3) prefix += "tmp"; 382 383 File where = base.getParentFile(); 384 385 if ( base.getParentFile() == null && suffix.equals(".bak")) 386 where = new File(".").getAbsoluteFile(); 387 388 389 File f = File.createTempFile(prefix, suffix, where); 390 return f; 391 } 392 393 static private 394 void printUsage(boolean doPack, boolean full, PrintStream out) { 395 String prog = doPack ? "pack200" : "unpack200"; 396 String[] packUsage = (String[])RESOURCE.getObject(DriverResource.PACK_HELP); 397 String[] unpackUsage = (String[])RESOURCE.getObject(DriverResource.UNPACK_HELP); 398 String[] usage = doPack? packUsage: unpackUsage; 399 for (int i = 0; i < usage.length; i++) { 400 out.println(usage[i]); 401 if (!full) { 402 out.println(MessageFormat.format(RESOURCE.getString(DriverResource.MORE_INFO), prog)); 403 break; 404 } 405 } 406 } 407 408 static private 409 String getZipComment(String jarfile) throws IOException { 410 byte[] tail = new byte[1000]; 411 long filelen = new File(jarfile).length(); 412 if (filelen <= 0) return ""; 413 long skiplen = Math.max(0, filelen - tail.length); 414 InputStream in = new FileInputStream(new File(jarfile)); 415 try { 416 in.skip(skiplen); 417 in.read(tail); 418 for (int i = tail.length-4; i >= 0; i--) { 419 if (tail[i+0] == 'P' && tail[i+1] == 'K' && 420 tail[i+2] == 5 && tail[i+3] == 6) { 421 // Skip sig4, disks4, entries4, clen4, coff4, cmt2 422 i += 4+4+4+4+4+2; 423 if (i < tail.length) 424 return new String(tail, i, tail.length-i, "UTF8"); 425 return ""; 426 } 427 } 428 return ""; 429 } finally { 430 in.close(); 431 } 432 } 433 434 private static final String PACK200_OPTION_MAP = 435 ("" 436 +"--repack $ \n -r +>- @--repack $ \n" 437 +"--no-gzip $ \n -g +>- @--no-gzip $ \n" 438 +"--strip-debug $ \n -G +>- @--strip-debug $ \n" 439 +"--no-keep-file-order $ \n -O +>- @--no-keep-file-order $ \n" 440 +"--segment-limit= *> = \n -S +> @--segment-limit= = \n" 441 +"--effort= *> = \n -E +> @--effort= = \n" 442 +"--deflate-hint= *> = \n -H +> @--deflate-hint= = \n" 443 +"--modification-time= *> = \n -m +> @--modification-time= = \n" 444 +"--pass-file= *> &\0 \n -P +> @--pass-file= &\0 \n" 445 +"--unknown-attribute= *> = \n -U +> @--unknown-attribute= = \n" 446 +"--class-attribute= *> &\0 \n -C +> @--class-attribute= &\0 \n" 447 +"--field-attribute= *> &\0 \n -F +> @--field-attribute= &\0 \n" 448 +"--method-attribute= *> &\0 \n -M +> @--method-attribute= &\0 \n" 449 +"--code-attribute= *> &\0 \n -D +> @--code-attribute= &\0 \n" 450 +"--config-file= *> . \n -f +> @--config-file= . \n" 451 452 // Negative options as required by CLIP: 453 +"--no-strip-debug !--strip-debug \n" 454 +"--gzip !--no-gzip \n" 455 +"--keep-file-order !--no-keep-file-order \n" 456 457 // Non-Standard Options 458 +"--verbose $ \n -v +>- @--verbose $ \n" 459 +"--quiet !--verbose \n -q +>- !--verbose \n" 460 +"--log-file= *> = \n -l +> @--log-file= = \n" 461 //+"--java-option= *> = \n -J +> @--java-option= = \n" 462 +"--version . \n -V +> @--version . \n" 463 +"--help . \n -? +> @--help . \n -h +> @--help . \n" 464 465 // Termination: 466 +"-- . \n" // end option sequence here 467 +"- +? >- . \n" // report error if -XXX present; else use stdout 468 ); 469 // Note: Collection options use "\0" as a delimiter between arguments. 470 471 // For Java version of unpacker (used for testing only): 472 private static final String UNPACK200_OPTION_MAP = 473 ("" 474 +"--deflate-hint= *> = \n -H +> @--deflate-hint= = \n" 475 +"--verbose $ \n -v +>- @--verbose $ \n" 476 +"--quiet !--verbose \n -q +>- !--verbose \n" 477 +"--remove-pack-file $ \n -r +>- @--remove-pack-file $ \n" 478 +"--log-file= *> = \n -l +> @--log-file= = \n" 479 +"--config-file= *> . \n -f +> @--config-file= . \n" 480 481 // Termination: 482 +"-- . \n" // end option sequence here 483 +"- +? >- . \n" // report error if -XXX present; else use stdin 484 +"--version . \n -V +> @--version . \n" 485 +"--help . \n -? +> @--help . \n -h +> @--help . \n" 486 ); 487 488 private static final String[] PACK200_PROPERTY_TO_OPTION = { 489 Pack200.Packer.SEGMENT_LIMIT, "--segment-limit=", 490 Pack200.Packer.KEEP_FILE_ORDER, "--no-keep-file-order", 491 Pack200.Packer.EFFORT, "--effort=", 492 Pack200.Packer.DEFLATE_HINT, "--deflate-hint=", 493 Pack200.Packer.MODIFICATION_TIME, "--modification-time=", 494 Pack200.Packer.PASS_FILE_PFX, "--pass-file=", 495 Pack200.Packer.UNKNOWN_ATTRIBUTE, "--unknown-attribute=", 496 Pack200.Packer.CLASS_ATTRIBUTE_PFX, "--class-attribute=", 497 Pack200.Packer.FIELD_ATTRIBUTE_PFX, "--field-attribute=", 498 Pack200.Packer.METHOD_ATTRIBUTE_PFX, "--method-attribute=", 499 Pack200.Packer.CODE_ATTRIBUTE_PFX, "--code-attribute=", 500 //Pack200.Packer.PROGRESS, "--progress=", 501 Utils.DEBUG_VERBOSE, "--verbose", 502 Utils.COM_PREFIX+"strip.debug", "--strip-debug", 503 }; 504 505 private static final String[] UNPACK200_PROPERTY_TO_OPTION = { 506 Pack200.Unpacker.DEFLATE_HINT, "--deflate-hint=", 507 //Pack200.Unpacker.PROGRESS, "--progress=", 508 Utils.DEBUG_VERBOSE, "--verbose", 509 Utils.UNPACK_REMOVE_PACKFILE, "--remove-pack-file", 510 }; 511 512 /*-* 513 * Remove a set of command-line options from args, 514 * storing them in the map in a canonicalized form. 515 * <p> 516 * The options string is a newline-separated series of 517 * option processing specifiers. 518 */ 519 private static 520 String parseCommandOptions(List<String> args, 521 String options, 522 Map<String,String> properties) { 523 //System.out.println(args+" // "+properties); 524 525 String resultString = null; 526 527 // Convert options string into optLines dictionary. 528 TreeMap<String,String[]> optmap = new TreeMap<>(); 529 loadOptmap: 530 for (String optline : options.split("\n")) { 531 String[] words = optline.split("\\p{Space}+"); 532 if (words.length == 0) continue loadOptmap; 533 String opt = words[0]; 534 words[0] = ""; // initial word is not a spec 535 if (opt.length() == 0 && words.length >= 1) { 536 opt = words[1]; // initial "word" is empty due to leading ' ' 537 words[1] = ""; 538 } 539 if (opt.length() == 0) continue loadOptmap; 540 String[] prevWords = optmap.put(opt, words); 541 if (prevWords != null) 542 throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.DUPLICATE_OPTION), optline.trim())); 543 } 544 545 // State machine for parsing a command line. 546 ListIterator<String> argp = args.listIterator(); 547 ListIterator<String> pbp = new ArrayList<String>().listIterator(); 548 doArgs: 549 for (;;) { 550 // One trip through this loop per argument. 551 // Multiple trips per option only if several options per argument. 552 String arg; 553 if (pbp.hasPrevious()) { 554 arg = pbp.previous(); 555 pbp.remove(); 556 } else if (argp.hasNext()) { 557 arg = argp.next(); 558 } else { 559 // No more arguments at all. 560 break doArgs; 561 } 562 tryOpt: 563 for (int optlen = arg.length(); ; optlen--) { 564 // One time through this loop for each matching arg prefix. 565 String opt; 566 // Match some prefix of the argument to a key in optmap. 567 findOpt: 568 for (;;) { 569 opt = arg.substring(0, optlen); 570 if (optmap.containsKey(opt)) break findOpt; 571 if (optlen == 0) break tryOpt; 572 // Decide on a smaller prefix to search for. 573 SortedMap<String,String[]> pfxmap = optmap.headMap(opt); 574 // pfxmap.lastKey is no shorter than any prefix in optmap. 575 int len = pfxmap.isEmpty() ? 0 : pfxmap.lastKey().length(); 576 optlen = Math.min(len, optlen - 1); 577 opt = arg.substring(0, optlen); 578 // (Note: We could cut opt down to its common prefix with 579 // pfxmap.lastKey, but that wouldn't save many cycles.) 580 } 581 opt = opt.intern(); 582 assert(arg.startsWith(opt)); 583 assert(opt.length() == optlen); 584 String val = arg.substring(optlen); // arg == opt+val 585 586 // Execute the option processing specs for this opt. 587 // If no actions are taken, then look for a shorter prefix. 588 boolean didAction = false; 589 boolean isError = false; 590 591 int pbpMark = pbp.nextIndex(); // in case of backtracking 592 String[] specs = optmap.get(opt); 593 eachSpec: 594 for (String spec : specs) { 595 if (spec.length() == 0) continue eachSpec; 596 if (spec.startsWith("#")) break eachSpec; 597 int sidx = 0; 598 char specop = spec.charAt(sidx++); 599 600 // Deal with '+'/'*' prefixes (spec conditions). 601 boolean ok; 602 switch (specop) { 603 case '+': 604 // + means we want an non-empty val suffix. 605 ok = (val.length() != 0); 606 specop = spec.charAt(sidx++); 607 break; 608 case '*': 609 // * means we accept empty or non-empty 610 ok = true; 611 specop = spec.charAt(sidx++); 612 break; 613 default: 614 // No condition prefix means we require an exact 615 // match, as indicated by an empty val suffix. 616 ok = (val.length() == 0); 617 break; 618 } 619 if (!ok) continue eachSpec; 620 621 String specarg = spec.substring(sidx); 622 switch (specop) { 623 case '.': // terminate the option sequence 624 resultString = (specarg.length() != 0)? specarg.intern(): opt; 625 break doArgs; 626 case '?': // abort the option sequence 627 resultString = (specarg.length() != 0)? specarg.intern(): arg; 628 isError = true; 629 break eachSpec; 630 case '@': // change the effective opt name 631 opt = specarg.intern(); 632 break; 633 case '>': // shift remaining arg val to next arg 634 pbp.add(specarg + val); // push a new argument 635 val = ""; 636 break; 637 case '!': // negation option 638 String negopt = (specarg.length() != 0)? specarg.intern(): opt; 639 properties.remove(negopt); 640 properties.put(negopt, null); // leave placeholder 641 didAction = true; 642 break; 643 case '$': // normal "boolean" option 644 String boolval; 645 if (specarg.length() != 0) { 646 // If there is a given spec token, store it. 647 boolval = specarg; 648 } else { 649 String old = properties.get(opt); 650 if (old == null || old.length() == 0) { 651 boolval = "1"; 652 } else { 653 // Increment any previous value as a numeral. 654 boolval = ""+(1+Integer.parseInt(old)); 655 } 656 } 657 properties.put(opt, boolval); 658 didAction = true; 659 break; 660 case '=': // "string" option 661 case '&': // "collection" option 662 // Read an option. 663 boolean append = (specop == '&'); 664 String strval; 665 if (pbp.hasPrevious()) { 666 strval = pbp.previous(); 667 pbp.remove(); 668 } else if (argp.hasNext()) { 669 strval = argp.next(); 670 } else { 671 resultString = arg + " ?"; 672 isError = true; 673 break eachSpec; 674 } 675 if (append) { 676 String old = properties.get(opt); 677 if (old != null) { 678 // Append new val to old with embedded delim. 679 String delim = specarg; 680 if (delim.length() == 0) delim = " "; 681 strval = old + specarg + strval; 682 } 683 } 684 properties.put(opt, strval); 685 didAction = true; 686 break; 687 default: 688 throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_SPEC),opt, spec)); 689 } 690 } 691 692 // Done processing specs. 693 if (didAction && !isError) { 694 continue doArgs; 695 } 696 697 // The specs should have done something, but did not. 698 while (pbp.nextIndex() > pbpMark) { 699 // Remove anything pushed during these specs. 700 pbp.previous(); 701 pbp.remove(); 702 } 703 704 if (isError) { 705 throw new IllegalArgumentException(resultString); 706 } 707 708 if (optlen == 0) { 709 // We cannot try a shorter matching option. 710 break tryOpt; 711 } 712 } 713 714 // If we come here, there was no matching option. 715 // So, push back the argument, and return to caller. 716 pbp.add(arg); 717 break doArgs; 718 } 719 // Report number of arguments consumed. 720 args.subList(0, argp.nextIndex()).clear(); 721 // Report any unconsumed partial argument. 722 while (pbp.hasPrevious()) { 723 args.add(0, pbp.previous()); 724 } 725 //System.out.println(args+" // "+properties+" -> "+resultString); 726 return resultString; 727 } 728 }