1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos; 17 18 import java.util.AbstractMap; 19 import java.util.HashMap; 20 import java.util.Map; 21 import java.util.function.Function; 22 23 /** 24 * command parser. 25 * 26 */ 27 public class CommandParser { 28 /** 29 * Parses and returns the hap list that supports the device type. 30 */ 31 public static final String PARSE_MODE_HAPLIST = "hap-list"; 32 33 /** 34 * Parses and returns the information about the hap. 35 */ 36 public static final String PARSE_MODE_HAPINFO = "hap-info"; 37 38 private static final String CMD_MODE = "--mode"; 39 private static final String CMD_JSON_PATH = "--json-path"; 40 private static final String CMD_PROFILE_PATH = "--profile-path"; 41 private static final String CMD_INDEX_PATH = "--index-path"; 42 private static final String CMD_JS_PATH = "--js-path"; 43 private static final String CMD_ETS_PATH = "--ets-path"; 44 private static final String CMD_HNP_PATH = "--hnp-path"; 45 private static final String CMD_RPCID_PATH = "--rpcid-path"; 46 private static final String CMD_RPCID = "--rpcid"; 47 private static final String CMD_LIBS = "--libs"; 48 private static final String CMD_CPU_ABIS = "--cpu-abis"; 49 private static final String CMD_SO_PATH = "--maple-so-path"; 50 private static final String CMD_SO_DIR = "--maple-so-dir"; 51 private static final String CMD_ABILITY_SO_PATH = "--ability-so-path"; 52 private static final String CMD_DEX_PATH = "--dex-path"; 53 private static final String CMD_ABC_PATH = "--abc-path"; 54 private static final String CMD_FILE_PATH = "--file-path"; 55 private static final String CMD_LIB_PATH = "--lib-path"; 56 private static final String CMD_RES_PATH = "--res-path"; 57 private static final String CMD_RESOURCES_PATH = "--resources-path"; 58 private static final String CMD_ASSETS_PATH = "--assets-path"; 59 private static final String CMD_APK_PATH = "--shell-apk-path"; 60 private static final String CMD_HAP_PATH = "--hap-path"; 61 private static final String CMD_APP_PATH = "--app-path"; 62 private static final String CMD_SIGNATURE_PATH = "--signature-path"; 63 private static final String CMD_CERTIFICATE_PATH = "--certificate-path"; 64 private static final String CMD_FORCE = "--force"; 65 private static final String CMD_OUT_PATH = "--out-path"; 66 private static final String CMD_PACK_INFO_PATH = "--pack-info-path"; 67 private static final String CMD_ENCRYPT_PATH = "--encrypt-path"; 68 private static final String CMD_BIN_PATH = "--bin-path"; 69 private static final String CMD_JAR_PATH = "--jar-path"; 70 private static final String CMD_TXT_PATH = "--txt-path"; 71 private static final String CMD_HAR_PATH = "--har-path"; 72 private static final String CMD_HSP_PATH = "--hsp-path"; 73 private static final String CMD_PARSE_MODE = "--p"; 74 private static final String CMD_PACK_RES_PATH = "--pack-res-path"; 75 private static final String CMD_UNPACKAPK = "--unpackapk"; 76 private static final String CMD_UNPACK_CUT_ENTRY_APK = "--unpack-cut_entry"; 77 private static final String CMD_SHAREDLIBS_PATH = "--shared-libs-path"; 78 private static final String CMD_ENTRYCARD_PATH = "--entrycard-path"; 79 private static final String CMD_HAP_LIST = "--hap-list"; 80 private static final String CMD_HSP_LIST = "--hsp-list"; 81 private static final String CMD_APP_LIST = "--app-list"; 82 private static final String CMD_DIR_LIST = "--dir-list"; 83 private static final String CMD_HQF_LIST = "--hqf-list"; 84 private static final String CMD_APPQF_PATH = "--appqf-path"; 85 private static final String CMD_AN_PATH = "--an-path"; 86 private static final String CMD_AP_PATH = "--ap-path"; 87 private static final String MAIN_MODULE_LIMIT = "--main-module-limit"; 88 private static final String NORMAL_MODULE_LIMIT = "--normal-module-limit"; 89 private static final String TOTAL_LIMIT = "--total-limit"; 90 private static final String VERSION_CODE = "--version-code"; 91 private static final String VERSION_NAME = "--version-name"; 92 private static final String INPUT_LIST = "--input-list"; 93 private static final String INPUT = "--input"; 94 private static final String STAT_DUPLICATE = "--stat-duplicate"; 95 private static final String STAT_SUFFIX = "--stat-suffix"; 96 private static final String STAT_FILE_SIZE = "--stat-file-size"; 97 private static final String CMD_COMPRESS_LEVEL = "--compress-level"; 98 private static final String CMD_PKG_CONTEXT_PATH = "--pkg-context-path"; 99 private static final String CMD_BUNDLE_NAME = "--bundle-name"; 100 private static final String PARSER_STAT_DUPLICATE_ERROR = "code:9132600 " + 101 "error:statDuplicate is invalid! Must be true or false."; 102 private static final String PARSER_STAT_SUFFIX_ERROR = "code:9132601 " + 103 "error:statSuffix is invalid! Must be true or false."; 104 private static final int PARSE_MODE_VALUE_LENGTH = 2; 105 private static final Log LOG = new Log(CommandParser.class.toString()); 106 private static final Map<String, Function<Map.Entry<Utility, String>, Boolean>> commandFuncs = new HashMap<>(); 107 108 static { initCommandFuncs()109 initCommandFuncs(); 110 } 111 initCommandFuncs()112 private static void initCommandFuncs() { 113 commandFuncs.put(CMD_MODE, entry -> { 114 entry.getKey().setMode(entry.getValue()); 115 return true; 116 }); 117 commandFuncs.put(CMD_JSON_PATH, entry -> { 118 entry.getKey().setJsonPath(entry.getValue()); 119 return true; 120 }); 121 commandFuncs.put(CMD_PROFILE_PATH, entry -> { 122 entry.getKey().setProfilePath(entry.getValue()); 123 return true; 124 }); 125 commandFuncs.put(CMD_INDEX_PATH, entry -> { 126 entry.getKey().setIndexPath(entry.getValue()); 127 return true; 128 }); 129 commandFuncs.put(CMD_JS_PATH, entry -> { 130 entry.getKey().setJsPath(entry.getValue()); 131 return true; 132 }); 133 commandFuncs.put(CMD_ETS_PATH, entry -> { 134 entry.getKey().setEtsPath(entry.getValue()); 135 return true; 136 }); 137 commandFuncs.put(CMD_HNP_PATH, entry -> { 138 entry.getKey().setHnpPath(entry.getValue()); 139 return true; 140 }); 141 commandFuncs.put(CMD_RPCID_PATH, entry -> { 142 entry.getKey().setRpcidPath(entry.getValue()); 143 return true; 144 }); 145 commandFuncs.put(CMD_RPCID, entry -> { 146 entry.getKey().setRpcid(entry.getValue()); 147 return true; 148 }); 149 commandFuncs.put(CMD_LIBS, entry -> { 150 entry.getKey().setLibs(entry.getValue()); 151 return true; 152 }); 153 commandFuncs.put(CMD_CPU_ABIS, entry -> { 154 entry.getKey().setCpuAbis(entry.getValue()); 155 return true; 156 }); 157 commandFuncs.put(CMD_SO_PATH, entry -> { 158 entry.getKey().setSoPath(entry.getValue()); 159 return true; 160 }); 161 commandFuncs.put(CMD_SO_DIR, entry -> { 162 entry.getKey().setSoDir(entry.getValue()); 163 return true; 164 }); 165 commandFuncs.put(CMD_ABILITY_SO_PATH, entry -> { 166 entry.getKey().setAbilitySoPath(entry.getValue()); 167 return true; 168 }); 169 commandFuncs.put(CMD_DEX_PATH, entry -> { 170 entry.getKey().setDexPath(entry.getValue()); 171 return true; 172 }); 173 commandFuncs.put(CMD_ABC_PATH, entry -> { 174 entry.getKey().setAbcPath(entry.getValue()); 175 return true; 176 }); 177 commandFuncs.put(CMD_FILE_PATH, entry -> { 178 entry.getKey().setFilePath(entry.getValue()); 179 return true; 180 }); 181 commandFuncs.put(CMD_LIB_PATH, entry -> { 182 entry.getKey().setLibPath(entry.getValue()); 183 return true; 184 }); 185 commandFuncs.put(CMD_RES_PATH, entry -> { 186 entry.getKey().setResPath(entry.getValue()); 187 return true; 188 }); 189 commandFuncs.put(CMD_RESOURCES_PATH, entry -> { 190 entry.getKey().setResourcesPath(entry.getValue()); 191 return true; 192 }); 193 commandFuncs.put(CMD_ASSETS_PATH, entry -> { 194 entry.getKey().setAssetsPath(entry.getValue()); 195 return true; 196 }); 197 commandFuncs.put(CMD_APK_PATH, entry -> { 198 entry.getKey().setApkPath(entry.getValue()); 199 return true; 200 }); 201 commandFuncs.put(CMD_HAP_PATH, entry -> { 202 entry.getKey().setHapPath(entry.getValue()); 203 return true; 204 }); 205 commandFuncs.put(CMD_APP_PATH, entry -> { 206 entry.getKey().setAppPath(entry.getValue()); 207 return true; 208 }); 209 commandFuncs.put(CMD_SIGNATURE_PATH, entry -> { 210 entry.getKey().setSignaturePath(entry.getValue()); 211 return true; 212 }); 213 commandFuncs.put(CMD_CERTIFICATE_PATH, entry -> { 214 entry.getKey().setCertificatePath(entry.getValue()); 215 return true; 216 }); 217 commandFuncs.put(CMD_FORCE, entry -> { 218 entry.getKey().setForceRewrite(entry.getValue()); 219 return true; 220 }); 221 commandFuncs.put(CMD_OUT_PATH, entry -> { 222 entry.getKey().setOutPath(entry.getValue()); 223 return true; 224 }); 225 commandFuncs.put(CMD_PACK_INFO_PATH, entry -> { 226 entry.getKey().setPackInfoPath(entry.getValue()); 227 return true; 228 }); 229 commandFuncs.put(CMD_ENCRYPT_PATH, entry -> { 230 entry.getKey().setEncryptPath(entry.getValue()); 231 return true; 232 }); 233 commandFuncs.put(CMD_BIN_PATH, entry -> { 234 entry.getKey().setBinPath(entry.getValue()); 235 return true; 236 }); 237 commandFuncs.put(CMD_JAR_PATH, entry -> { 238 entry.getKey().setJarPath(entry.getValue()); 239 return true; 240 }); 241 commandFuncs.put(CMD_TXT_PATH, entry -> { 242 entry.getKey().setTxtPath(entry.getValue()); 243 return true; 244 }); 245 commandFuncs.put(CMD_HAR_PATH, entry -> { 246 entry.getKey().setHarPath(entry.getValue()); 247 return true; 248 }); 249 commandFuncs.put(CMD_HSP_PATH, entry -> { 250 entry.getKey().setHspPath(entry.getValue()); 251 return true; 252 }); 253 commandFuncs.put(CMD_PACK_RES_PATH, entry -> { 254 entry.getKey().setPackResPath(entry.getValue()); 255 return true; 256 }); 257 commandFuncs.put(CMD_UNPACKAPK, entry -> { 258 entry.getKey().setUnpackApk(entry.getValue()); 259 return true; 260 }); 261 commandFuncs.put(CMD_UNPACK_CUT_ENTRY_APK, entry -> { 262 entry.getKey().setUnpackCutEntryApk(entry.getValue()); 263 return true; 264 }); 265 commandFuncs.put(CMD_SHAREDLIBS_PATH, entry -> { 266 entry.getKey().setSharedLibsPath(entry.getValue()); 267 return true; 268 }); 269 commandFuncs.put(CMD_ENTRYCARD_PATH, entry -> { 270 entry.getKey().setEntryCardPath(entry.getValue()); 271 return true; 272 }); 273 commandFuncs.put(CMD_HAP_LIST, entry -> { 274 entry.getKey().setHapList(entry.getValue()); 275 return true; 276 }); 277 commandFuncs.put(CMD_HSP_LIST, entry -> { 278 entry.getKey().setHspList(entry.getValue()); 279 return true; 280 }); 281 commandFuncs.put(CMD_APP_LIST, entry -> { 282 entry.getKey().setAppList(entry.getValue()); 283 return true; 284 }); 285 commandFuncs.put(CMD_DIR_LIST, entry -> { 286 entry.getKey().setDirList(entry.getValue()); 287 return true; 288 }); 289 commandFuncs.put(CMD_HQF_LIST, entry -> { 290 entry.getKey().setHqfList(entry.getValue()); 291 return true; 292 }); 293 commandFuncs.put(CMD_APPQF_PATH, entry -> { 294 entry.getKey().setAPPQFPath(entry.getValue()); 295 return true; 296 }); 297 commandFuncs.put(CMD_AN_PATH, entry -> { 298 entry.getKey().setANPath(entry.getValue()); 299 return true; 300 }); 301 commandFuncs.put(CMD_AP_PATH, entry -> { 302 entry.getKey().setAPPath(entry.getValue()); 303 return true; 304 }); 305 commandFuncs.put(MAIN_MODULE_LIMIT, entry -> { 306 entry.getKey().setMainModuleLimit(entry.getValue()); 307 return true; 308 }); 309 commandFuncs.put(NORMAL_MODULE_LIMIT, entry -> { 310 entry.getKey().setNormalModuleLimit(entry.getValue()); 311 return true; 312 }); 313 commandFuncs.put(TOTAL_LIMIT, entry -> { 314 entry.getKey().setTotalLimit(entry.getValue()); 315 return true; 316 }); 317 commandFuncs.put(VERSION_CODE, entry -> { 318 try { 319 entry.getKey().setVersionCode(Integer.parseInt(entry.getValue())); 320 } catch (NumberFormatException ignored) { 321 LOG.error("CommandParser::--version-code value must be number."); 322 return false; 323 } 324 return true; 325 }); 326 commandFuncs.put(VERSION_NAME, entry -> { 327 entry.getKey().setVersionName(entry.getValue()); 328 return true; 329 }); 330 commandFuncs.put(INPUT_LIST, entry -> { 331 entry.getKey().setInputList(entry.getValue()); 332 return true; 333 }); 334 commandFuncs.put(INPUT, entry -> { 335 entry.getKey().setInput(entry.getValue()); 336 return true; 337 }); 338 commandFuncs.put(STAT_DUPLICATE, entry -> { 339 if (Boolean.TRUE.toString().equals(entry.getValue()) || Boolean.FALSE.toString().equals(entry.getValue())) { 340 entry.getKey().setStatDuplicate(Boolean.parseBoolean(entry.getValue())); 341 return true; 342 } else { 343 LOG.error(PARSER_STAT_DUPLICATE_ERROR); 344 return false; 345 } 346 }); 347 commandFuncs.put(STAT_SUFFIX, entry -> { 348 if (Boolean.TRUE.toString().equals(entry.getValue()) || Boolean.FALSE.toString().equals(entry.getValue())) { 349 entry.getKey().setStatSuffix(Boolean.parseBoolean(entry.getValue())); 350 return true; 351 } else { 352 LOG.error(PARSER_STAT_SUFFIX_ERROR); 353 return false; 354 } 355 }); 356 commandFuncs.put(STAT_FILE_SIZE, entry -> { 357 entry.getKey().setStatFileSize(entry.getValue()); 358 return true; 359 }); 360 commandFuncs.put(CMD_COMPRESS_LEVEL, entry -> { 361 String level = entry.getValue(); 362 try { 363 int compressLevel = Integer.parseInt(level); 364 if (compressLevel < 1 || compressLevel > 9) { 365 LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString("--compress-level value not number between 1-9.")); 366 return false; 367 } else { 368 entry.getKey().setCompressLevel(compressLevel); 369 return true; 370 } 371 } catch (NumberFormatException ex) { 372 LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString("--compress-level value not number between 1-9.")); 373 return false; 374 } 375 }); 376 commandFuncs.put(CMD_PKG_CONTEXT_PATH, entry -> { 377 entry.getKey().setPkgContextPath(entry.getValue()); 378 return true; 379 }); 380 commandFuncs.put(CMD_BUNDLE_NAME, entry -> { 381 entry.getKey().setBundleName(entry.getValue()); 382 return true; 383 }); 384 } 385 386 387 /** 388 * judge args is null and enter parser. 389 * 390 * @param utility common data 391 * @param args command line 392 * @return commandParser if input valid 393 */ commandParser(Utility utility, String[] args)394 public static boolean commandParser(Utility utility, String[] args) { 395 if (args == null) { 396 LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString("Parser args is null.")); 397 return false; 398 } 399 for (int i = 0; i < args.length - 1; ++i) { 400 String key = args[i]; 401 String value = args[i + 1]; 402 Map.Entry<Utility, String> entry = new AbstractMap.SimpleEntry<>(utility, value); 403 if (commandFuncs.get(key) != null) { 404 commandFuncs.get(key).apply(entry); 405 ++i; 406 } else if (CMD_PARSE_MODE.equals(key)) { 407 if (i + PARSE_MODE_VALUE_LENGTH >= args.length) { 408 LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString("Input wrong number value for --p command.")); 409 return false; 410 } 411 utility.setParseMode(args[i + 1]); 412 if (PARSE_MODE_HAPLIST.equals(utility.getParseMode())) { 413 utility.setDeviceType(args[i + PARSE_MODE_VALUE_LENGTH]); 414 } else if (PARSE_MODE_HAPINFO.equals(utility.getParseMode())) { 415 utility.setHapName(args[i + PARSE_MODE_VALUE_LENGTH]); 416 } 417 i += PARSE_MODE_VALUE_LENGTH; 418 } else { 419 LOG.warning(key + " is invalid!"); 420 } 421 } 422 return true; 423 } 424 } 425