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