1 /* 2 * Copyright (c) 2021 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.io.Closeable; 19 import java.io.File; 20 import java.io.IOException; 21 import java.util.ArrayList; 22 import java.util.List; 23 import java.util.Locale; 24 25 /** 26 * common resource. 27 * 28 */ 29 public 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_RES = "res"; 34 static final String FALSE_STRING = "false"; 35 36 private static final String CMD_PREFIX = "--"; 37 private static final Log LOG = new Log(Utility.class.toString()); 38 39 private String mode = ""; 40 private String jsonPath = ""; 41 private String profilePath = ""; 42 private String soPath = ""; 43 private String soDir = ""; 44 private String abilitySoPath = ""; 45 private String dexPath = ""; 46 private String abcPath = ""; 47 private String indexPath = ""; 48 private String hapPath = ""; 49 private String appPath = ""; 50 private String libPath = ""; 51 private String resPath = ""; 52 private String resourcesPath = ""; 53 private String assetsPath = ""; 54 private String apkPath = ""; 55 private String certificatePath = ""; 56 private String signaturePath = ""; 57 private String forceRewrite = "false"; 58 private String outPath = ""; 59 private String packInfoPath = ""; 60 private String binPath = ""; 61 private boolean isCompressNativeLibs = true; 62 private String moduleName = ""; 63 private String harPath = ""; 64 private String jarPath = ""; 65 private String txtPath = ""; 66 private String parseMode = ""; 67 private String deviceType = ""; 68 private String hapName = ""; 69 private String sharedLibsPath = ""; 70 private String unpackApk = "false"; 71 private String unpackCutEntryApk = "false"; 72 private String entryCardPath = ""; 73 private String packRes = ""; 74 private String packResPath = ""; 75 private String filePath = ""; 76 private String jsPath = ""; 77 private String etsPath = ""; 78 private String rpcidPath = ""; 79 private int VersionCode = -1; 80 private String VersionName = ""; 81 private boolean isModuleJson = false; 82 83 private List<String> formattedSoPathList = new ArrayList<>(); 84 private List<String> formattedAbilitySoPathList = new ArrayList<>(); 85 private List<String> formattedDexPathList = new ArrayList<>(); 86 private List<String> formattedAbcPathList = new ArrayList<>(); 87 private List<String> formattedHapPathList = new ArrayList<>(); 88 private List<String> formattedApkPathList = new ArrayList<>(); 89 private List<String> formattedJarPathList = new ArrayList<>(); 90 private List<String> formattedTxtPathList = new ArrayList<>(); 91 private List<String> formattedEntryCardPathList = new ArrayList<>(); 92 private List<String> formNameList = new ArrayList<>(); 93 getMode()94 public String getMode() { 95 return mode; 96 } 97 setMode(String mode)98 public void setMode(String mode) { 99 if (!mode.startsWith(CMD_PREFIX)) { 100 this.mode = mode.toLowerCase(Locale.ENGLISH); 101 } 102 } 103 getJsonPath()104 public String getJsonPath() { 105 return jsonPath; 106 } 107 setJsonPath(String jsonPath)108 public void setJsonPath(String jsonPath) { 109 if (!jsonPath.startsWith(CMD_PREFIX)) { 110 this.jsonPath = getFormattedPath(jsonPath); 111 } 112 } 113 getProfilePath()114 public String getProfilePath() { 115 return profilePath; 116 } 117 setProfilePath(String profilePath)118 public void setProfilePath(String profilePath) { 119 if (!profilePath.startsWith(CMD_PREFIX)) { 120 this.profilePath = getFormattedPath(profilePath); 121 } 122 } 123 getSoPath()124 public String getSoPath() { 125 return soPath; 126 } 127 setSoPath(String soPath)128 public void setSoPath(String soPath) { 129 if (!soPath.startsWith(CMD_PREFIX)) { 130 this.soPath = soPath; 131 } 132 } 133 getAbilitySoPath()134 public String getAbilitySoPath() { 135 return abilitySoPath; 136 } 137 setAbilitySoPath(String abilitySoPath)138 public void setAbilitySoPath(String abilitySoPath) { 139 if (!abilitySoPath.startsWith(CMD_PREFIX)) { 140 this.abilitySoPath = abilitySoPath; 141 } 142 } 143 getSoDir()144 public String getSoDir() { 145 return soDir; 146 } 147 setSoDir(String soDir)148 public void setSoDir(String soDir) { 149 if (!soDir.startsWith(CMD_PREFIX)) { 150 this.soDir = soDir; 151 } 152 } 153 getDexPath()154 public String getDexPath() { 155 return dexPath; 156 } 157 setDexPath(String dexPath)158 public void setDexPath(String dexPath) { 159 if (!dexPath.startsWith(CMD_PREFIX)) { 160 this.dexPath = dexPath; 161 } 162 } 163 getAbcPath()164 public String getAbcPath() { 165 return abcPath; 166 } 167 setAbcPath(String abcPath)168 public void setAbcPath(String abcPath) { 169 if (!abcPath.startsWith(CMD_PREFIX)) { 170 this.abcPath = abcPath; 171 } 172 } 173 getFilePath()174 public String getFilePath() { 175 return filePath; 176 } 177 setFilePath(String filePath)178 public void setFilePath(String filePath) { 179 if (!filePath.startsWith(CMD_PREFIX)) { 180 this.filePath = filePath; 181 } 182 } 183 getIndexPath()184 public String getIndexPath() { 185 return indexPath; 186 } 187 setIndexPath(String indexPath)188 public void setIndexPath(String indexPath) { 189 if (!indexPath.startsWith(CMD_PREFIX)) { 190 this.indexPath = getFormattedPath(indexPath); 191 } 192 } 193 getBinPath()194 public String getBinPath() { 195 return binPath; 196 } 197 setBinPath(String binPath)198 public void setBinPath(String binPath) { 199 if (!binPath.startsWith(CMD_PREFIX)) { 200 this.binPath = getFormattedPath(binPath); 201 } 202 } 203 setIsModuleJson(boolean isModuleJson)204 public void setIsModuleJson(boolean isModuleJson) { 205 this.isModuleJson = isModuleJson; 206 } 207 getIsModuleJson()208 public boolean getIsModuleJson() { 209 return isModuleJson; 210 } 211 isCompressNativeLibs()212 public boolean isCompressNativeLibs() { 213 return isCompressNativeLibs; 214 } 215 setIsCompressNativeLibs(boolean isCompress)216 public void setIsCompressNativeLibs(boolean isCompress) { 217 this.isCompressNativeLibs = isCompress; 218 } 219 getHapPath()220 public String getHapPath() { 221 return hapPath; 222 } 223 setHapPath(String hapPath)224 public void setHapPath(String hapPath) { 225 if (!hapPath.startsWith(CMD_PREFIX)) { 226 this.hapPath = hapPath; 227 } 228 } 229 getAppPath()230 public String getAppPath() { 231 return appPath; 232 } 233 setAppPath(String appPath)234 public void setAppPath(String appPath) { 235 if (!appPath.startsWith(CMD_PREFIX)) { 236 this.appPath = appPath; 237 } 238 } 239 getLibPath()240 public String getLibPath() { 241 return libPath; 242 } 243 setLibPath(String libPath)244 public void setLibPath(String libPath) { 245 if (!libPath.startsWith(CMD_PREFIX)) { 246 this.libPath = getFormattedPath(libPath); 247 } 248 } 249 getResPath()250 public String getResPath() { 251 return resPath; 252 } 253 setResPath(String resPath)254 public void setResPath(String resPath) { 255 if (!resPath.startsWith(CMD_PREFIX)) { 256 this.resPath = getFormattedPath(resPath); 257 } 258 } 259 getResourcesPath()260 public String getResourcesPath() { 261 return resourcesPath; 262 } 263 setResourcesPath(String resourcesPath)264 public void setResourcesPath(String resourcesPath) { 265 if (!resourcesPath.startsWith(CMD_PREFIX)) { 266 this.resourcesPath = getFormattedPath(resourcesPath); 267 } 268 } 269 getAssetsPath()270 public String getAssetsPath() { 271 return assetsPath; 272 } 273 setAssetsPath(String assetsPath)274 public void setAssetsPath(String assetsPath) { 275 if (!assetsPath.startsWith(CMD_PREFIX)) { 276 this.assetsPath = getFormattedPath(assetsPath); 277 } 278 } 279 getModuleName()280 public String getModuleName() { 281 if (moduleName == null) { 282 moduleName = ""; 283 } 284 return moduleName; 285 } 286 setModuleName(String moduleName)287 public void setModuleName(String moduleName) { 288 this.moduleName = moduleName; 289 } 290 getApkPath()291 public String getApkPath() { 292 return apkPath; 293 } 294 setApkPath(String apkPath)295 public void setApkPath(String apkPath) { 296 if (!apkPath.startsWith(CMD_PREFIX)) { 297 this.apkPath = apkPath; 298 } 299 } 300 getCertificatePath()301 public String getCertificatePath() { 302 return certificatePath; 303 } 304 setCertificatePath(String certificatePath)305 public void setCertificatePath(String certificatePath) { 306 if (!certificatePath.startsWith(CMD_PREFIX)) { 307 this.certificatePath = getFormattedPath(certificatePath); 308 } 309 } 310 getSignaturePath()311 public String getSignaturePath() { 312 return signaturePath; 313 } 314 setSignaturePath(String signaturePath)315 public void setSignaturePath(String signaturePath) { 316 if (!signaturePath.startsWith(CMD_PREFIX)) { 317 this.signaturePath = getFormattedPath(signaturePath); 318 } 319 } 320 getForceRewrite()321 public String getForceRewrite() { 322 return forceRewrite; 323 } 324 setForceRewrite(String forceRewrite)325 public void setForceRewrite(String forceRewrite) { 326 if (!forceRewrite.startsWith(CMD_PREFIX)) { 327 this.forceRewrite = forceRewrite.toLowerCase(Locale.ENGLISH); 328 } 329 } 330 getOutPath()331 public String getOutPath() { 332 return outPath; 333 } 334 setOutPath(String outPath)335 public void setOutPath(String outPath) { 336 if (!outPath.startsWith(CMD_PREFIX)) { 337 this.outPath = getFormattedPath(outPath); 338 } 339 } 340 getPackInfoPath()341 public String getPackInfoPath() { 342 return packInfoPath; 343 } 344 setPackInfoPath(String packInfoPath)345 public void setPackInfoPath(String packInfoPath) { 346 if (!packInfoPath.startsWith(CMD_PREFIX)) { 347 this.packInfoPath = getFormattedPath(packInfoPath); 348 } 349 } 350 getJarPath()351 public String getJarPath() { 352 return jarPath; 353 } 354 setJarPath(String jarPath)355 public void setJarPath(String jarPath) { 356 if (!jarPath.startsWith(CMD_PREFIX)) { 357 this.jarPath = jarPath; 358 } 359 } 360 getTxtPath()361 public String getTxtPath() { 362 return txtPath; 363 } 364 setTxtPath(String txtPath)365 public void setTxtPath(String txtPath) { 366 if (!txtPath.startsWith(CMD_PREFIX)) { 367 this.txtPath = txtPath; 368 } 369 } 370 getHarPath()371 public String getHarPath() { 372 return harPath; 373 } 374 setHarPath(String harPath)375 public void setHarPath(String harPath) { 376 if (!harPath.startsWith(CMD_PREFIX)) { 377 this.harPath = harPath; 378 } 379 } 380 getParseMode()381 public String getParseMode() { 382 return parseMode; 383 } 384 setParseMode(String parseMode)385 public void setParseMode(String parseMode) { 386 if (!parseMode.startsWith(CMD_PREFIX)) { 387 this.parseMode = parseMode; 388 } 389 } 390 getDeviceType()391 public String getDeviceType() { 392 return deviceType; 393 } 394 setDeviceType(String deviceType)395 public void setDeviceType(String deviceType) { 396 if (!deviceType.startsWith(CMD_PREFIX)) { 397 this.deviceType = deviceType; 398 } 399 } 400 getUnpackApk()401 public String getUnpackApk() { 402 return unpackApk; 403 } 404 setUnpackApk(String unpackApk)405 public void setUnpackApk(String unpackApk) { 406 this.unpackApk = unpackApk; 407 } 408 getUnpackCutEntryApk()409 public String getUnpackCutEntryApk() { 410 return unpackCutEntryApk; 411 } 412 setUnpackCutEntryApk(String unpackCutEntryApk)413 public void setUnpackCutEntryApk(String unpackCutEntryApk) { 414 this.unpackCutEntryApk = unpackCutEntryApk; 415 } 416 getHapName()417 public String getHapName() { 418 return hapName; 419 } 420 setHapName(String hapName)421 public void setHapName(String hapName) { 422 if (!hapName.startsWith(CMD_PREFIX)) { 423 this.hapName = hapName; 424 } 425 } 426 getSharedLibsPath()427 public String getSharedLibsPath() { 428 return sharedLibsPath; 429 } 430 setSharedLibsPath(String sharedLibsPath)431 public void setSharedLibsPath(String sharedLibsPath) { 432 if (!sharedLibsPath.startsWith(CMD_PREFIX)) { 433 this.sharedLibsPath = sharedLibsPath; 434 } 435 } 436 getEntryCardPath()437 public String getEntryCardPath() { 438 return entryCardPath; 439 } 440 setEntryCardPath(String entryCardPath)441 public void setEntryCardPath(String entryCardPath) { 442 if (!entryCardPath.startsWith(CMD_PREFIX)) { 443 this.entryCardPath = entryCardPath; 444 } 445 } 446 getPackRes()447 public String getPackRes() { 448 return packRes; 449 } 450 setPackRes(String packRes)451 public void setPackRes(String packRes) { 452 this.packRes = packRes; 453 } 454 getPackResPath()455 public String getPackResPath() { 456 return packResPath; 457 } 458 setPackResPath(String packResPath)459 public void setPackResPath(String packResPath) { 460 this.packResPath = packResPath; 461 } 462 getJsPath()463 public String getJsPath() { return jsPath; } 464 setJsPath(String jsPath)465 public void setJsPath(String jsPath) { this.jsPath = jsPath; } 466 getEtsPath()467 public String getEtsPath() { return etsPath; } 468 setEtsPath(String etsPath)469 public void setEtsPath(String etsPath) { this.etsPath = etsPath; } 470 getRpcidPath()471 public String getRpcidPath() { 472 return rpcidPath; 473 } 474 setRpcidPath(String rpcidPath)475 public void setRpcidPath(String rpcidPath) { 476 this.rpcidPath = rpcidPath; 477 } 478 getVersionName()479 public String getVersionName() { 480 return VersionName; 481 } 482 setVersionName(String VersionName)483 public void setVersionName(String VersionName) { 484 this.VersionName = VersionName; 485 } 486 getVersionCode()487 public int getVersionCode() { 488 return VersionCode; 489 } 490 setVersionCode(int VersionCode)491 public void setVersionCode(int VersionCode) { 492 this.VersionCode = VersionCode; 493 } 494 getFormattedSoPathList()495 public List<String> getFormattedSoPathList() { 496 return formattedSoPathList; 497 } 498 getFormattedAbilitySoPathList()499 public List<String> getFormattedAbilitySoPathList() { 500 return formattedAbilitySoPathList; 501 } 502 getFormattedDexPathList()503 public List<String> getFormattedDexPathList() { 504 return formattedDexPathList; 505 } 506 getFormattedAbcPathList()507 public List<String> getFormattedAbcPathList() { 508 return formattedAbcPathList; 509 } 510 getFormattedHapPathList()511 public List<String> getFormattedHapPathList() { 512 return formattedHapPathList; 513 } 514 getFormattedApkPathList()515 public List<String> getFormattedApkPathList() { 516 return formattedApkPathList; 517 } 518 getFormattedJarPathList()519 public List<String> getFormattedJarPathList() { 520 return formattedJarPathList; 521 } 522 getFormattedTxtPathList()523 public List<String> getFormattedTxtPathList() { 524 return formattedTxtPathList; 525 } 526 527 /** 528 * get dEntryCard path 529 * 530 * @return formattedEntryCardPathList 531 */ getformattedEntryCardPathList()532 public List<String> getformattedEntryCardPathList() { 533 return formattedEntryCardPathList; 534 } 535 536 /** 537 * get canonical path 538 * 539 * @param path path input 540 * @return formatted path 541 */ getFormattedPath(String path)542 public String getFormattedPath(String path) { 543 if (path == null) { 544 return ""; 545 } 546 547 File file = new File(path); 548 String canonicalPath; 549 try { 550 canonicalPath = file.getCanonicalPath(); 551 } catch (IOException exception) { 552 canonicalPath = ""; 553 LOG.error("Utility::getFormattedPath exception," + exception.getMessage()); 554 } 555 return canonicalPath; 556 } 557 558 /** 559 * close stream. 560 * 561 * @param stream stream to close 562 */ closeStream(Closeable stream)563 static void closeStream(Closeable stream) { 564 if (stream != null) { 565 try { 566 stream.close(); 567 } catch (IOException exception) { 568 LOG.error("Utility::closeStream io close exception: " + exception.getMessage()); 569 } 570 } 571 } 572 573 /** 574 * Add form name. 575 * 576 * @param formName the name of form 577 */ addFormNameList(String formName)578 public void addFormNameList(String formName) { 579 this.formNameList.add(formName); 580 } 581 582 /** 583 * Get all form names 584 * 585 * @return all form names 586 */ getFormNameList()587 public List<String> getFormNameList() { 588 return formNameList; 589 } 590 } 591