diff --git a/adapter/ohos/BinaryTool.java b/adapter/ohos/BinaryTool.java index ec117de70ff3ab4e028187e40299dec3aa38e589..f887576c7c9ae43974f692b52ceb06f6648b7c02 100644 --- a/adapter/ohos/BinaryTool.java +++ b/adapter/ohos/BinaryTool.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Optional; import java.util.Queue; +import com.alibaba.fastjson.JSONObject; /** * generate binary file @@ -87,7 +88,8 @@ public class BinaryTool { * @return true: success, false: fail */ private static boolean writePackageInfo(final String filePath, RandomAccessFile appStream) { - Optional packageName = FileUtils.getBundleNameFromFileContent(JSON_FILE_NAME, filePath); + Optional packageName = getValueFromJsonFileContent(PROFILE_KEY, PACKAGE_KEY, + JSON_FILE_NAME, filePath); if (!packageName.isPresent()) { LOG.error("have no config.json or have no key of package!"); return false; @@ -157,4 +159,39 @@ public class BinaryTool { } } } + + /** + * get special value from JSON String + * + * @param key json main key + * @param subKey json sub key + * @param jsonFileName json file name + * @param filePath path which will be searched + * @return value + */ + public static Optional getValueFromJsonFileContent(final String key, final String subKey, + final String jsonFileName, final String filePath) { + Optional jsonFilePath = FileUtils.searchFile(jsonFileName, filePath); + if (!jsonFilePath.isPresent()) { + return Optional.empty(); + } + + Optional jsonStr = FileUtils.getFileContent(jsonFilePath.get()); + if (!jsonStr.isPresent()) { + return Optional.empty(); + } + + JSONObject jsonObject = JSONObject.parseObject(jsonStr.get()); + if (jsonObject == null) { + return Optional.empty(); + } + + if (!jsonObject.containsKey(key)) { + return Optional.empty(); + } + + JSONObject subObject = jsonObject.getJSONObject(key); + String value = subObject.getString(subKey); + return Optional.of(value); + } } \ No newline at end of file diff --git a/adapter/ohos/CompressVerify.java b/adapter/ohos/CompressVerify.java index afd5517a55980f6ee9c8d700f793f8ccfc5122a6..5f5ba5ce4545bc4b4bc27c0ae3cf465c6dcd3763 100644 --- a/adapter/ohos/CompressVerify.java +++ b/adapter/ohos/CompressVerify.java @@ -82,89 +82,107 @@ public class CompressVerify { * @return commandPathVerify if command valid. */ private static boolean commandPathVerify(Utility utility) { - if (Utility.MODE_HAP.equals(utility.getMode())) { - if (utility.getJsonPath().isEmpty()) { - LOG.error("CompressVerify::commandPathVerify json-path is empty!"); + switch (utility.getMode()) { + case Utility.MODE_HAP: + if (!utility.getBinPath().isEmpty() && utility.getJsonPath().isEmpty()) { + return isOutPathValid(utility, HAP_SUFFIX); + } else { + return isVerifyValidInHapCommonMode(utility) && isVerifyValidInHapMode(utility); + } + case Utility.MODE_HAR: + return isVerifyValidInHarMode(utility); + case Utility.MODE_APP: + return isVerifyValidInAppMode(utility); + case Utility.MODE_RES: + return isVerifyValidInResMode(utility); + case Utility.MODE_MULTI_APP: + return isVerifyValidInMultiAppMode(utility); + case Utility.MODE_HQF: + return isVerifyValidInHQFMode(utility); + case Utility.MODE_APPQF: + return isVerifyValidInAPPQFMode(utility); + default: + LOG.error("CompressVerify::commandVerify mode is invalid!"); return false; - } - if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE) - && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) { - LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file!"); + } + } + + private static boolean isValidRpcid(Utility utility) { + if (!utility.getRpcidPath().isEmpty()) { + File file = new File(utility.getRpcidPath()); + if (!file.isFile()) { + LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path is not a file!"); return false; } - - if (!utility.getRpcidPath().isEmpty()) { - File file = new File(utility.getRpcidPath()); - if (!file.isFile()) { - LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path is not a file!"); - return false; - } - if (!RPCID_PROFILE.equals(file.getName())) { - LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path must be rpcid.sc file!"); - return false; - } + if (!RPCID_PROFILE.equals(file.getName())) { + LOG.error("CompressVerify::isArgsValidInHapMode rpcid-path must be rpcid.sc file!"); + return false; } + } + return true; + } - // compress pack.info into hap - if (!utility.getPackInfoPath().isEmpty()) { - File file = new File(utility.getPackInfoPath()); - if (!file.isFile()) { - LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path is not a file!"); - return false; - } - if (!PACK_INFO.equals(file.getName())) { - LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path must be pack.info file!"); - return false; - } + private static boolean isValidPackInfo(Utility utility) { + if (!utility.getPackInfoPath().isEmpty()) { + File file = new File(utility.getPackInfoPath()); + if (!file.isFile()) { + LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path is not a file!"); + return false; } - - if (!utility.getApkPath().isEmpty() && !compatibleProcess(utility, utility.getApkPath(), - utility.getFormattedApkPathList(), APK_SUFFIX)) { - LOG.error("CompressVerify::isArgsValidInHapMode shell-apk-path is invalid!"); + if (!PACK_INFO.equals(file.getName())) { + LOG.error("CompressVerify::isArgsValidInHapMode --pack-info-path must be pack.info file!"); return false; } + } + return true; + } - if (!utility.getProfilePath().isEmpty()) { - File file = new File(utility.getProfilePath()); - if (!file.isFile() || !PROFILE_NAME.equals(file.getName())) { - LOG.error("CompressVerify::isArgsValidInHapMode profile-path must be CAPABILITY.profile file!"); - return false; - } - } + private static boolean isVerifyValidInHapCommonMode(Utility utility) { + if (utility.getJsonPath().isEmpty()) { + LOG.error("CompressVerify::commandPathVerify json-path is empty!"); + return false; + } + if (!isPathValid(utility.getJsonPath(), TYPE_FILE, JSON_PROFILE) + && !isPathValid(utility.getJsonPath(), TYPE_FILE, MODULE_PROFILE)) { + LOG.error("CompressVerify::isArgsValidInHarMode json-path must be config.json file!"); + return false; + } - if (!utility.getDexPath().isEmpty() && !compatibleProcess(utility, utility.getDexPath(), - utility.getFormattedDexPathList(), DEX_SUFFIX)) { - LOG.error("CompressVerify::isArgsValidInHapMode dex-path is invalid!"); - return false; - } + if (!isValidRpcid(utility) || !isValidPackInfo(utility)) { + return false; + } - if (!utility.getAbcPath().isEmpty() && !compatibleProcess(utility, utility.getAbcPath(), - utility.getFormattedAbcPathList(), ABC_SUFFIX)) { - LOG.error("CompressVerify::isArgsValidInHapMode abc-path is invalid!"); - return false; - } + if (!utility.getApkPath().isEmpty() && !compatibleProcess(utility, utility.getApkPath(), + utility.getFormattedApkPathList(), APK_SUFFIX)) { + LOG.error("CompressVerify::isArgsValidInHapMode shell-apk-path is invalid!"); + return false; + } - if (!utility.getDirList().isEmpty() && !splitDirList(utility, utility.getDirList(), utility.getFormatedDirList())) { - LOG.error("CompressVerify::isArgsValidInHapMode --dir-list is invalid!"); + if (!utility.getProfilePath().isEmpty()) { + File file = new File(utility.getProfilePath()); + if (!file.isFile() || !PROFILE_NAME.equals(file.getName())) { + LOG.error("CompressVerify::isArgsValidInHapMode profile-path must be CAPABILITY.profile file!"); return false; } - return isVerifyValidInHapMode(utility); - } else if (Utility.MODE_HAR.equals(utility.getMode())) { - return isVerifyValidInHarMode(utility); - } else if (Utility.MODE_APP.equals(utility.getMode())) { - return isVerifyValidInAppMode(utility); - } else if (Utility.MODE_RES.equals(utility.getMode())) { - return isVerifyValidInResMode(utility); - } else if (Utility.MODE_MULTI_APP.equals(utility.getMode())) { - return isVerifyValidInMultiAppMode(utility); - } else if (Utility.MODE_HQF.equals(utility.getMode())) { - return isVerifyValidInHQFMode(utility); - } else if (Utility.MODE_APPQF.equals(utility.getMode())) { - return isVerifyValidInAPPQFMode(utility); - } else { - LOG.error("CompressVerify::commandVerify mode is invalid!"); + } + + if (!utility.getDexPath().isEmpty() && !compatibleProcess(utility, utility.getDexPath(), + utility.getFormattedDexPathList(), DEX_SUFFIX)) { + LOG.error("CompressVerify::isArgsValidInHapMode dex-path is invalid!"); return false; } + + if (!utility.getAbcPath().isEmpty() && !compatibleProcess(utility, utility.getAbcPath(), + utility.getFormattedAbcPathList(), ABC_SUFFIX)) { + LOG.error("CompressVerify::isArgsValidInHapMode abc-path is invalid!"); + return false; + } + + if (!utility.getDirList().isEmpty() && !splitDirList(utility, utility.getDirList(), utility.getFormatedDirList())) { + LOG.error("CompressVerify::isArgsValidInHapMode --dir-list is invalid!"); + return false; + } + return true; } /** @@ -234,12 +252,6 @@ public class CompressVerify { return false; } - - if (!utility.getPackInfoPath().isEmpty() && !isPathValid(utility.getPackInfoPath(), TYPE_FILE, PACK_INFO)) { - LOG.error("CompressVerify::isArgsValidInHapMode pack-info-path is invalid!"); - return false; - } - if (isHapPathValid(utility.getANPath())) { LOG.error("CompressVerify::isArgsValidInHapMode an-path is invalid!"); return false; diff --git a/adapter/ohos/FileUtils.java b/adapter/ohos/FileUtils.java index 260d23551fd98145997d6b27ce15240f0eb21473..ee132d61a21997840b07980936ce8a07819b14cc 100644 --- a/adapter/ohos/FileUtils.java +++ b/adapter/ohos/FileUtils.java @@ -46,11 +46,7 @@ class FileUtils { private static final int SHA256_BUFFER_SIZE = 10240; private static final Log LOG = new Log(FileUtils.class.toString()); private static final String RESOURCE_PATH = "resources/base/profile/"; - private static final String MODULE_JSON = "module.json"; - private static final String CONFIG_JSON = "config.json"; private static final String SHA256 = "SHA-256"; - private static final String BUNDLE_NAME = "\"bundleName\":"; - private static final char QUATATION = '\"'; /** * generate fileData byte stream @@ -90,7 +86,7 @@ class FileUtils { * @param directory dir path * @return filePath */ - private static Optional searchFile(final String fileName, final String directory) { + public static Optional searchFile(final String fileName, final String directory) { ArrayList fileList = new ArrayList<>(); getFileList(directory, fileList); for (String fileItem : fileList) { @@ -140,7 +136,7 @@ class FileUtils { * @param filePath file path * @return String for file */ - private static Optional getFileContent(final String filePath) { + public static Optional getFileContent(final String filePath) { if (filePath.isEmpty()) { return Optional.empty(); } @@ -518,47 +514,6 @@ class FileUtils { return hexString.toString(); } - /** - * get bundle name for file content - * - * @param jsonFileName is file name in input directory - * @param filePath is the input directory - */ - public static Optional getBundleNameFromFileContent(final String jsonFileName, final String filePath) { - Optional jsonFilePath = searchFile(jsonFileName, filePath); - if (!jsonFilePath.isPresent()) { - return Optional.empty(); - } - - Optional jsonOptional = getFileContent(jsonFilePath.get()); - if (!jsonOptional.isPresent()) { - return Optional.empty(); - } - String jsonStr = jsonOptional.get().replaceAll(System.getProperty("line.separator"), ""); - return Optional.of(getBundleName(jsonStr)); - } - - private static String getBundleName(String jsonStr) { - String realStr = jsonStr.replaceAll(" ", ""); - if (!realStr.contains(BUNDLE_NAME)) { - return ""; - } - int index = realStr.indexOf(BUNDLE_NAME); - String res = ""; - index += BUNDLE_NAME.length(); - int left = index; - while (realStr.charAt(left) != QUATATION) { - ++left; - } - ++left; - int right = left; - while (realStr.charAt(right) != QUATATION) { - ++right; - } - res = realStr.substring(left, right); - return res; - } - /** * unzip file * diff --git a/haptobin.sh b/haptobin.sh index a5e48e685fddcad5bc0b2c87ea050a0922fad263..66e5ea749d3dc96f64f1094cade01ced3b4316d4 100755 --- a/haptobin.sh +++ b/haptobin.sh @@ -23,6 +23,7 @@ jar_dir="jar" haptobin_jar_file="haptobin_tool.jar" haptobin_jar_path="${final_path}/${out_build_path}" haptobin_jar_file_path="${final_path}/${haptobin_build_jar_path}" +fastjson_jar_path="${root_path}/jar/fastjson-1.2.83.jar" # make out dir if [ -d "${haptobin_jar_path}" ] then @@ -59,7 +60,7 @@ do java_collection="${java_collection} ${root_path}/adapter/ohos/${compile_class[$i]}${java_suffix}" done -compile_command="javac -source 1.8 -target 1.8 -d ${out_dir} ${java_collection}" +compile_command="javac -source 1.8 -target 1.8 -cp ${fastjson_jar_path} -d ${out_dir} ${java_collection}" eval ${compile_command} cd ${out_dir} temp_jar_path="${root_path}/jar/haptobin_${toolchain}/${haptobin_jar_file}"