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 LOG_DEBUG(UDMF_CLIENT, "DeclarationsSize:%{public}zu, ReferenceSize:%{public}zu",
68 typesDeclarations.size(), typesReference.size());
69 return true;
70 }
71
ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> & typesCfg,std::string & jsonData)72 bool CustomUtdJsonParser::ConvertUtdCfgsToJson(const std::vector<TypeDescriptorCfg> &typesCfg, std::string &jsonData)
73 {
74 json* root = cJSON_CreateObject();
75 json* CustomUTDs = cJSON_CreateArray();
76 for (auto utdTypeCfg : typesCfg) {
77 json* jsonItem = cJSON_CreateObject();
78 if (jsonItem == nullptr) {
79 LOG_ERROR(UDMF_CLIENT, "Create jsonItem failed.");
80 cJSON_Delete(CustomUTDs);
81 cJSON_Delete(root);
82 return false;
83 }
84 cJSON_AddStringToObject(jsonItem, TYPEID, utdTypeCfg.typeId.c_str());
85 std::vector<std::string> belongingToTypes(utdTypeCfg.belongingToTypes.begin(),
86 utdTypeCfg.belongingToTypes.end());
87 AddJsonStringArray(belongingToTypes, BELONGINGTOTYPES, *jsonItem);
88 AddJsonStringArray(utdTypeCfg.filenameExtensions, FILE_NAME_EXTENSTENSIONS, *jsonItem);
89 AddJsonStringArray(utdTypeCfg.mimeTypes, MIME_TYPES, *jsonItem);
90 cJSON_AddStringToObject(jsonItem, DESCRIPTION, utdTypeCfg.description.c_str());
91 cJSON_AddStringToObject(jsonItem, REFERENCE_URL, utdTypeCfg.referenceURL.c_str());
92 cJSON_AddStringToObject(jsonItem, ICON_FILE, utdTypeCfg.iconFile.c_str());
93 cJSON_AddStringToObject(jsonItem, OWNER, utdTypeCfg.ownerBundle.c_str());
94 std::vector<std::string> installerBundles(utdTypeCfg.installerBundles.begin(),
95 utdTypeCfg.installerBundles.end());
96 AddJsonStringArray(installerBundles, INSTALLERS, *jsonItem);
97
98 cJSON_AddItemToArray(CustomUTDs, jsonItem);
99 }
100 cJSON_AddItemToObject(root, UTD_CUSTOM, CustomUTDs);
101
102 jsonData = cJSON_Print(root);
103 cJSON_Delete(root);
104 LOG_DEBUG(UDMF_CLIENT, "ConvertUtdCfgsToJson, jsonData size: %{public}zu.", jsonData.length());
105 return true;
106 }
107
AddJsonStringArray(const std::vector<std::string> & datas,const std::string & nodeName,json & node)108 bool CustomUtdJsonParser::AddJsonStringArray(const std::vector<std::string> &datas, const std::string &nodeName,
109 json &node)
110 {
111 json *arrayNode = cJSON_AddArrayToObject(&node, nodeName.c_str());
112 for (const auto &data : datas) {
113 json* item = cJSON_CreateString(data.c_str());
114 cJSON_AddItemToArray(arrayNode, item);
115 }
116 return true;
117 }
118
GetTypeDescriptors(const json & jsonRoot,const std::string & nodeName,std::vector<TypeDescriptorCfg> & typesCfg)119 bool CustomUtdJsonParser::GetTypeDescriptors(const json &jsonRoot, const std::string &nodeName,
120 std::vector<TypeDescriptorCfg> &typesCfg)
121 {
122 if (cJSON_HasObjectItem(&jsonRoot, nodeName.c_str())) {
123 cJSON *subNode = cJSON_GetObjectItem(&jsonRoot, nodeName.c_str());
124 int itemNum = cJSON_GetArraySize(subNode);
125 for (int i = 0; i < itemNum; i++) {
126 cJSON *node = cJSON_GetArrayItem(subNode, i);
127 TypeDescriptorCfg typeCfg;
128 typeCfg.typeId = GetStringValue(*node, TYPEID);
129 typeCfg.belongingToTypes = GetStringArrayValue(*node, BELONGINGTOTYPES);
130 typeCfg.filenameExtensions = GetStringArrayValue(*node, FILE_NAME_EXTENSTENSIONS);
131 typeCfg.mimeTypes = GetStringArrayValue(*node, MIME_TYPES);
132 typeCfg.description = GetStringValue(*node, DESCRIPTION);
133 typeCfg.referenceURL = GetStringValue(*node, REFERENCE_URL);
134 typeCfg.iconFile = GetStringValue(*node, ICON_FILE);
135 typeCfg.ownerBundle = GetStringValue(*node, OWNER);
136 std::vector<std::string> installerBundles = GetStringArrayValue(*node, INSTALLERS);
137 typeCfg.installerBundles.insert(installerBundles.begin(), installerBundles.end());
138 typesCfg.push_back(typeCfg);
139 }
140 }
141 return true;
142 }
143
GetStringValue(const json & node,const std::string & nodeName)144 std::string CustomUtdJsonParser::GetStringValue(const json &node, const std::string &nodeName)
145 {
146 std::string value;
147 if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
148 cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
149 if (cJSON_IsString(subNode)) {
150 value = cJSON_GetStringValue(subNode);
151 }
152 }
153 return value;
154 }
155
GetStringArrayValue(const json & node,const std::string & nodeName)156 std::vector<std::string> CustomUtdJsonParser::GetStringArrayValue(const json &node, const std::string &nodeName)
157 {
158 std::vector<std::string> values;
159 if (!cJSON_IsNull(&node) && cJSON_IsObject(&node) && cJSON_HasObjectItem(&node, nodeName.c_str())) {
160 cJSON *subNode = cJSON_GetObjectItem(&node, nodeName.c_str());
161 if (cJSON_IsNull(subNode) || !cJSON_IsArray(subNode)) {
162 return values;
163 }
164 for (int i = 0; i < cJSON_GetArraySize(subNode); i++) {
165 json *item = cJSON_GetArrayItem(subNode, i);
166 if (cJSON_IsString(item)) {
167 values.emplace_back(cJSON_GetStringValue(item));
168 }
169 }
170 }
171 return values;
172 }
173 } // namespace UDMF
174 } // namespace OHOS