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 filePath_ = filePath;
34 if (!ResourceUtil::OpenJsonFile(filePath, &root_)) {
35 return RESTOOL_ERROR;
36 }
37 if (!root_ || !cJSON_IsObject(root_)) {
38 PrintError(GetError(ERR_CODE_JSON_FORMAT_ERROR).SetPosition(filePath));
39 return RESTOOL_ERROR;
40 }
41 if (!callback) {
42 return RESTOOL_SUCCESS;
43 }
44
45 InitFileListCommand(callback);
46
47 for (cJSON *item = root_->child; item; item = item->next) {
48 auto handler = fileListHandles_.find(item->string);
49 if (handler == fileListHandles_.end()) {
50 cout << "Warning: unsupport " << item->string << endl;
51 continue;
52 }
53 if (handler->second(item) != RESTOOL_SUCCESS) {
54 return RESTOOL_ERROR;
55 }
56 }
57
58 callback(Option::FORCEWRITE, "");
59 return RESTOOL_SUCCESS;
60 }
61
62 // below private
InitFileListCommand(HandleBack callback)63 void ResConfigParser::InitFileListCommand(HandleBack callback)
64 {
65 using namespace placeholders;
66 fileListHandles_.emplace("configPath", bind(&ResConfigParser::GetString, this, "configPath", _1,
67 Option::JSON, callback));
68 fileListHandles_.emplace("packageName", bind(&ResConfigParser::GetString, this, "packageName", _1,
69 Option::PACKAGENAME, callback));
70 fileListHandles_.emplace("output", bind(&ResConfigParser::GetString, this, "output", _1,
71 Option::OUTPUTPATH, callback));
72 fileListHandles_.emplace("startId", bind(&ResConfigParser::GetString, this, "startId", _1,
73 Option::STARTID, callback));
74 fileListHandles_.emplace("entryCompiledResource", bind(&ResConfigParser::GetString, this, "entryCompiledResource",
75 _1, Option::DEPENDENTRY, callback));
76 fileListHandles_.emplace("ids", bind(&ResConfigParser::GetString, this, "ids", _1, Option::IDS, callback));
77 fileListHandles_.emplace("definedIds", bind(&ResConfigParser::GetString, this, "definedIds", _1,
78 Option::DEFINED_IDS, callback));
79 fileListHandles_.emplace("applicationResource", bind(&ResConfigParser::GetString, this, "applicationResource", _1,
80 Option::INPUTPATH, callback));
81 fileListHandles_.emplace("ResourceTable", bind(&ResConfigParser::GetArray, this, "ResourceTable", _1,
82 Option::RESHEADER, callback));
83 fileListHandles_.emplace("moduleResources", bind(&ResConfigParser::GetArray, this, "moduleResources", _1,
84 Option::INPUTPATH, callback));
85 fileListHandles_.emplace("dependencies", bind(&ResConfigParser::GetArray, this, "dependencies", _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, "iconCheck", _1,
90 Option::ICON_CHECK, callback));
91 fileListHandles_.emplace("definedSysIds", bind(&ResConfigParser::GetString, this, "definedSysIds", _1,
92 Option::DEFINED_SYSIDS, callback));
93 fileListHandles_.emplace("compression", bind(&ResConfigParser::GetString, this, "compression", _1,
94 Option::COMPRESSED_CONFIG, callback));
95 fileListHandles_.emplace("thread", bind(&ResConfigParser::GetNumber, this, "thread", _1,
96 Option::THREAD, callback));
97 fileListHandles_.emplace("ignoreResourcePattern", bind(&ResConfigParser::GetIgnorePatterns, this,
98 "ignoreResourcePattern", _1, Option::IGNORED_FILE));
99 }
100
GetString(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)101 uint32_t ResConfigParser::GetString(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
102 {
103 if (!node) {
104 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
105 return RESTOOL_ERROR;
106 }
107 if (!cJSON_IsString(node)) {
108 PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "string").SetPosition(filePath_));
109 return RESTOOL_ERROR;
110 }
111
112 if (callback(c, node->valuestring) != RESTOOL_SUCCESS) {
113 return RESTOOL_ERROR;
114 }
115 return RESTOOL_SUCCESS;
116 }
117
GetArray(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)118 uint32_t ResConfigParser::GetArray(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
119 {
120 if (!node) {
121 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
122 return RESTOOL_ERROR;
123 }
124 if (!cJSON_IsArray(node)) {
125 PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "array").SetPosition(filePath_));
126 return RESTOOL_ERROR;
127 }
128
129 for (cJSON *item = node->child; item; item = item->next) {
130 if (!cJSON_IsString(item)) {
131 PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(item->string, "string")
132 .SetPosition(filePath_));
133 return RESTOOL_ERROR;
134 }
135 if (callback(c, item->valuestring) != RESTOOL_SUCCESS) {
136 return RESTOOL_ERROR;
137 }
138 }
139 return RESTOOL_SUCCESS;
140 }
141
GetModuleNames(const cJSON * node,int c,HandleBack callback)142 uint32_t ResConfigParser::GetModuleNames(const cJSON *node, int c, HandleBack callback)
143 {
144 if (!node) {
145 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause("moduleNames").SetPosition(filePath_));
146 return RESTOOL_ERROR;
147 }
148 if (cJSON_IsString(node)) {
149 return GetString("moduleNames", node, c, callback);
150 }
151 string moduleNames;
152 if (GetArray("moduleNames", node, c, [&moduleNames](int c, const string &argValue) {
153 if (!moduleNames.empty()) {
154 moduleNames.append(",");
155 }
156 moduleNames.append(argValue);
157 return RESTOOL_SUCCESS;
158 }) != RESTOOL_SUCCESS) {
159 return RESTOOL_ERROR;
160 }
161
162 if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) {
163 return RESTOOL_ERROR;
164 }
165 return RESTOOL_SUCCESS;
166 }
167
GetBool(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)168 uint32_t ResConfigParser::GetBool(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
169 {
170 if (!node) {
171 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
172 return RESTOOL_ERROR;
173 }
174 if (!cJSON_IsBool(node)) {
175 PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "bool").SetPosition(filePath_));
176 return RESTOOL_ERROR;
177 }
178
179 if (cJSON_IsTrue(node) == 1 && callback(c, "") != RESTOOL_SUCCESS) {
180 return RESTOOL_ERROR;
181 }
182 return RESTOOL_SUCCESS;
183 }
184
GetNumber(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)185 uint32_t ResConfigParser::GetNumber(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
186 {
187 if (!node) {
188 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
189 return RESTOOL_ERROR;
190 }
191 if (!cJSON_IsNumber(node)) {
192 PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "number").SetPosition(filePath_));
193 return RESTOOL_ERROR;
194 }
195
196 if (callback(c, std::to_string(node->valueint)) != RESTOOL_SUCCESS) {
197 return RESTOOL_ERROR;
198 }
199 return RESTOOL_SUCCESS;
200 }
201
GetIgnorePatterns(const std::string & nodeName,const cJSON * node,int c)202 uint32_t ResConfigParser::GetIgnorePatterns(const std::string &nodeName, const cJSON *node, int c)
203 {
204 if (!node) {
205 PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
206 return RESTOOL_ERROR;
207 }
208 ResourceUtil::SetUseCustomIgnoreRegex(true);
209 HandleBack callback = [](int c, const string &argValue) {
210 bool isSucceed = ResourceUtil::AddIgnoreFileRegex(argValue, IgnoreType::IGNORE_ALL);
211 if (!isSucceed) {
212 return RESTOOL_ERROR;
213 }
214 return RESTOOL_SUCCESS;
215 };
216 if (GetArray(nodeName, node, c, callback) != RESTOOL_SUCCESS) {
217 return RESTOOL_ERROR;
218 }
219 return RESTOOL_SUCCESS;
220 }
221 }
222 }
223 }
224