114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci */
1514cf0368Sopenharmony_ci#define LOG_TAG "CustomUtdJsonParser"
1614cf0368Sopenharmony_ci#include "custom_utd_json_parser.h"
1714cf0368Sopenharmony_ci#include "logger.h"
1814cf0368Sopenharmony_cinamespace OHOS {
1914cf0368Sopenharmony_cinamespace UDMF {
2014cf0368Sopenharmony_ciconstexpr const char* TYPEID = "typeId";
2114cf0368Sopenharmony_ciconstexpr const char* BELONGINGTOTYPES = "belongingToTypes";
2214cf0368Sopenharmony_ciconstexpr const char* FILE_NAME_EXTENSTENSIONS = "filenameExtensions";
2314cf0368Sopenharmony_ciconstexpr const char* MIME_TYPES = "mimeTypes";
2414cf0368Sopenharmony_ciconstexpr const char* DESCRIPTION = "description";
2514cf0368Sopenharmony_ciconstexpr const char* REFERENCE_URL = "referenceURL";
2614cf0368Sopenharmony_ciconstexpr const char* ICON_FILE = "iconFile";
2714cf0368Sopenharmony_ciconstexpr const char* OWNER = "ownerBundle";
2814cf0368Sopenharmony_ciconstexpr const char* INSTALLERS = "installerBundles";
2914cf0368Sopenharmony_ci
3014cf0368Sopenharmony_ciCustomUtdJsonParser::CustomUtdJsonParser()
3114cf0368Sopenharmony_ci{
3214cf0368Sopenharmony_ci}
3314cf0368Sopenharmony_ci
3414cf0368Sopenharmony_ciCustomUtdJsonParser::~CustomUtdJsonParser()
3514cf0368Sopenharmony_ci{
3614cf0368Sopenharmony_ci}
3714cf0368Sopenharmony_ci
3814cf0368Sopenharmony_cibool CustomUtdJsonParser::ParseStoredCustomUtdJson(const std::string &jsonData,
3914cf0368Sopenharmony_ci                                                   std::vector<TypeDescriptorCfg> &typesCfg)
4014cf0368Sopenharmony_ci{
4114cf0368Sopenharmony_ci    if (jsonData.empty()) {
4214cf0368Sopenharmony_ci        return false;
4314cf0368Sopenharmony_ci    }
4414cf0368Sopenharmony_ci
4514cf0368Sopenharmony_ci    cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
4614cf0368Sopenharmony_ci    if (jsonRoot != NULL && cJSON_IsObject(jsonRoot)) {
4714cf0368Sopenharmony_ci        GetTypeDescriptors(*jsonRoot, UTD_CUSTOM, typesCfg);
4814cf0368Sopenharmony_ci    }
4914cf0368Sopenharmony_ci    cJSON_Delete(jsonRoot);
5014cf0368Sopenharmony_ci    return true;
5114cf0368Sopenharmony_ci}
5214cf0368Sopenharmony_ci
5314cf0368Sopenharmony_cibool CustomUtdJsonParser::ParseUserCustomUtdJson(const std::string &jsonData,
5414cf0368Sopenharmony_ci                                                 std::vector<TypeDescriptorCfg> &typesDeclarations,
5514cf0368Sopenharmony_ci                                                 std::vector<TypeDescriptorCfg> &typesReference)
5614cf0368Sopenharmony_ci{
5714cf0368Sopenharmony_ci    if (jsonData.empty()) {
5814cf0368Sopenharmony_ci        return false;
5914cf0368Sopenharmony_ci    }
6014cf0368Sopenharmony_ci    // parse utd-adt.json to TypeDescriptorCfg obj
6114cf0368Sopenharmony_ci    cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
6214cf0368Sopenharmony_ci    if (jsonRoot != NULL && cJSON_IsObject(jsonRoot)) {
6314cf0368Sopenharmony_ci        GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_DECLAEEARION, typesDeclarations);
6414cf0368Sopenharmony_ci        GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_REFERENCE, typesReference);
6514cf0368Sopenharmony_ci    }
6614cf0368Sopenharmony_ci    cJSON_Delete(jsonRoot);
6714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_CLIENT, "DeclarationsSize:%{public}zu, ReferenceSize:%{public}zu",
6814cf0368Sopenharmony_ci        typesDeclarations.size(), typesReference.size());
6914cf0368Sopenharmony_ci    return true;
7014cf0368Sopenharmony_ci}
7114cf0368Sopenharmony_ci
7214cf0368Sopenharmony_cibool CustomUtdJsonParser::ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> &typesCfg, std::string &jsonData)
7314cf0368Sopenharmony_ci{
7414cf0368Sopenharmony_ci    json* root = cJSON_CreateObject();
7514cf0368Sopenharmony_ci    json* CustomUTDs = cJSON_CreateArray();
7614cf0368Sopenharmony_ci    for (auto utdTypeCfg : typesCfg) {
7714cf0368Sopenharmony_ci        json* jsonItem = cJSON_CreateObject();
7814cf0368Sopenharmony_ci        cJSON_AddStringToObject(jsonItem, TYPEID, utdTypeCfg.typeId.c_str());
7914cf0368Sopenharmony_ci        std::vector<std::string> belongingToTypes(utdTypeCfg.belongingToTypes.begin(),
8014cf0368Sopenharmony_ci                                                  utdTypeCfg.belongingToTypes.end());
8114cf0368Sopenharmony_ci        AddJsonStringArray(belongingToTypes, BELONGINGTOTYPES, *jsonItem);
8214cf0368Sopenharmony_ci        AddJsonStringArray(utdTypeCfg.filenameExtensions, FILE_NAME_EXTENSTENSIONS, *jsonItem);
8314cf0368Sopenharmony_ci        AddJsonStringArray(utdTypeCfg.mimeTypes, MIME_TYPES, *jsonItem);
8414cf0368Sopenharmony_ci        cJSON_AddStringToObject(jsonItem, DESCRIPTION, utdTypeCfg.description.c_str());
8514cf0368Sopenharmony_ci        cJSON_AddStringToObject(jsonItem, REFERENCE_URL, utdTypeCfg.referenceURL.c_str());
8614cf0368Sopenharmony_ci        cJSON_AddStringToObject(jsonItem, ICON_FILE, utdTypeCfg.iconFile.c_str());
8714cf0368Sopenharmony_ci        cJSON_AddStringToObject(jsonItem, OWNER, utdTypeCfg.ownerBundle.c_str());
8814cf0368Sopenharmony_ci        std::vector<std::string> installerBundles(utdTypeCfg.installerBundles.begin(),
8914cf0368Sopenharmony_ci                                                  utdTypeCfg.installerBundles.end());
9014cf0368Sopenharmony_ci        AddJsonStringArray(installerBundles, INSTALLERS, *jsonItem);
9114cf0368Sopenharmony_ci
9214cf0368Sopenharmony_ci        cJSON_AddItemToArray(CustomUTDs, jsonItem);
9314cf0368Sopenharmony_ci    }
9414cf0368Sopenharmony_ci    cJSON_AddItemToObject(root, UTD_CUSTOM, CustomUTDs);
9514cf0368Sopenharmony_ci
9614cf0368Sopenharmony_ci    jsonData = cJSON_Print(root);
9714cf0368Sopenharmony_ci    cJSON_Delete(root);
9814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_CLIENT, "ConvertUtdCfgsToJson, jsonData size: %{public}zu.", jsonData.length());
9914cf0368Sopenharmony_ci    return true;
10014cf0368Sopenharmony_ci}
10114cf0368Sopenharmony_ci
10214cf0368Sopenharmony_cibool CustomUtdJsonParser::AddJsonStringArray(const std::vector<std::string> &datas, const std::string &nodeName,
10314cf0368Sopenharmony_ci                                             json &node)
10414cf0368Sopenharmony_ci{
10514cf0368Sopenharmony_ci    json *arrayNode = cJSON_AddArrayToObject(&node, nodeName.c_str());
10614cf0368Sopenharmony_ci    for (const auto &data : datas) {
10714cf0368Sopenharmony_ci        json* item = cJSON_CreateString(data.c_str());
10814cf0368Sopenharmony_ci        cJSON_AddItemToArray(arrayNode, item);
10914cf0368Sopenharmony_ci    }
11014cf0368Sopenharmony_ci    return true;
11114cf0368Sopenharmony_ci}
11214cf0368Sopenharmony_ci
11314cf0368Sopenharmony_cibool CustomUtdJsonParser::GetTypeDescriptors(const json &jsonRoot, const std::string &nodeName,
11414cf0368Sopenharmony_ci                                             std::vector<TypeDescriptorCfg> &typesCfg)
11514cf0368Sopenharmony_ci{
11614cf0368Sopenharmony_ci    if (cJSON_HasObjectItem(&jsonRoot, nodeName.c_str())) {
11714cf0368Sopenharmony_ci        cJSON *subNode = cJSON_GetObjectItem(&jsonRoot, nodeName.c_str());
11814cf0368Sopenharmony_ci        int itemNum = cJSON_GetArraySize(subNode);
11914cf0368Sopenharmony_ci        for (int i = 0; i < itemNum; i++) {
12014cf0368Sopenharmony_ci            cJSON *node = cJSON_GetArrayItem(subNode, i);
12114cf0368Sopenharmony_ci            TypeDescriptorCfg typeCfg;
12214cf0368Sopenharmony_ci            typeCfg.typeId = GetStringValue(*node, TYPEID);
12314cf0368Sopenharmony_ci            typeCfg.belongingToTypes = GetStringArrayValue(*node, BELONGINGTOTYPES);
12414cf0368Sopenharmony_ci            typeCfg.filenameExtensions = GetStringArrayValue(*node, FILE_NAME_EXTENSTENSIONS);
12514cf0368Sopenharmony_ci            typeCfg.mimeTypes = GetStringArrayValue(*node, MIME_TYPES);
12614cf0368Sopenharmony_ci            typeCfg.description = GetStringValue(*node, DESCRIPTION);
12714cf0368Sopenharmony_ci            typeCfg.referenceURL = GetStringValue(*node, REFERENCE_URL);
12814cf0368Sopenharmony_ci            typeCfg.iconFile = GetStringValue(*node, ICON_FILE);
12914cf0368Sopenharmony_ci            typeCfg.ownerBundle = GetStringValue(*node, OWNER);
13014cf0368Sopenharmony_ci            std::vector<std::string> installerBundles = GetStringArrayValue(*node, INSTALLERS);
13114cf0368Sopenharmony_ci            typeCfg.installerBundles.insert(installerBundles.begin(), installerBundles.end());
13214cf0368Sopenharmony_ci            typesCfg.push_back(typeCfg);
13314cf0368Sopenharmony_ci        }
13414cf0368Sopenharmony_ci    }
13514cf0368Sopenharmony_ci    return true;
13614cf0368Sopenharmony_ci}
13714cf0368Sopenharmony_ci
13814cf0368Sopenharmony_cistd::string CustomUtdJsonParser::GetStringValue(const json &node, const std::string &nodeName)
13914cf0368Sopenharmony_ci{
14014cf0368Sopenharmony_ci    std::string value;
14114cf0368Sopenharmony_ci    if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
14214cf0368Sopenharmony_ci        cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
14314cf0368Sopenharmony_ci        if (cJSON_IsString(subNode)) {
14414cf0368Sopenharmony_ci            value = cJSON_GetStringValue(subNode);
14514cf0368Sopenharmony_ci        }
14614cf0368Sopenharmony_ci    }
14714cf0368Sopenharmony_ci    return value;
14814cf0368Sopenharmony_ci}
14914cf0368Sopenharmony_ci
15014cf0368Sopenharmony_cistd::vector<std::string> CustomUtdJsonParser::GetStringArrayValue(const json &node, const std::string &nodeName)
15114cf0368Sopenharmony_ci{
15214cf0368Sopenharmony_ci    std::vector<std::string> values;
15314cf0368Sopenharmony_ci    if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
15414cf0368Sopenharmony_ci        cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
15514cf0368Sopenharmony_ci        if (cJSON_IsNull(subNode) || !cJSON_IsArray(subNode)) {
15614cf0368Sopenharmony_ci            return values;
15714cf0368Sopenharmony_ci        }
15814cf0368Sopenharmony_ci        for (int i = 0; i < cJSON_GetArraySize(subNode); i++) {
15914cf0368Sopenharmony_ci            json *item = cJSON_GetArrayItem(subNode, i);
16014cf0368Sopenharmony_ci            if (cJSON_IsString(item)) {
16114cf0368Sopenharmony_ci                values.emplace_back(cJSON_GetStringValue(item));
16214cf0368Sopenharmony_ci            }
16314cf0368Sopenharmony_ci        }
16414cf0368Sopenharmony_ci    }
16514cf0368Sopenharmony_ci    return values;
16614cf0368Sopenharmony_ci}
16714cf0368Sopenharmony_ci} // namespace UDMF
16814cf0368Sopenharmony_ci} // namespace OHOS