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 16package ohos; 17 18import java.io.Closeable; 19import java.io.File; 20import java.io.IOException; 21import java.util.ArrayList; 22import java.util.List; 23import java.util.Locale; 24 25/** 26 * common resource. 27 * 28 */ 29public class Utility { 30 static final String MODE_HAP = "hap"; 31 static final String MODE_HAR = "har"; 32 static final String MODE_APP = "app"; 33 static final String MODE_FAST_APP = "fastApp"; 34 static final String MODE_MULTI_APP = "multiApp"; 35 static final String MODE_HQF = "hqf"; 36 static final String MODE_APPQF = "appqf"; 37 static final String MODE_RES = "res"; 38 static final String MODE_HSP = "hsp"; 39 static final String MODE_HAPADDITION = "hapAddition"; 40 static final String VERSION_NORMALIZE = "versionNormalize"; 41 static final String PACKAGE_NORMALIZE = "packageNormalize"; 42 static final String FALSE_STRING = "false"; 43 static final String TRUE_STRING = "true"; 44 45 private static final String INVALID_PATH = "invalid"; 46 private static final String CMD_PREFIX = "--"; 47 private static final Log LOG = new Log(Utility.class.toString()); 48 49 private String mode = ""; 50 private String jsonPath = ""; 51 private String profilePath = ""; 52 private String soPath = ""; 53 private String soDir = ""; 54 private String abilitySoPath = ""; 55 private String dexPath = ""; 56 private String abcPath = ""; 57 private String indexPath = ""; 58 private String hapPath = ""; 59 private String appPath = ""; 60 private String libPath = ""; 61 private String resPath = ""; 62 private String resourcesPath = ""; 63 private String assetsPath = ""; 64 private String apkPath = ""; 65 private String certificatePath = ""; 66 private String signaturePath = ""; 67 private String forceRewrite = "false"; 68 private String outPath = ""; 69 private String packInfoPath = ""; 70 private String encryptPath = ""; 71 private String binPath = ""; 72 private boolean isCompressNativeLibs = false; 73 private String moduleName = ""; 74 private String harPath = ""; 75 private String hspPath = ""; 76 private String jarPath = ""; 77 private String txtPath = ""; 78 private String parseMode = ""; 79 private String deviceType = ""; 80 private String hapName = ""; 81 private String sharedLibsPath = ""; 82 private String unpackApk = "false"; 83 private String unpackCutEntryApk = "false"; 84 private String entryCardPath = ""; 85 private String packRes = ""; 86 private String packResPath = ""; 87 private String filePath = ""; 88 private String jsPath = ""; 89 private String etsPath = ""; 90 private String hnpPath = ""; 91 private String rpcidPath = ""; 92 private int VersionCode = -1; 93 private String VersionName = ""; 94 private boolean isModuleJson = false; 95 private String rpcid = FALSE_STRING; 96 private boolean isParse = false; 97 private String appqfPath = ""; 98 private String anPath = ""; 99 private String apPath = ""; 100 private Boolean isSharedApp = false; 101 private Boolean isAppService = false; 102 private String mainModuleLimit = ""; 103 private String normalModuleLimit = ""; 104 private String totalLimit = ""; 105 private int compressLevel = 1; 106 private String pkgContextPath = ""; 107 private String bundleName = ""; 108 private String absoluteHapPath = ""; 109 private boolean generateBuildHash = false; 110 private boolean buildHashFinish = false; 111 112 private List<String> formattedSoPathList = new ArrayList<>(); 113 private List<String> formattedAbilitySoPathList = new ArrayList<>(); 114 private List<String> formattedDexPathList = new ArrayList<>(); 115 private List<String> formattedAbcPathList = new ArrayList<>(); 116 private List<String> formattedHapPathList = new ArrayList<>(); 117 private List<String> formattedHspPathList = new ArrayList<>(); 118 private List<String> formattedApkPathList = new ArrayList<>(); 119 private List<String> formattedJarPathList = new ArrayList<>(); 120 private List<String> formattedTxtPathList = new ArrayList<>(); 121 private List<String> formattedEntryCardPathList = new ArrayList<>(); 122 private List<String> formNameList = new ArrayList<>(); 123 private List<String> formattedAppList = new ArrayList<>(); 124 private List<String> formattedHapList = new ArrayList<>(); 125 126 private List<String> formatedDirList = new ArrayList<>(); 127 128 private List<String> formatedHQFList = new ArrayList<>(); 129 private List<String> formatedABCList = new ArrayList<>(); 130 131 private String appList = ""; 132 private String hapList = ""; 133 private String dirList = ""; 134 private String hqfList = ""; 135 private String hspList = ""; 136 private String inputList = ""; 137 private String input = ""; 138 private boolean statDuplicate = false; 139 private boolean statSuffix = false; 140 private String statFileSize = ""; 141 142 public void setIsParse(boolean isParse) { 143 this.isParse = isParse; 144 } 145 146 public boolean getIsParse() { 147 return isParse; 148 } 149 150 public String getMode() { 151 return mode; 152 } 153 154 public void setMode(String mode) { 155 if (!mode.startsWith(CMD_PREFIX)) { 156 this.mode = mode; 157 } 158 } 159 160 public String getJsonPath() { 161 return jsonPath; 162 } 163 164 public void setJsonPath(String jsonPath) { 165 if (!jsonPath.startsWith(CMD_PREFIX)) { 166 this.jsonPath = getFormattedPath(jsonPath); 167 } 168 } 169 170 public String getProfilePath() { 171 return profilePath; 172 } 173 174 public void setProfilePath(String profilePath) { 175 if (!profilePath.startsWith(CMD_PREFIX)) { 176 this.profilePath = getFormattedPath(profilePath); 177 } 178 } 179 180 public String getSoPath() { 181 return soPath; 182 } 183 184 public void setSoPath(String soPath) { 185 if (!soPath.startsWith(CMD_PREFIX)) { 186 this.soPath = soPath; 187 } 188 } 189 190 public String getAbilitySoPath() { 191 return abilitySoPath; 192 } 193 194 public void setAbilitySoPath(String abilitySoPath) { 195 if (!abilitySoPath.startsWith(CMD_PREFIX)) { 196 this.abilitySoPath = abilitySoPath; 197 } 198 } 199 200 public String getSoDir() { 201 return soDir; 202 } 203 204 public void setSoDir(String soDir) { 205 if (!soDir.startsWith(CMD_PREFIX)) { 206 this.soDir = soDir; 207 } 208 } 209 210 public String getDexPath() { 211 return dexPath; 212 } 213 214 public void setDexPath(String dexPath) { 215 if (!dexPath.startsWith(CMD_PREFIX)) { 216 this.dexPath = dexPath; 217 } 218 } 219 220 public String getAbcPath() { 221 return abcPath; 222 } 223 224 public void setAbcPath(String abcPath) { 225 if (!abcPath.startsWith(CMD_PREFIX)) { 226 this.abcPath = abcPath; 227 } 228 } 229 230 public List<String> getABCList() { 231 return formatedABCList; 232 } 233 234 public String getFilePath() { 235 return filePath; 236 } 237 238 public void setFilePath(String filePath) { 239 if (!filePath.startsWith(CMD_PREFIX)) { 240 this.filePath = filePath; 241 } 242 } 243 244 public String getIndexPath() { 245 return indexPath; 246 } 247 248 public void setIndexPath(String indexPath) { 249 if (!indexPath.startsWith(CMD_PREFIX)) { 250 this.indexPath = getFormattedPath(indexPath); 251 } 252 } 253 254 public String getBinPath() { 255 return binPath; 256 } 257 258 public void setBinPath(String binPath) { 259 if (!binPath.startsWith(CMD_PREFIX)) { 260 this.binPath = getFormattedPath(binPath); 261 } 262 } 263 264 public void setIsModuleJson(boolean isModuleJson) { 265 this.isModuleJson = isModuleJson; 266 } 267 268 public boolean getIsModuleJson() { 269 return isModuleJson; 270 } 271 272 public boolean isCompressNativeLibs() { 273 return isCompressNativeLibs; 274 } 275 276 public void setIsCompressNativeLibs(boolean isCompress) { 277 this.isCompressNativeLibs = isCompress; 278 } 279 280 public String getHapPath() { 281 return hapPath; 282 } 283 284 public void setHapPath(String hapPath) { 285 if (!hapPath.startsWith(CMD_PREFIX)) { 286 this.hapPath = hapPath; 287 if (MODE_HAPADDITION.equals(this.getMode())) { 288 this.absoluteHapPath = getFormattedPath(hapPath); 289 } 290 } 291 } 292 293 public String getAppPath() { 294 return appPath; 295 } 296 297 public void setAppPath(String appPath) { 298 if (!appPath.startsWith(CMD_PREFIX)) { 299 this.appPath = appPath; 300 } 301 } 302 303 public String getLibPath() { 304 return libPath; 305 } 306 307 public void setLibPath(String libPath) { 308 if (!libPath.startsWith(CMD_PREFIX)) { 309 this.libPath = getFormattedPath(libPath); 310 } 311 } 312 313 public String getResPath() { 314 return resPath; 315 } 316 317 public void setResPath(String resPath) { 318 if (!resPath.startsWith(CMD_PREFIX)) { 319 this.resPath = getFormattedPath(resPath); 320 } 321 } 322 323 public String getResourcesPath() { 324 return resourcesPath; 325 } 326 327 public void setResourcesPath(String resourcesPath) { 328 if (!resourcesPath.startsWith(CMD_PREFIX)) { 329 this.resourcesPath = getFormattedPath(resourcesPath); 330 } 331 } 332 333 public String getAssetsPath() { 334 return assetsPath; 335 } 336 337 public void setAssetsPath(String assetsPath) { 338 if (!assetsPath.startsWith(CMD_PREFIX)) { 339 this.assetsPath = getFormattedPath(assetsPath); 340 } 341 } 342 343 public String getModuleName() { 344 if (moduleName == null) { 345 moduleName = ""; 346 } 347 return moduleName; 348 } 349 350 public void setModuleName(String moduleName) { 351 this.moduleName = moduleName; 352 } 353 354 public String getApkPath() { 355 return apkPath; 356 } 357 358 public void setApkPath(String apkPath) { 359 if (!apkPath.startsWith(CMD_PREFIX)) { 360 this.apkPath = apkPath; 361 } 362 } 363 364 public String getCertificatePath() { 365 return certificatePath; 366 } 367 368 public void setCertificatePath(String certificatePath) { 369 if (!certificatePath.startsWith(CMD_PREFIX)) { 370 this.certificatePath = getFormattedPath(certificatePath); 371 } 372 } 373 374 public String getSignaturePath() { 375 return signaturePath; 376 } 377 378 public void setSignaturePath(String signaturePath) { 379 if (!signaturePath.startsWith(CMD_PREFIX)) { 380 this.signaturePath = getFormattedPath(signaturePath); 381 } 382 } 383 384 public String getForceRewrite() { 385 return forceRewrite; 386 } 387 388 public void setForceRewrite(String forceRewrite) { 389 if (!forceRewrite.startsWith(CMD_PREFIX)) { 390 this.forceRewrite = forceRewrite.toLowerCase(Locale.ENGLISH); 391 } 392 } 393 394 public String getOutPath() { 395 return outPath; 396 } 397 398 public void setOutPath(String outPath) { 399 if (!outPath.startsWith(CMD_PREFIX)) { 400 this.outPath = getFormattedPath(outPath); 401 } 402 } 403 404 public String getPackInfoPath() { 405 return packInfoPath; 406 } 407 408 public void setPackInfoPath(String packInfoPath) { 409 if (!packInfoPath.startsWith(CMD_PREFIX)) { 410 this.packInfoPath = getFormattedPath(packInfoPath); 411 } 412 } 413 414 public String getEncryptPath() { 415 return encryptPath; 416 } 417 418 public void setEncryptPath(String encryptPath) { 419 if (!encryptPath.startsWith(CMD_PREFIX)) { 420 this.encryptPath = getFormattedPath(encryptPath); 421 } 422 } 423 424 public String getJarPath() { 425 return jarPath; 426 } 427 428 public void setJarPath(String jarPath) { 429 if (!jarPath.startsWith(CMD_PREFIX)) { 430 this.jarPath = jarPath; 431 } 432 } 433 434 public String getTxtPath() { 435 return txtPath; 436 } 437 438 public void setTxtPath(String txtPath) { 439 if (!txtPath.startsWith(CMD_PREFIX)) { 440 this.txtPath = txtPath; 441 } 442 } 443 444 public String getHarPath() { 445 return harPath; 446 } 447 448 public void setHarPath(String harPath) { 449 if (!harPath.startsWith(CMD_PREFIX)) { 450 this.harPath = harPath; 451 } 452 } 453 454 public void setHspPath(String hspPath) { 455 if (!hspPath.startsWith(CMD_PREFIX)) { 456 this.hspPath = hspPath; 457 } 458 } 459 460 public String getHspPath() { 461 return hspPath; 462 } 463 464 public String getParseMode() { 465 return parseMode; 466 } 467 468 public void setParseMode(String parseMode) { 469 if (!parseMode.startsWith(CMD_PREFIX)) { 470 this.parseMode = parseMode; 471 } 472 } 473 474 public String getDeviceType() { 475 return deviceType; 476 } 477 478 public void setDeviceType(String deviceType) { 479 if (!deviceType.startsWith(CMD_PREFIX)) { 480 this.deviceType = deviceType; 481 } 482 } 483 484 public String getUnpackApk() { 485 return unpackApk; 486 } 487 488 public void setUnpackApk(String unpackApk) { 489 this.unpackApk = unpackApk; 490 } 491 492 public String getUnpackCutEntryApk() { 493 return unpackCutEntryApk; 494 } 495 496 public void setUnpackCutEntryApk(String unpackCutEntryApk) { 497 this.unpackCutEntryApk = unpackCutEntryApk; 498 } 499 500 public String getHapName() { 501 return hapName; 502 } 503 504 public void setHapName(String hapName) { 505 if (!hapName.startsWith(CMD_PREFIX)) { 506 this.hapName = hapName; 507 } 508 } 509 510 public String getSharedLibsPath() { 511 return sharedLibsPath; 512 } 513 514 public void setSharedLibsPath(String sharedLibsPath) { 515 if (!sharedLibsPath.startsWith(CMD_PREFIX)) { 516 this.sharedLibsPath = sharedLibsPath; 517 } 518 } 519 520 public String getEntryCardPath() { 521 return entryCardPath; 522 } 523 524 public void setEntryCardPath(String entryCardPath) { 525 if (!entryCardPath.startsWith(CMD_PREFIX)) { 526 this.entryCardPath = entryCardPath; 527 } 528 } 529 530 public String getPackRes() { 531 return packRes; 532 } 533 534 public void setPackRes(String packRes) { 535 this.packRes = packRes; 536 } 537 538 public String getPackResPath() { 539 return packResPath; 540 } 541 542 public void setPackResPath(String packResPath) { 543 this.packResPath = packResPath; 544 } 545 546 public String getJsPath() { return jsPath; } 547 548 public void setJsPath(String jsPath) { this.jsPath = jsPath; } 549 550 public String getEtsPath() { return etsPath; } 551 552 public void setEtsPath(String etsPath) { this.etsPath = etsPath; } 553 554 public String getHnpPath() { return hnpPath; } 555 556 public void setHnpPath(String hnpPath) { this.hnpPath = hnpPath; } 557 558 public String getRpcidPath() { 559 return rpcidPath; 560 } 561 562 public void setRpcidPath(String rpcidPath) { 563 this.rpcidPath = rpcidPath; 564 } 565 566 public String getRpcid() { return rpcid; } 567 568 public void setRpcid(String rpcid) { 569 this.rpcid = rpcid; 570 } 571 572 public String getVersionName() { 573 return VersionName; 574 } 575 576 public void setVersionName(String VersionName) { 577 this.VersionName = VersionName; 578 } 579 580 public int getVersionCode() { 581 return VersionCode; 582 } 583 584 public void setVersionCode(int VersionCode) { 585 this.VersionCode = VersionCode; 586 } 587 588 public List<String> getFormattedSoPathList() { 589 return formattedSoPathList; 590 } 591 592 public List<String> getFormattedAbilitySoPathList() { 593 return formattedAbilitySoPathList; 594 } 595 596 public List<String> getFormattedDexPathList() { 597 return formattedDexPathList; 598 } 599 600 public List<String> getFormattedAbcPathList() { 601 return formattedAbcPathList; 602 } 603 604 public List<String> getFormattedHapPathList() { 605 return formattedHapPathList; 606 } 607 608 public List<String> getFormattedHspPathList() { 609 return formattedHspPathList; 610 } 611 612 public List<String> getFormattedApkPathList() { 613 return formattedApkPathList; 614 } 615 616 public List<String> getFormattedJarPathList() { 617 return formattedJarPathList; 618 } 619 620 public List<String> getFormattedTxtPathList() { 621 return formattedTxtPathList; 622 } 623 624 public List<String> getFormattedAppList() { 625 return formattedAppList; 626 } 627 628 public List<String> getFormattedHapList() { 629 return formattedHapList; 630 } 631 632 public void setAppList(String appList) { 633 this.appList = appList; 634 } 635 636 public String getAppList() { 637 return this.appList; 638 } 639 640 public void setHapList(String hapList) { 641 this.hapList = hapList; 642 } 643 644 public String getHapList() { 645 return this.hapList; 646 } 647 648 public String getDirList() { 649 return this.dirList; 650 } 651 652 public void setDirList(String dirList) { 653 this.dirList = dirList; 654 } 655 656 public List<String> getFormatedDirList() { 657 return this.formatedDirList; 658 } 659 660 /** 661 * get dEntryCard path 662 * 663 * @return formattedEntryCardPathList 664 */ 665 public List<String> getformattedEntryCardPathList() { 666 return formattedEntryCardPathList; 667 } 668 669 /** 670 * get canonical path 671 * 672 * @param path path input 673 * @return formatted path 674 */ 675 public String getFormattedPath(String path) { 676 if (path == null) { 677 return ""; 678 } 679 680 File file = new File(path); 681 String canonicalPath; 682 try { 683 canonicalPath = file.getCanonicalPath(); 684 } catch (IOException exception) { 685 canonicalPath = INVALID_PATH; 686 LOG.error("Utility::getFormattedPath exception," + exception.getMessage()); 687 } 688 return canonicalPath; 689 } 690 691 /** 692 * close stream. 693 * 694 * @param stream stream to close 695 */ 696 static void closeStream(Closeable stream) { 697 if (stream != null) { 698 try { 699 stream.close(); 700 } catch (IOException exception) { 701 LOG.error("Utility::closeStream io close exception: " + exception.getMessage()); 702 } 703 } 704 } 705 706 /** 707 * Add form name. 708 * 709 * @param formName the name of form 710 */ 711 public void addFormNameList(String formName) { 712 this.formNameList.add(formName); 713 } 714 715 /** 716 * Get all form names 717 * 718 * @return all form names 719 */ 720 public List<String> getFormNameList() { 721 return formNameList; 722 } 723 724 public String getHqfList() { 725 return hqfList; 726 } 727 728 public void setHqfList(String hqfList) { 729 this.hqfList = hqfList; 730 } 731 732 public List<String> getFormatedHQFList() { 733 return formatedHQFList; 734 } 735 736 public void setFormatedHQFList(List<String> formatedHQFList) { 737 this.formatedHQFList = formatedHQFList; 738 } 739 740 public void setAPPQFPath(String appqfPath) { 741 this.appqfPath = appqfPath; 742 } 743 744 public String getAPPQFPath() { 745 return this.appqfPath; 746 } 747 748 public void setANPath(String anPath) { 749 this.anPath = anPath; 750 } 751 752 public String getANPath() { 753 return this.anPath; 754 } 755 756 public void setAPPath(String apPath) { 757 this.apPath = apPath; 758 } 759 760 public String getAPPath() { 761 return this.apPath; 762 } 763 764 public String getAbsoluteHapPath() { 765 return this.absoluteHapPath; 766 } 767 768 public void setAbsoluteHapPath(String absoluteHapPath) { 769 if (!absoluteHapPath.startsWith(CMD_PREFIX)) { 770 this.absoluteHapPath = getFormattedPath(absoluteHapPath); 771 } 772 } 773 774 public void setIsSharedApp(boolean isSharedApp) { 775 this.isSharedApp = isSharedApp; 776 } 777 778 public Boolean getSharedApp() { 779 return isSharedApp; 780 } 781 782 public void setIsAppService(boolean isAppService) { 783 this.isAppService = isAppService; 784 } 785 786 public Boolean getIsAppService() { 787 return isAppService; 788 } 789 790 public void setMainModuleLimit(String limit) { 791 this.mainModuleLimit = limit; 792 } 793 794 public String getMainModuleLimit() { 795 return mainModuleLimit; 796 } 797 798 public String getNormalModuleLimit() { 799 return normalModuleLimit; 800 } 801 802 public void setNormalModuleLimit(String limit) { 803 this.normalModuleLimit = limit; 804 } 805 806 public String getTotalLimit() { 807 return totalLimit; 808 } 809 810 public void setTotalLimit(String limit) { 811 this.totalLimit = limit; 812 } 813 814 public int getCompressLevel() { 815 return compressLevel; 816 } 817 818 public void setCompressLevel(int compressLevel) { 819 this.compressLevel = compressLevel; 820 } 821 822 public String getPkgContextPath() { 823 return pkgContextPath; 824 } 825 826 public void setPkgContextPath(String pkgContextPath) { 827 this.pkgContextPath = pkgContextPath; 828 } 829 830 public String getBundleName() { 831 return bundleName; 832 } 833 834 public void setBundleName(String bundleName) { 835 this.bundleName = bundleName; 836 } 837 838 public String getHspList() { 839 return hspList; 840 } 841 842 public void setHspList(String hspList) { 843 this.hspList = hspList; 844 } 845 846 public void setGenerateBuildHash(boolean generateBuildHash) { 847 this.generateBuildHash = generateBuildHash; 848 } 849 850 public boolean getGenerateBuildHash() { 851 return generateBuildHash; 852 } 853 854 public void setBuildHashFinish(boolean buildHashFinish) { 855 this.buildHashFinish = buildHashFinish; 856 } 857 858 public boolean isBuildHashFinish() { 859 return buildHashFinish; 860 } 861 862 public String getInputList() { 863 return inputList; 864 } 865 866 public void setInputList(String inputList) { 867 this.inputList = inputList; 868 } 869 870 public String getInput() { 871 return input; 872 } 873 874 public void setInput(String input) { 875 this.input = getFormattedPath(input); 876 } 877 878 public boolean getStatDuplicate() { 879 return statDuplicate; 880 } 881 882 public void setStatDuplicate(boolean statDuplicate) { 883 this.statDuplicate = statDuplicate; 884 } 885 886 public boolean getStatSuffix() { 887 return statSuffix; 888 } 889 890 public void setStatSuffix(boolean statSuffix) { 891 this.statSuffix = statSuffix; 892 } 893 public String getStatFileSize() { 894 return statFileSize; 895 } 896 897 public void setStatFileSize(String statFileSize) { 898 this.statFileSize = statFileSize; 899 } 900} 901