• 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 constexpr const int32_t MAX_UTD_CUSTOM_SIZE = 1024 * 1024;
30 
CustomUtdJsonParser()31 CustomUtdJsonParser::CustomUtdJsonParser()
32 {
33 }
34 
~CustomUtdJsonParser()35 CustomUtdJsonParser::~CustomUtdJsonParser()
36 {
37 }
38 
ParseStoredCustomUtdJson(const std::string & jsonData,std::vector<TypeDescriptorCfg> & typesCfg)39 bool CustomUtdJsonParser::ParseStoredCustomUtdJson(const std::string &jsonData,
40                                                    std::vector<TypeDescriptorCfg> &typesCfg)
41 {
42     if (jsonData.empty()) {
43         return false;
44     }
45 
46     cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
47     if (jsonRoot == nullptr) {
48         LOG_ERROR(UDMF_CLIENT, "Failed to parse JSON: invalid format");
49         return false;
50     }
51     if (!cJSON_IsObject(jsonRoot)) {
52         LOG_ERROR(UDMF_CLIENT, "Parsed JSON root is not an object");
53         cJSON_Delete(jsonRoot);
54         return false;
55     }
56     if (!GetTypeDescriptors(*jsonRoot, UTD_CUSTOM, typesCfg)) {
57         LOG_ERROR(UDMF_CLIENT, "Failed to get type descriptors");
58         cJSON_Delete(jsonRoot);
59         return false;
60     }
61     cJSON_Delete(jsonRoot);
62     return true;
63 }
64 
ParseUserCustomUtdJson(const std::string & jsonData,std::vector<TypeDescriptorCfg> & typesDeclarations,std::vector<TypeDescriptorCfg> & typesReference)65 bool CustomUtdJsonParser::ParseUserCustomUtdJson(const std::string &jsonData,
66                                                  std::vector<TypeDescriptorCfg> &typesDeclarations,
67                                                  std::vector<TypeDescriptorCfg> &typesReference)
68 {
69     if (jsonData.empty()) {
70         return false;
71     }
72     // parse utd-adt.json to TypeDescriptorCfg obj
73     cJSON* jsonRoot = cJSON_Parse(jsonData.c_str());
74     if (jsonRoot == nullptr) {
75         LOG_ERROR(UDMF_CLIENT, "Parse failed: invalid JSON format.");
76         return false;
77     }
78     if (!cJSON_IsObject(jsonRoot)) {
79         LOG_ERROR(UDMF_CLIENT, "Parsed JSON root is not an object");
80         cJSON_Delete(jsonRoot);
81         return false;
82     }
83     if (!GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_DECLARATION, typesDeclarations) ||
84         !GetTypeDescriptors(*jsonRoot, UTD_CUSTOM_REFERENCE, typesReference)) {
85         LOG_ERROR(UDMF_CLIENT, "Failed to get type descriptors");
86         cJSON_Delete(jsonRoot);
87         return false;
88     }
89     cJSON_Delete(jsonRoot);
90     LOG_INFO(UDMF_CLIENT, "DeclarationsSize:%{public}zu, ReferenceSize:%{public}zu",
91         typesDeclarations.size(), typesReference.size());
92     return true;
93 }
94 
ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> & typesCfg,std::string & jsonData)95 bool CustomUtdJsonParser::ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> &typesCfg, std::string &jsonData)
96 {
97     json* root = cJSON_CreateObject();
98     json* CustomUTDs = cJSON_CreateArray();
99     for (auto utdTypeCfg : typesCfg) {
100         json* jsonItem = cJSON_CreateObject();
101         if (jsonItem == nullptr) {
102             LOG_ERROR(UDMF_CLIENT, "Create jsonItem failed.");
103             cJSON_Delete(CustomUTDs);
104             cJSON_Delete(root);
105             return false;
106         }
107         cJSON_AddStringToObject(jsonItem, TYPEID, utdTypeCfg.typeId.c_str());
108         std::vector<std::string> belongingToTypes(utdTypeCfg.belongingToTypes.begin(),
109                                                   utdTypeCfg.belongingToTypes.end());
110         AddJsonStringArray(belongingToTypes, BELONGINGTOTYPES, *jsonItem);
111         AddJsonStringArray(utdTypeCfg.filenameExtensions, FILE_NAME_EXTENSTENSIONS, *jsonItem);
112         AddJsonStringArray(utdTypeCfg.mimeTypes, MIME_TYPES, *jsonItem);
113         cJSON_AddStringToObject(jsonItem, DESCRIPTION, utdTypeCfg.description.c_str());
114         cJSON_AddStringToObject(jsonItem, REFERENCE_URL, utdTypeCfg.referenceURL.c_str());
115         cJSON_AddStringToObject(jsonItem, ICON_FILE, utdTypeCfg.iconFile.c_str());
116         cJSON_AddStringToObject(jsonItem, OWNER, utdTypeCfg.ownerBundle.c_str());
117         std::vector<std::string> installerBundles(utdTypeCfg.installerBundles.begin(),
118                                                   utdTypeCfg.installerBundles.end());
119         AddJsonStringArray(installerBundles, INSTALLERS, *jsonItem);
120 
121         cJSON_AddItemToArray(CustomUTDs, jsonItem);
122     }
123     cJSON_AddItemToObject(root, UTD_CUSTOM, CustomUTDs);
124 
125     jsonData = cJSON_Print(root);
126     cJSON_Delete(root);
127     LOG_DEBUG(UDMF_CLIENT, "ConvertUtdCfgsToJson, jsonData size: %{public}zu.", jsonData.length());
128     return true;
129 }
130 
AddJsonStringArray(const std::vector<std::string> & datas,const std::string & nodeName,json & node)131 bool CustomUtdJsonParser::AddJsonStringArray(const std::vector<std::string> &datas, const std::string &nodeName,
132                                              json &node)
133 {
134     json *arrayNode = cJSON_AddArrayToObject(&node, nodeName.c_str());
135     for (const auto &data : datas) {
136         json* item = cJSON_CreateString(data.c_str());
137         cJSON_AddItemToArray(arrayNode, item);
138     }
139     return true;
140 }
141 
GetTypeDescriptors(const json & jsonRoot,const std::string & nodeName,std::vector<TypeDescriptorCfg> & typesCfg)142 bool CustomUtdJsonParser::GetTypeDescriptors(const json &jsonRoot, const std::string &nodeName,
143                                              std::vector<TypeDescriptorCfg> &typesCfg)
144 {
145     if (cJSON_HasObjectItem(&jsonRoot, nodeName.c_str())) {
146         cJSON *subNode = cJSON_GetObjectItem(&jsonRoot, nodeName.c_str());
147         int itemNum = cJSON_GetArraySize(subNode);
148         if (itemNum > MAX_UTD_CUSTOM_SIZE) {
149             LOG_ERROR(UDMF_CLIENT, "itemNum is too large, itemNum:%{public}d", itemNum);
150             return false;
151         }
152         for (int i = 0; i < itemNum; i++) {
153             const cJSON *node = cJSON_GetArrayItem(subNode, i);
154             TypeDescriptorCfg typeCfg;
155             typeCfg.typeId = GetStringValue(node, TYPEID);
156             typeCfg.belongingToTypes = GetStringArrayValue(node, BELONGINGTOTYPES);
157             typeCfg.filenameExtensions = GetStringArrayValue(node, FILE_NAME_EXTENSTENSIONS);
158             typeCfg.mimeTypes = GetStringArrayValue(node, MIME_TYPES);
159             typeCfg.description = GetStringValue(node, DESCRIPTION);
160             typeCfg.referenceURL = GetStringValue(node, REFERENCE_URL);
161             typeCfg.iconFile = GetStringValue(node, ICON_FILE);
162             typeCfg.ownerBundle = GetStringValue(node, OWNER);
163             std::vector<std::string> installerBundles = GetStringArrayValue(node, INSTALLERS);
164             typeCfg.installerBundles.insert(installerBundles.begin(), installerBundles.end());
165             typesCfg.push_back(typeCfg);
166         }
167     }
168     return true;
169 }
170 
GetStringValue(const json * node,const std::string & nodeName)171 std::string CustomUtdJsonParser::GetStringValue(const json *node, const std::string &nodeName)
172 {
173     std::string value;
174     if (!cJSON_IsNull(node) && cJSON_IsObject(node) && cJSON_HasObjectItem(node, nodeName.c_str())) {
175         cJSON *subNode = cJSON_GetObjectItem(node, nodeName.c_str());
176         if (cJSON_IsString(subNode)) {
177             value = cJSON_GetStringValue(subNode);
178         }
179     }
180     return value;
181 }
182 
GetStringArrayValue(const json * node,const std::string & nodeName)183 std::vector<std::string> CustomUtdJsonParser::GetStringArrayValue(const json *node, const std::string &nodeName)
184 {
185     std::vector<std::string> values;
186     if (!cJSON_IsNull(node) && cJSON_IsObject(node) && cJSON_HasObjectItem(node, nodeName.c_str())) {
187         cJSON *subNode = cJSON_GetObjectItem(node, nodeName.c_str());
188         if (cJSON_IsNull(subNode) || !cJSON_IsArray(subNode)) {
189             return values;
190         }
191         for (int i = 0; i < cJSON_GetArraySize(subNode); i++) {
192             json *item = cJSON_GetArrayItem(subNode, i);
193             if (cJSON_IsString(item)) {
194                 values.emplace_back(cJSON_GetStringValue(item));
195             }
196         }
197     }
198     return values;
199 }
200 } // namespace UDMF
201 } // namespace OHOS