• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define LOG_TAG "CustomUtdJsonParser"
16 #include "custom_utd_json_parser.h"
17 #include "logger.h"
18 namespace OHOS {
19 namespace UDMF {
20 constexpr const char* TYPEID = "typeId";
21 constexpr const char* BELONGINGTOTYPES = "belongingToTypes";
22 constexpr const char* FILE_NAME_EXTENSTENSIONS = "filenameExtensions";
23 constexpr const char* MIME_TYPES = "mimeTypes";
24 constexpr const char* DESCRIPTION = "description";
25 constexpr const char* REFERENCE_URL = "referenceURL";
26 constexpr const char* ICON_FILE = "iconFile";
27 constexpr const char* OWNER = "ownerBundle";
28 constexpr const char* INSTALLERS = "installerBundles";
29 
CustomUtdJsonParser()30 CustomUtdJsonParser::CustomUtdJsonParser()
31 {
32 }
33 
~CustomUtdJsonParser()34 CustomUtdJsonParser::~CustomUtdJsonParser()
35 {
36 }
37 
ParseStoredCustomUtdJson(const std::string & jsonData,std::vector<TypeDescriptorCfg> & typesCfg)38 bool CustomUtdJsonParser::ParseStoredCustomUtdJson(const std::string &jsonData,
39                                                    std::vector<TypeDescriptorCfg> &typesCfg)
40 {
41     if (jsonData.empty()) {
42         return false;
43     }
44 
45     cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
46     if (jsonRoot != NULL && cJSON_IsObject(jsonRoot)) {
47         GetTypeDescriptors(*jsonRoot, UTD_CUSTOM, typesCfg);
48     }
49     cJSON_Delete(jsonRoot);
50     return true;
51 }
52 
ParseUserCustomUtdJson(const std::string & jsonData,std::vector<TypeDescriptorCfg> & typesDeclarations,std::vector<TypeDescriptorCfg> & typesReference)53 bool CustomUtdJsonParser::ParseUserCustomUtdJson(const std::string &jsonData,
54                                                  std::vector<TypeDescriptorCfg> &typesDeclarations,
55                                                  std::vector<TypeDescriptorCfg> &typesReference)
56 {
57     if (jsonData.empty()) {
58         return false;
59     }
60     // parse utd-adt.json to TypeDescriptorCfg obj
61     cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
62     if (jsonRoot != NULL && cJSON_IsObject(jsonRoot)) {
63         GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_DECLAEEARION, typesDeclarations);
64         GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_REFERENCE, typesReference);
65     }
66     cJSON_Delete(jsonRoot);
67     return true;
68 }
69 
ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> & typesCfg,std::string & jsonData)70 bool CustomUtdJsonParser::ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> &typesCfg, std::string &jsonData)
71 {
72     json* root = cJSON_CreateObject();
73     json* CustomUTDs = cJSON_CreateArray();
74     for (auto utdTypeCfg : typesCfg) {
75         json* jsonItem = cJSON_CreateObject();
76         cJSON_AddStringToObject(jsonItem, TYPEID, utdTypeCfg.typeId.c_str());
77         std::vector<std::string> belongingToTypes(utdTypeCfg.belongingToTypes.begin(),
78                                                   utdTypeCfg.belongingToTypes.end());
79         AddJsonStringArray(belongingToTypes, BELONGINGTOTYPES, *jsonItem);
80         AddJsonStringArray(utdTypeCfg.filenameExtensions, FILE_NAME_EXTENSTENSIONS, *jsonItem);
81         AddJsonStringArray(utdTypeCfg.mimeTypes, MIME_TYPES, *jsonItem);
82         cJSON_AddStringToObject(jsonItem, DESCRIPTION, utdTypeCfg.description.c_str());
83         cJSON_AddStringToObject(jsonItem, REFERENCE_URL, utdTypeCfg.referenceURL.c_str());
84         cJSON_AddStringToObject(jsonItem, ICON_FILE, utdTypeCfg.iconFile.c_str());
85         cJSON_AddStringToObject(jsonItem, OWNER, utdTypeCfg.ownerBundle.c_str());
86         std::vector<std::string> installerBundles(utdTypeCfg.installerBundles.begin(),
87                                                   utdTypeCfg.installerBundles.end());
88         AddJsonStringArray(installerBundles, INSTALLERS, *jsonItem);
89 
90         cJSON_AddItemToArray(CustomUTDs, jsonItem);
91     }
92     cJSON_AddItemToObject(root, UTD_CUSTOM, CustomUTDs);
93 
94     jsonData = cJSON_Print(root);
95     cJSON_Delete(root);
96     LOG_DEBUG(UDMF_CLIENT, "ConvertUtdCfgsToJson, jsonData size: %{public}zu.", jsonData.length());
97     return true;
98 }
99 
AddJsonStringArray(const std::vector<std::string> & datas,const std::string & nodeName,json & node)100 bool CustomUtdJsonParser::AddJsonStringArray(const std::vector<std::string> &datas, const std::string &nodeName,
101                                              json &node)
102 {
103     json *arrayNode = cJSON_AddArrayToObject(&node, nodeName.c_str());
104     for (const auto &data : datas) {
105         json* item = cJSON_CreateString(data.c_str());
106         cJSON_AddItemToArray(arrayNode, item);
107     }
108     return true;
109 }
110 
GetTypeDescriptors(const json & jsonRoot,const std::string & nodeName,std::vector<TypeDescriptorCfg> & typesCfg)111 bool CustomUtdJsonParser::GetTypeDescriptors(const json &jsonRoot, const std::string &nodeName,
112                                              std::vector<TypeDescriptorCfg> &typesCfg)
113 {
114     if (cJSON_HasObjectItem(&jsonRoot, nodeName.c_str())) {
115         cJSON *subNode = cJSON_GetObjectItem(&jsonRoot, nodeName.c_str());
116         int itemNum = cJSON_GetArraySize(subNode);
117         for (int i = 0; i < itemNum; i++) {
118             cJSON *node = cJSON_GetArrayItem(subNode, i);
119             TypeDescriptorCfg typeCfg;
120             typeCfg.typeId = GetStringValue(*node, TYPEID);
121             typeCfg.belongingToTypes = GetStringArrayValue(*node, BELONGINGTOTYPES);
122             typeCfg.filenameExtensions = GetStringArrayValue(*node, FILE_NAME_EXTENSTENSIONS);
123             typeCfg.mimeTypes = GetStringArrayValue(*node, MIME_TYPES);
124             typeCfg.description = GetStringValue(*node, DESCRIPTION);
125             typeCfg.referenceURL = GetStringValue(*node, REFERENCE_URL);
126             typeCfg.iconFile = GetStringValue(*node, ICON_FILE);
127             typeCfg.ownerBundle = GetStringValue(*node, OWNER);
128             std::vector<std::string> installerBundles = GetStringArrayValue(*node, INSTALLERS);
129             typeCfg.installerBundles.insert(installerBundles.begin(), installerBundles.end());
130             typesCfg.push_back(typeCfg);
131         }
132     }
133     return true;
134 }
135 
GetStringValue(const json & node,const std::string & nodeName)136 std::string CustomUtdJsonParser::GetStringValue(const json &node, const std::string &nodeName)
137 {
138     std::string value;
139     if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
140         cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
141         if (cJSON_IsString(subNode)) {
142             value = cJSON_GetStringValue(subNode);
143         }
144     }
145     return value;
146 }
147 
GetStringArrayValue(const json & node,const std::string & nodeName)148 std::vector<std::string> CustomUtdJsonParser::GetStringArrayValue(const json &node, const std::string &nodeName)
149 {
150     std::vector<std::string> values;
151     if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
152         cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
153         if (cJSON_IsNull(subNode) || !cJSON_IsArray(subNode)) {
154             return values;
155         }
156         for (int i = 0; i < cJSON_GetArraySize(subNode); i++) {
157             json *item = cJSON_GetArrayItem(subNode, i);
158             if (cJSON_IsString(item)) {
159                 values.emplace_back(cJSON_GetStringValue(item));
160             }
161         }
162     }
163     return values;
164 }
165 } // namespace UDMF
166 } // namespace OHOS