1 /* 2 * Copyright (c) 2018, 2019, 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 package jdk.jpackage.internal; 26 27 import java.io.File; 28 import java.io.FileInputStream; 29 import java.io.IOException; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.text.MessageFormat; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.Collection; 36 import java.util.EnumSet; 37 import java.util.HashMap; 38 import java.util.HashSet; 39 import java.util.List; 40 import java.util.Map; 41 import java.util.Set; 42 import java.util.Properties; 43 import java.util.ResourceBundle; 44 import java.util.jar.Attributes; 45 import java.util.jar.JarFile; 46 import java.util.jar.Manifest; 47 import java.util.stream.Stream; 48 import java.util.regex.Matcher; 49 import java.util.regex.Pattern; 50 51 /** 52 * Arguments 53 * 54 * This class encapsulates and processes the command line arguments, 55 * in effect, implementing all the work of jpackage tool. 56 * 57 * The primary entry point, processArguments(): 58 * Processes and validates command line arguments, constructing DeployParams. 59 * Validates the DeployParams, and generate the BundleParams. 60 * Generates List of Bundlers from BundleParams valid for this platform. 61 * Executes each Bundler in the list. 62 */ 63 public class Arguments { 64 private static final ResourceBundle I18N = ResourceBundle.getBundle( 65 "jdk.jpackage.internal.resources.MainResources"); 66 67 private static final String FA_EXTENSIONS = "extension"; 68 private static final String FA_CONTENT_TYPE = "mime-type"; 69 private static final String FA_DESCRIPTION = "description"; 70 private static final String FA_ICON = "icon"; 71 72 // regexp for parsing args (for example, for additional launchers) 73 private static Pattern pattern = Pattern.compile( 74 "(?:(?:([\"'])(?:\\\\\\1|.)*?(?:\\1|$))|(?:\\\\[\"'\\s]|[^\\s]))++"); 75 76 private DeployParams deployParams = null; 77 private String packageType = null; 78 79 private int pos = 0; 80 private List<String> argList = null; 81 82 private List<CLIOptions> allOptions = null; 83 84 private String input = null; 85 private String output = null; 86 87 private boolean hasMainJar = false; 88 private boolean hasMainClass = false; 89 private boolean hasMainModule = false; 90 public boolean userProvidedBuildRoot = false; 91 92 private String buildRoot = null; 93 private String mainJarPath = null; 94 95 private static boolean runtimeInstaller = false; 96 97 private List<AddLauncherArguments> addLaunchers = null; 98 99 private static Map<String, CLIOptions> argIds = new HashMap<>(); 100 private static Map<String, CLIOptions> argShortIds = new HashMap<>(); 101 102 static { 103 // init maps for parsing arguments 104 (EnumSet.allOf(CLIOptions.class)).forEach(option -> { 105 argIds.put(option.getIdWithPrefix(), option); 106 if (option.getShortIdWithPrefix() != null) { 107 argShortIds.put(option.getShortIdWithPrefix(), option); 108 } 109 }); 110 } 111 112 public Arguments(String[] args) { 113 argList = new ArrayList<String>(args.length); 114 for (String arg : args) { 115 argList.add(arg); 116 } 117 Log.debug ("\njpackage argument list: \n" + argList + "\n"); 118 pos = 0; 119 120 deployParams = new DeployParams(); 121 122 packageType = null; 123 124 allOptions = new ArrayList<>(); 125 126 addLaunchers = new ArrayList<>(); 127 } 128 129 // CLIOptions is public for DeployParamsTest 130 public enum CLIOptions { 131 PACKAGE_TYPE("package-type", OptionCategories.PROPERTY, () -> { 132 context().packageType = popArg(); 133 context().deployParams.setTargetFormat(context().packageType); 134 }), 135 136 INPUT ("input", "i", OptionCategories.PROPERTY, () -> { 137 context().input = popArg(); 138 setOptionValue("input", context().input); 139 }), 140 141 OUTPUT ("output", "o", OptionCategories.PROPERTY, () -> { 142 context().output = popArg(); 143 context().deployParams.setOutput(new File(context().output)); 144 }), 145 146 DESCRIPTION ("description", "d", OptionCategories.PROPERTY), 147 148 VENDOR ("vendor", OptionCategories.PROPERTY), 149 150 APPCLASS ("main-class", OptionCategories.PROPERTY, () -> { 151 context().hasMainClass = true; 152 setOptionValue("main-class", popArg()); 153 }), 154 155 NAME ("name", "n", OptionCategories.PROPERTY), 156 157 IDENTIFIER ("identifier", OptionCategories.PROPERTY), 158 159 VERBOSE ("verbose", OptionCategories.PROPERTY, () -> { 160 setOptionValue("verbose", true); 161 Log.setVerbose(true); 162 }), 163 164 RESOURCE_DIR("resource-dir", 165 OptionCategories.PROPERTY, () -> { 166 String resourceDir = popArg(); 167 setOptionValue("resource-dir", resourceDir); 168 }), 169 170 ARGUMENTS ("arguments", OptionCategories.PROPERTY, () -> { 171 List<String> arguments = getArgumentList(popArg()); 172 setOptionValue("arguments", arguments); 173 }), 174 175 ICON ("icon", OptionCategories.PROPERTY), 176 177 COPYRIGHT ("copyright", OptionCategories.PROPERTY), 178 179 LICENSE_FILE ("license-file", OptionCategories.PROPERTY), 180 181 VERSION ("app-version", OptionCategories.PROPERTY), 182 183 JAVA_OPTIONS ("java-options", OptionCategories.PROPERTY, () -> { 184 List<String> args = getArgumentList(popArg()); 185 args.forEach(a -> setOptionValue("java-options", a)); 186 }), 187 188 FILE_ASSOCIATIONS ("file-associations", 189 OptionCategories.PROPERTY, () -> { 190 Map<String, ? super Object> args = new HashMap<>(); 191 192 // load .properties file 193 Map<String, String> initialMap = getPropertiesFromFile(popArg()); 194 195 String ext = initialMap.get(FA_EXTENSIONS); 196 if (ext != null) { 197 args.put(StandardBundlerParam.FA_EXTENSIONS.getID(), ext); 198 } 199 200 String type = initialMap.get(FA_CONTENT_TYPE); 201 if (type != null) { 202 args.put(StandardBundlerParam.FA_CONTENT_TYPE.getID(), type); 203 } 204 205 String desc = initialMap.get(FA_DESCRIPTION); 206 if (desc != null) { 207 args.put(StandardBundlerParam.FA_DESCRIPTION.getID(), desc); 208 } 209 210 String icon = initialMap.get(FA_ICON); 211 if (icon != null) { 212 args.put(StandardBundlerParam.FA_ICON.getID(), icon); 213 } 214 215 ArrayList<Map<String, ? super Object>> associationList = 216 new ArrayList<Map<String, ? super Object>>(); 217 218 associationList.add(args); 219 220 // check that we really add _another_ value to the list 221 setOptionValue("file-associations", associationList); 222 223 }), 224 225 ADD_LAUNCHER ("add-launcher", 226 OptionCategories.PROPERTY, () -> { 227 String spec = popArg(); 228 String name = null; 229 String filename = spec; 230 if (spec.contains("=")) { 231 String[] values = spec.split("=", 2); 232 name = values[0]; 233 filename = values[1]; 234 } 235 context().addLaunchers.add( 236 new AddLauncherArguments(name, filename)); 237 }), 238 239 TEMP_ROOT ("temp-root", OptionCategories.PROPERTY, () -> { 240 context().buildRoot = popArg(); 241 context().userProvidedBuildRoot = true; 242 setOptionValue("temp-root", context().buildRoot); 243 }), 244 245 INSTALL_DIR ("install-dir", OptionCategories.PROPERTY), 246 247 PREDEFINED_APP_IMAGE ("app-image", OptionCategories.PROPERTY), 248 249 PREDEFINED_RUNTIME_IMAGE ("runtime-image", OptionCategories.PROPERTY), 250 251 MAIN_JAR ("main-jar", OptionCategories.PROPERTY, () -> { 252 context().mainJarPath = popArg(); 253 context().hasMainJar = true; 254 setOptionValue("main-jar", context().mainJarPath); 255 }), 256 257 MODULE ("module", "m", OptionCategories.MODULAR, () -> { 258 context().hasMainModule = true; 259 setOptionValue("module", popArg()); 260 }), 261 262 ADD_MODULES ("add-modules", OptionCategories.MODULAR), 263 264 MODULE_PATH ("module-path", "p", OptionCategories.MODULAR), 265 266 MAC_SIGN ("mac-sign", "s", OptionCategories.PLATFORM_MAC, () -> { 267 setOptionValue("mac-sign", true); 268 }), 269 270 MAC_BUNDLE_NAME ("mac-bundle-name", OptionCategories.PLATFORM_MAC), 271 272 MAC_BUNDLE_IDENTIFIER("mac-bundle-identifier", 273 OptionCategories.PLATFORM_MAC), 274 275 MAC_APP_STORE_CATEGORY ("mac-app-store-category", 276 OptionCategories.PLATFORM_MAC), 277 278 MAC_BUNDLE_SIGNING_PREFIX ("mac-bundle-signing-prefix", 279 OptionCategories.PLATFORM_MAC), 280 281 MAC_SIGNING_KEY_NAME ("mac-signing-key-user-name", 282 OptionCategories.PLATFORM_MAC), 283 284 MAC_SIGNING_KEYCHAIN ("mac-signing-keychain", 285 OptionCategories.PLATFORM_MAC), 286 287 MAC_APP_STORE_ENTITLEMENTS ("mac-app-store-entitlements", 288 OptionCategories.PLATFORM_MAC), 289 290 WIN_MENU_HINT ("win-menu", OptionCategories.PLATFORM_WIN, () -> { 291 setOptionValue("win-menu", true); 292 }), 293 294 WIN_MENU_GROUP ("win-menu-group", OptionCategories.PLATFORM_WIN), 295 296 WIN_SHORTCUT_HINT ("win-shortcut", 297 OptionCategories.PLATFORM_WIN, () -> { 298 setOptionValue("win-shortcut", true); 299 }), 300 301 WIN_PER_USER_INSTALLATION ("win-per-user-install", 302 OptionCategories.PLATFORM_WIN, () -> { 303 setOptionValue("win-per-user-install", false); 304 }), 305 306 WIN_DIR_CHOOSER ("win-dir-chooser", 307 OptionCategories.PLATFORM_WIN, () -> { 308 setOptionValue("win-dir-chooser", true); 309 }), 310 311 WIN_REGISTRY_NAME ("win-registry-name", OptionCategories.PLATFORM_WIN), 312 313 WIN_UPGRADE_UUID ("win-upgrade-uuid", 314 OptionCategories.PLATFORM_WIN), 315 316 WIN_CONSOLE_HINT ("win-console", OptionCategories.PLATFORM_WIN, () -> { 317 setOptionValue("win-console", true); 318 }), 319 320 LINUX_BUNDLE_NAME ("linux-bundle-name", 321 OptionCategories.PLATFORM_LINUX), 322 323 LINUX_DEB_MAINTAINER ("linux-deb-maintainer", 324 OptionCategories.PLATFORM_LINUX), 325 326 LINUX_RPM_LICENSE_TYPE ("linux-rpm-license-type", 327 OptionCategories.PLATFORM_LINUX), 328 329 LINUX_PACKAGE_DEPENDENCIES ("linux-package-deps", 330 OptionCategories.PLATFORM_LINUX), 331 332 LINUX_MENU_GROUP ("linux-menu-group", OptionCategories.PLATFORM_LINUX); 333 334 private final String id; 335 private final String shortId; 336 private final OptionCategories category; 337 private final ArgAction action; 338 private static Arguments argContext; 339 340 private CLIOptions(String id, OptionCategories category) { 341 this(id, null, category, null); 342 } 343 344 private CLIOptions(String id, String shortId, 345 OptionCategories category) { 346 this(id, shortId, category, null); 347 } 348 349 private CLIOptions(String id, 350 OptionCategories category, ArgAction action) { 351 this(id, null, category, action); 352 } 353 354 private CLIOptions(String id, String shortId, 355 OptionCategories category, ArgAction action) { 356 this.id = id; 357 this.shortId = shortId; 358 this.action = action; 359 this.category = category; 360 } 361 362 static void setContext(Arguments context) { 363 argContext = context; 364 } 365 366 public static Arguments context() { 367 if (argContext != null) { 368 return argContext; 369 } else { 370 throw new RuntimeException("Argument context is not set."); 371 } 372 } 373 374 public String getId() { 375 return this.id; 376 } 377 378 String getIdWithPrefix() { 379 return "--" + this.id; 380 } 381 382 String getShortIdWithPrefix() { 383 return this.shortId == null ? null : "-" + this.shortId; 384 } 385 386 void execute() { 387 if (action != null) { 388 action.execute(); 389 } else { 390 defaultAction(); 391 } 392 } 393 394 private void defaultAction() { 395 context().deployParams.addBundleArgument(id, popArg()); 396 } 397 398 private static void setOptionValue(String option, Object value) { 399 context().deployParams.addBundleArgument(option, value); 400 } 401 402 private static String popArg() { 403 nextArg(); 404 return (context().pos >= context().argList.size()) ? 405 "" : context().argList.get(context().pos); 406 } 407 408 private static String getArg() { 409 return (context().pos >= context().argList.size()) ? 410 "" : context().argList.get(context().pos); 411 } 412 413 private static void nextArg() { 414 context().pos++; 415 } 416 417 private static boolean hasNextArg() { 418 return context().pos < context().argList.size(); 419 } 420 } 421 422 enum OptionCategories { 423 MODULAR, 424 PROPERTY, 425 PLATFORM_MAC, 426 PLATFORM_WIN, 427 PLATFORM_LINUX; 428 } 429 430 public boolean processArguments() { 431 try { 432 433 // init context of arguments 434 CLIOptions.setContext(this); 435 436 // parse cmd line 437 String arg; 438 CLIOptions option; 439 for (; CLIOptions.hasNextArg(); CLIOptions.nextArg()) { 440 arg = CLIOptions.getArg(); 441 if ((option = toCLIOption(arg)) != null) { 442 // found a CLI option 443 allOptions.add(option); 444 option.execute(); 445 } else { 446 throw new PackagerException("ERR_InvalidOption", arg); 447 } 448 } 449 450 if (hasMainJar && !hasMainClass) { 451 // try to get main-class from manifest 452 String mainClass = getMainClassFromManifest(); 453 if (mainClass != null) { 454 CLIOptions.setOptionValue( 455 CLIOptions.APPCLASS.getId(), mainClass); 456 } 457 } 458 459 // display error for arguments that are not supported 460 // for current configuration. 461 462 validateArguments(); 463 464 addResources(deployParams, input); 465 466 List<Map<String, ? super Object>> launchersAsMap = 467 new ArrayList<>(); 468 469 for (AddLauncherArguments sl : addLaunchers) { 470 launchersAsMap.add(sl.getLauncherMap()); 471 } 472 473 deployParams.addBundleArgument( 474 StandardBundlerParam.ADD_LAUNCHERS.getID(), 475 launchersAsMap); 476 477 // at this point deployParams should be already configured 478 479 deployParams.validate(); 480 481 BundleParams bp = deployParams.getBundleParams(); 482 483 // validate name(s) 484 ArrayList<String> usedNames = new ArrayList<String>(); 485 usedNames.add(bp.getName()); // add main app name 486 487 for (AddLauncherArguments sl : addLaunchers) { 488 Map<String, ? super Object> slMap = sl.getLauncherMap(); 489 String slName = 490 (String) slMap.get(Arguments.CLIOptions.NAME.getId()); 491 if (slName == null) { 492 throw new PackagerException("ERR_NoAddLauncherName"); 493 } 494 // same rules apply to additional launcher names as app name 495 DeployParams.validateName(slName, false); 496 for (String usedName : usedNames) { 497 if (slName.equals(usedName)) { 498 throw new PackagerException("ERR_NoUniqueName"); 499 } 500 } 501 usedNames.add(slName); 502 } 503 if (runtimeInstaller && bp.getName() == null) { 504 throw new PackagerException("ERR_NoJreInstallerName"); 505 } 506 507 generateBundle(bp.getBundleParamsAsMap()); 508 return true; 509 } catch (Exception e) { 510 if (Log.isVerbose()) { 511 Log.verbose(e); 512 } else { 513 String msg1 = e.getMessage(); 514 Log.error(msg1); 515 if (e.getCause() != null && e.getCause() != e) { 516 String msg2 = e.getCause().getMessage(); 517 if (!msg1.contains(msg2)) { 518 Log.error(msg2); 519 } 520 } 521 } 522 return false; 523 } 524 } 525 526 private void validateArguments() throws PackagerException { 527 String packageType = deployParams.getTargetFormat(); 528 String ptype = (packageType != null) ? packageType : "default"; 529 boolean imageOnly = (packageType == null); 530 boolean hasAppImage = allOptions.contains( 531 CLIOptions.PREDEFINED_APP_IMAGE); 532 boolean hasRuntime = allOptions.contains( 533 CLIOptions.PREDEFINED_RUNTIME_IMAGE); 534 boolean installerOnly = !imageOnly && hasAppImage; 535 runtimeInstaller = !imageOnly && hasRuntime && !hasAppImage && 536 !hasMainModule && !hasMainJar; 537 538 for (CLIOptions option : allOptions) { 539 if (!ValidOptions.checkIfSupported(option)) { 540 // includes option valid only on different platform 541 throw new PackagerException("ERR_UnsupportedOption", 542 option.getIdWithPrefix()); 543 } 544 if (imageOnly) { 545 if (!ValidOptions.checkIfImageSupported(option)) { 546 throw new PackagerException("ERR_InvalidTypeOption", 547 option.getIdWithPrefix(), packageType); 548 } 549 } else if (installerOnly || runtimeInstaller) { 550 if (!ValidOptions.checkIfInstallerSupported(option)) { 551 if (runtimeInstaller) { 552 throw new PackagerException("ERR_NoInstallerEntryPoint", 553 option.getIdWithPrefix()); 554 } else { 555 throw new PackagerException("ERR_InvalidTypeOption", 556 option.getIdWithPrefix(), ptype); 557 } 558 } 559 } 560 } 561 if (installerOnly && hasRuntime) { 562 // note --runtime-image is only for image or runtime installer. 563 throw new PackagerException("ERR_InvalidTypeOption", 564 CLIOptions.PREDEFINED_RUNTIME_IMAGE.getIdWithPrefix(), 565 ptype); 566 } 567 if (hasMainJar && hasMainModule) { 568 throw new PackagerException("ERR_BothMainJarAndModule"); 569 } 570 if (imageOnly && !hasMainJar && !hasMainModule) { 571 throw new PackagerException("ERR_NoEntryPoint"); 572 } 573 } 574 575 private jdk.jpackage.internal.Bundler getPlatformBundler() { 576 String bundleType = (packageType == null ? "IMAGE" : "INSTALLER"); 577 578 for (jdk.jpackage.internal.Bundler bundler : 579 Bundlers.createBundlersInstance().getBundlers(bundleType)) { 580 if ((packageType == null) || 581 packageType.equalsIgnoreCase(bundler.getID())) { 582 if (bundler.supported(runtimeInstaller)) { 583 return bundler; 584 } 585 } 586 } 587 return null; 588 } 589 590 private void generateBundle(Map<String,? super Object> params) 591 throws PackagerException { 592 593 boolean bundleCreated = false; 594 595 // the temp-root needs to be fetched from the params early, 596 // to prevent each copy of the params (such as may be used for 597 // additional launchers) from generating a separate temp-root when 598 // the default is used (the default is a new temp directory) 599 // The bundler.cleanup() below would not otherwise be able to 600 // clean these extra (and unneeded) temp directories. 601 StandardBundlerParam.TEMP_ROOT.fetchFrom(params); 602 603 // determine what bundler to run 604 jdk.jpackage.internal.Bundler bundler = getPlatformBundler(); 605 606 if (bundler == null) { 607 throw new PackagerException("ERR_InvalidInstallerType", 608 deployParams.getTargetFormat()); 609 } 610 611 Map<String, ? super Object> localParams = new HashMap<>(params); 612 try { 613 bundler.validate(localParams); 614 File result = bundler.execute(localParams, deployParams.outdir); 615 if (result == null) { 616 throw new PackagerException("MSG_BundlerFailed", 617 bundler.getID(), bundler.getName()); 618 } 619 Log.verbose(MessageFormat.format( 620 I18N.getString("message.bundle-created"), 621 bundler.getName())); 622 } catch (ConfigException e) { 623 Log.debug(e); 624 if (e.getAdvice() != null) { 625 throw new PackagerException(e, "MSG_BundlerConfigException", 626 bundler.getName(), e.getMessage(), e.getAdvice()); 627 } else { 628 throw new PackagerException(e, 629 "MSG_BundlerConfigExceptionNoAdvice", 630 bundler.getName(), e.getMessage()); 631 } 632 } catch (RuntimeException re) { 633 Log.debug(re); 634 throw new PackagerException(re, "MSG_BundlerRuntimeException", 635 bundler.getName(), re.toString()); 636 } finally { 637 if (userProvidedBuildRoot) { 638 Log.verbose(MessageFormat.format( 639 I18N.getString("message.debug-working-directory"), 640 (new File(buildRoot)).getAbsolutePath())); 641 } else { 642 // always clean up the temporary directory created 643 // when --temp-root option not used. 644 bundler.cleanup(localParams); 645 } 646 } 647 } 648 649 private void addResources(DeployParams deployParams, 650 String inputdir) throws PackagerException { 651 652 if (inputdir == null || inputdir.isEmpty()) { 653 return; 654 } 655 656 File baseDir = new File(inputdir); 657 658 if (!baseDir.isDirectory()) { 659 throw new PackagerException("ERR_InputNotDirectory", inputdir); 660 } 661 if (!baseDir.canRead()) { 662 throw new PackagerException("ERR_CannotReadInputDir", inputdir); 663 } 664 665 List<String> fileNames; 666 fileNames = new ArrayList<>(); 667 try (Stream<Path> files = Files.list(baseDir.toPath())) { 668 files.forEach(file -> fileNames.add( 669 file.getFileName().toString())); 670 } catch (IOException e) { 671 Log.error("Unable to add resources: " + e.getMessage()); 672 } 673 fileNames.forEach(file -> deployParams.addResource(baseDir, file)); 674 675 deployParams.setClasspath(); 676 } 677 678 static CLIOptions toCLIOption(String arg) { 679 CLIOptions option; 680 if ((option = argIds.get(arg)) == null) { 681 option = argShortIds.get(arg); 682 } 683 return option; 684 } 685 686 static Map<String, String> getPropertiesFromFile(String filename) { 687 Map<String, String> map = new HashMap<>(); 688 // load properties file 689 File file = new File(filename); 690 Properties properties = new Properties(); 691 try (FileInputStream in = new FileInputStream(file)) { 692 properties.load(in); 693 } catch (IOException e) { 694 Log.error("Exception: " + e.getMessage()); 695 } 696 697 for (final String name: properties.stringPropertyNames()) { 698 map.put(name, properties.getProperty(name)); 699 } 700 701 return map; 702 } 703 704 static List<String> getArgumentList(String inputString) { 705 List<String> list = new ArrayList<>(); 706 if (inputString == null || inputString.isEmpty()) { 707 return list; 708 } 709 710 // The "pattern" regexp attempts to abide to the rule that 711 // strings are delimited by whitespace unless surrounded by 712 // quotes, then it is anything (including spaces) in the quotes. 713 Matcher m = pattern.matcher(inputString); 714 while (m.find()) { 715 String s = inputString.substring(m.start(), m.end()).trim(); 716 // Ensure we do not have an empty string. trim() will take care of 717 // whitespace only strings. The regex preserves quotes and escaped 718 // chars so we need to clean them before adding to the List 719 if (!s.isEmpty()) { 720 list.add(unquoteIfNeeded(s)); 721 } 722 } 723 return list; 724 } 725 726 private static String unquoteIfNeeded(String in) { 727 if (in == null) { 728 return null; 729 } 730 731 if (in.isEmpty()) { 732 return ""; 733 } 734 735 // Use code points to preserve non-ASCII chars 736 StringBuilder sb = new StringBuilder(); 737 int codeLen = in.codePointCount(0, in.length()); 738 int quoteChar = -1; 739 for (int i = 0; i < codeLen; i++) { 740 int code = in.codePointAt(i); 741 if (code == '"' || code == '\'') { 742 // If quote is escaped make sure to copy it 743 if (i > 0 && in.codePointAt(i - 1) == '\\') { 744 sb.deleteCharAt(sb.length() - 1); 745 sb.appendCodePoint(code); 746 continue; 747 } 748 if (quoteChar != -1) { 749 if (code == quoteChar) { 750 // close quote, skip char 751 quoteChar = -1; 752 } else { 753 sb.appendCodePoint(code); 754 } 755 } else { 756 // opening quote, skip char 757 quoteChar = code; 758 } 759 } else { 760 sb.appendCodePoint(code); 761 } 762 } 763 return sb.toString(); 764 } 765 766 private String getMainClassFromManifest() { 767 if (mainJarPath == null || 768 input == null ) { 769 return null; 770 } 771 772 JarFile jf; 773 try { 774 File file = new File(input, mainJarPath); 775 if (!file.exists()) { 776 return null; 777 } 778 jf = new JarFile(file); 779 Manifest m = jf.getManifest(); 780 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 781 if (attrs != null) { 782 return attrs.getValue(Attributes.Name.MAIN_CLASS); 783 } 784 } catch (IOException ignore) {} 785 return null; 786 } 787 788 }