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