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 
  26 package jdk.jpackage.internal;
  27 
  28 import java.util.HashMap;
  29 import java.util.HashSet;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import jdk.jpackage.internal.Arguments.CLIOptions;
  33 
  34 /**
  35  * ValidOptions
  36  *
  37  * Two basic methods for validating command line options.
  38  *
  39  * initArgs()
  40  *      Computes the Map of valid options for each mode on this Platform.
  41  *
  42  * checkIfSupported(CLIOptions arg)
  43  *      Determine if the given arg is valid on this platform.
  44  *
  45  * checkIfImageSupported(CLIOptions arg)
  46  *      Determine if the given arg is valid for creating app image.
  47  *
  48  * checkIfInstallerSupported(CLIOptions arg)
  49  *      Determine if the given arg is valid for creating installer.
  50  *
  51  */
  52 class ValidOptions {
  53 
  54     enum USE {
  55         ALL,        // valid in all cases
  56         LAUNCHER,   // valid when creating a launcher
  57         INSTALL     // valid when creating an installer
  58     }
  59 
  60     private static final HashMap<String, USE> options = new HashMap<>();
  61 
  62 
  63     // initializing list of mandatory arguments
  64     static {
  65         options.put(CLIOptions.NAME.getId(), USE.ALL);
  66         options.put(CLIOptions.VERSION.getId(), USE.ALL);
  67         options.put(CLIOptions.OUTPUT.getId(), USE.ALL);
  68         options.put(CLIOptions.TEMP_ROOT.getId(), USE.ALL);
  69         options.put(CLIOptions.VERBOSE.getId(), USE.ALL);
  70         options.put(CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId(), USE.ALL);
  71         options.put(CLIOptions.RESOURCE_DIR.getId(), USE.ALL);
  72         options.put(CLIOptions.IDENTIFIER.getId(), USE.ALL);
  73         options.put(CLIOptions.DESCRIPTION.getId(), USE.ALL);
  74         options.put(CLIOptions.VENDOR.getId(), USE.ALL);
  75         options.put(CLIOptions.COPYRIGHT.getId(), USE.ALL);
  76         options.put(CLIOptions.PACKAGE_TYPE.getId(), USE.ALL);
  77 
  78         options.put(CLIOptions.INPUT.getId(), USE.LAUNCHER);
  79         options.put(CLIOptions.MODULE.getId(), USE.LAUNCHER);
  80         options.put(CLIOptions.MODULE_PATH.getId(), USE.LAUNCHER);
  81         options.put(CLIOptions.ADD_MODULES.getId(), USE.LAUNCHER);
  82         options.put(CLIOptions.MAIN_JAR.getId(), USE.LAUNCHER);
  83         options.put(CLIOptions.APPCLASS.getId(), USE.LAUNCHER);
  84         options.put(CLIOptions.ICON.getId(), USE.LAUNCHER);
  85         options.put(CLIOptions.ARGUMENTS.getId(), USE.LAUNCHER);
  86         options.put(CLIOptions.JAVA_OPTIONS.getId(), USE.LAUNCHER);
  87         options.put(CLIOptions.ADD_LAUNCHER.getId(), USE.LAUNCHER);
  88 
  89         options.put(CLIOptions.LICENSE_FILE.getId(), USE.INSTALL);
  90         options.put(CLIOptions.INSTALL_DIR.getId(), USE.INSTALL);
  91         options.put(CLIOptions.PREDEFINED_APP_IMAGE.getId(), USE.INSTALL);
  92 
  93         options.put(CLIOptions.FILE_ASSOCIATIONS.getId(),
  94             (Platform.getPlatform() == Platform.MAC) ?  USE.ALL : USE.INSTALL);
  95 
  96         if (Platform.getPlatform() == Platform.WINDOWS) {
  97             options.put(CLIOptions.WIN_CONSOLE_HINT.getId(), USE.LAUNCHER);
  98 
  99             options.put(CLIOptions.WIN_MENU_HINT.getId(), USE.INSTALL);
 100             options.put(CLIOptions.WIN_MENU_GROUP.getId(), USE.INSTALL);
 101             options.put(CLIOptions.WIN_SHORTCUT_HINT.getId(), USE.INSTALL);
 102             options.put(CLIOptions.WIN_DIR_CHOOSER.getId(), USE.INSTALL);
 103             options.put(CLIOptions.WIN_UPGRADE_UUID.getId(), USE.INSTALL);
 104             options.put(CLIOptions.WIN_PER_USER_INSTALLATION.getId(),
 105                     USE.INSTALL);
 106         }
 107 
 108         if (Platform.getPlatform() == Platform.MAC) {
 109             options.put(CLIOptions.MAC_SIGN.getId(), USE.ALL);
 110             options.put(CLIOptions.MAC_BUNDLE_NAME.getId(), USE.ALL);
 111             options.put(CLIOptions.MAC_BUNDLE_IDENTIFIER.getId(), USE.ALL);
 112             options.put(CLIOptions.MAC_BUNDLE_SIGNING_PREFIX.getId(),
 113                     USE.ALL);
 114             options.put(CLIOptions.MAC_SIGNING_KEY_NAME.getId(), USE.ALL);
 115             options.put(CLIOptions.MAC_SIGNING_KEYCHAIN.getId(), USE.ALL);
 116             options.put(CLIOptions.MAC_APP_STORE_CATEGORY.getId(), USE.ALL);
 117             options.put(CLIOptions.MAC_APP_STORE_ENTITLEMENTS.getId(),
 118                     USE.ALL);
 119         }
 120 
 121         if (Platform.getPlatform() == Platform.LINUX) {
 122             options.put(CLIOptions.LINUX_BUNDLE_NAME.getId(), USE.INSTALL);
 123             options.put(CLIOptions.LINUX_DEB_MAINTAINER.getId(), USE.INSTALL);
 124             options.put(CLIOptions.LINUX_DEB_COPYRIGHT_FILE.getId(),
 125                     USE.INSTALL);
 126             options.put(CLIOptions.LINUX_CATEGORY.getId(), USE.INSTALL);
 127             options.put(CLIOptions.LINUX_RPM_LICENSE_TYPE.getId(), USE.INSTALL);
 128             options.put(CLIOptions.LINUX_PACKAGE_DEPENDENCIES.getId(),
 129                     USE.INSTALL);
 130             options.put(CLIOptions.LINUX_MENU_GROUP.getId(), USE.INSTALL);
 131             options.put(CLIOptions.RELEASE.getId(), USE.INSTALL);
 132             options.put(CLIOptions.LINUX_SHORTCUT_HINT.getId(), USE.INSTALL);
 133         }
 134     }
 135 
 136     static boolean checkIfSupported(CLIOptions arg) {
 137         return options.containsKey(arg.getId());
 138     }
 139 
 140     static boolean checkIfImageSupported(CLIOptions arg) {
 141         USE use = options.get(arg.getId());
 142         return USE.ALL == use || USE.LAUNCHER == use;
 143     }
 144 
 145     static boolean checkIfInstallerSupported(CLIOptions arg) {
 146         USE use = options.get(arg.getId());
 147         return USE.ALL == use || USE.INSTALL == use;
 148     }
 149 }