1 /*
2 * Copyright (c) 2024 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
16 #include "resconfig_parser.h"
17 #include <iostream>
18 #include "restool_errors.h"
19
20 namespace OHOS {
21 namespace Global {
22 namespace Restool {
23 using namespace std;
ResConfigParser()24 ResConfigParser::ResConfigParser() : root_(nullptr) {}
~ResConfigParser()25 ResConfigParser::~ResConfigParser()
26 {
27 if (root_) {
28 cJSON_Delete(root_);
29 }
30 }
Init(const string & filePath,HandleBack callback)31 uint32_t ResConfigParser::Init(const string &filePath, HandleBack callback)
32 {
33 if (!ResourceUtil::OpenJsonFile(filePath, &root_)) {
34 return RESTOOL_ERROR;
35 }
36 if (!root_ || !cJSON_IsObject(root_)) {
37 cerr << "Error: JSON file parsing failed, please check the JSON file." << NEW_LINE_PATH << filePath << endl;
38 return RESTOOL_ERROR;
39 }
40 if (!callback) {
41 return RESTOOL_SUCCESS;
42 }
43
44 InitFileListCommand(callback);
45
46 for (cJSON *item = root_->child; item; item = item->next) {
47 auto handler = fileListHandles_.find(item->string);
48 if (handler == fileListHandles_.end()) {
49 cout << "Warning: unsupport " << item->string << endl;
50 continue;
51 }
52 if (handler->second(item) != RESTOOL_SUCCESS) {
53 return RESTOOL_ERROR;
54 }
55 }
56
57 callback(Option::FORCEWRITE, "");
58 return RESTOOL_SUCCESS;
59 }
60
61 // below private
InitFileListCommand(HandleBack callback)62 void ResConfigParser::InitFileListCommand(HandleBack callback)
63 {
64 using namespace placeholders;
65 fileListHandles_.emplace("configPath", bind(&ResConfigParser::GetString, this, _1,
66 Option::JSON, callback));
67 fileListHandles_.emplace("packageName", bind(&ResConfigParser::GetString, this, _1,
68 Option::PACKAGENAME, callback));
69 fileListHandles_.emplace("output", bind(&ResConfigParser::GetString, this, _1,
70 Option::OUTPUTPATH, callback));
71 fileListHandles_.emplace("startId", bind(&ResConfigParser::GetString, this, _1,
72 Option::STARTID, callback));
73 fileListHandles_.emplace("entryCompiledResource", bind(&ResConfigParser::GetString, this, _1,
74 Option::DEPENDENTRY, callback));
75 fileListHandles_.emplace("ids", bind(&ResConfigParser::GetString, this, _1,
76 Option::IDS, callback));
77 fileListHandles_.emplace("definedIds", bind(&ResConfigParser::GetString, this, _1,
78 Option::DEFINED_IDS, callback));
79 fileListHandles_.emplace("applicationResource", bind(&ResConfigParser::GetString, this, _1,
80 Option::INPUTPATH, callback));
81 fileListHandles_.emplace("ResourceTable", bind(&ResConfigParser::GetArray, this, _1,
82 Option::RESHEADER, callback));
83 fileListHandles_.emplace("moduleResources", bind(&ResConfigParser::GetArray, this, _1,
84 Option::INPUTPATH, callback));
85 fileListHandles_.emplace("dependencies", bind(&ResConfigParser::GetArray, this, _1,
86 Option::INPUTPATH, callback));
87 fileListHandles_.emplace("moduleNames", bind(&ResConfigParser::GetModuleNames, this, _1,
88 Option::MODULES, callback));
89 fileListHandles_.emplace("iconCheck", bind(&ResConfigParser::GetBool, this, _1,
90 Option::ICON_CHECK, callback));
91 fileListHandles_.emplace("definedSysIds", bind(&ResConfigParser::GetString, this, _1,
92 Option::DEFINED_SYSIDS, callback));
93 }
94
GetString(const cJSON * node,int c,HandleBack callback)95 uint32_t ResConfigParser::GetString(const cJSON *node, int c, HandleBack callback)
96 {
97 if (!node || !cJSON_IsString(node)) {
98 cerr << "Error: GetString node not string. Option = " << c << endl;
99 return RESTOOL_ERROR;
100 }
101
102 if (callback(c, node->valuestring) != RESTOOL_SUCCESS) {
103 return RESTOOL_ERROR;
104 }
105 return RESTOOL_SUCCESS;
106 }
107
GetArray(const cJSON * node,int c,HandleBack callback)108 uint32_t ResConfigParser::GetArray(const cJSON *node, int c, HandleBack callback)
109 {
110 if (!node || !cJSON_IsArray(node)) {
111 cerr << "Error: GetArray node not array. Option = " << c << endl;
112 return RESTOOL_ERROR;
113 }
114
115 for (cJSON *item = node->child; item; item = item->next) {
116 if (!cJSON_IsString(item)) {
117 return RESTOOL_ERROR;
118 }
119 if (callback(c, item->valuestring) != RESTOOL_SUCCESS) {
120 return RESTOOL_ERROR;
121 }
122 }
123 return RESTOOL_SUCCESS;
124 }
125
GetModuleNames(const cJSON * node,int c,HandleBack callback)126 uint32_t ResConfigParser::GetModuleNames(const cJSON *node, int c, HandleBack callback)
127 {
128 if (!node) {
129 cerr << "Error: GetModuleNames node is null. Option = " << c << endl;
130 return RESTOOL_ERROR;
131 }
132 if (cJSON_IsString(node)) {
133 return GetString(node, c, callback);
134 }
135 string moduleNames;
136 if (GetArray(node, c, [&moduleNames](int c, const string &argValue) {
137 if (!moduleNames.empty()) {
138 moduleNames.append(",");
139 }
140 moduleNames.append(argValue);
141 return RESTOOL_SUCCESS;
142 }) != RESTOOL_SUCCESS) {
143 return RESTOOL_ERROR;
144 }
145
146 if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) {
147 return RESTOOL_ERROR;
148 }
149 return RESTOOL_SUCCESS;
150 }
151
GetBool(const cJSON * node,int c,HandleBack callback)152 uint32_t ResConfigParser::GetBool(const cJSON *node, int c, HandleBack callback)
153 {
154 if (!node || !cJSON_IsBool(node)) {
155 cerr << "Error: GetBool node not bool. Option = " << c << endl;
156 return RESTOOL_ERROR;
157 }
158
159 if (cJSON_IsTrue(node) == 1 && callback(c, "") != RESTOOL_SUCCESS) {
160 return RESTOOL_ERROR;
161 }
162 return RESTOOL_SUCCESS;
163 }
164 }
165 }
166 }
167