• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
98 
GetString(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)99 uint32_t ResConfigParser::GetString(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
100 {
101     if (!node) {
102         PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
103         return RESTOOL_ERROR;
104     }
105     if (!cJSON_IsString(node)) {
106         PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "string").SetPosition(filePath_));
107         return RESTOOL_ERROR;
108     }
109 
110     if (callback(c, node->valuestring) != RESTOOL_SUCCESS) {
111         return RESTOOL_ERROR;
112     }
113     return RESTOOL_SUCCESS;
114 }
115 
GetArray(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)116 uint32_t ResConfigParser::GetArray(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
117 {
118     if (!node) {
119         PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
120         return RESTOOL_ERROR;
121     }
122     if (!cJSON_IsArray(node)) {
123         PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "array").SetPosition(filePath_));
124         return RESTOOL_ERROR;
125     }
126 
127     for (cJSON *item = node->child; item; item = item->next) {
128         if (!cJSON_IsString(item)) {
129             PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(item->string, "string")
130                 .SetPosition(filePath_));
131             return RESTOOL_ERROR;
132         }
133         if (callback(c, item->valuestring) != RESTOOL_SUCCESS) {
134             return RESTOOL_ERROR;
135         }
136     }
137     return RESTOOL_SUCCESS;
138 }
139 
GetModuleNames(const cJSON * node,int c,HandleBack callback)140 uint32_t ResConfigParser::GetModuleNames(const cJSON *node, int c, HandleBack callback)
141 {
142     if (!node) {
143         PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause("moduleNames").SetPosition(filePath_));
144         return RESTOOL_ERROR;
145     }
146     if (cJSON_IsString(node)) {
147         return GetString("moduleNames", node, c, callback);
148     }
149     string moduleNames;
150     if (GetArray("moduleNames", node, c, [&moduleNames](int c, const string &argValue) {
151         if (!moduleNames.empty()) {
152             moduleNames.append(",");
153         }
154         moduleNames.append(argValue);
155         return RESTOOL_SUCCESS;
156     }) != RESTOOL_SUCCESS) {
157         return RESTOOL_ERROR;
158     }
159 
160     if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) {
161         return RESTOOL_ERROR;
162     }
163     return RESTOOL_SUCCESS;
164 }
165 
GetBool(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)166 uint32_t ResConfigParser::GetBool(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
167 {
168     if (!node) {
169         PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
170         return RESTOOL_ERROR;
171     }
172     if (!cJSON_IsBool(node)) {
173         PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "bool").SetPosition(filePath_));
174         return RESTOOL_ERROR;
175     }
176 
177     if (cJSON_IsTrue(node) == 1 && callback(c, "") != RESTOOL_SUCCESS) {
178         return RESTOOL_ERROR;
179     }
180     return RESTOOL_SUCCESS;
181 }
182 
GetNumber(const std::string & nodeName,const cJSON * node,int c,HandleBack callback)183 uint32_t ResConfigParser::GetNumber(const std::string &nodeName, const cJSON *node, int c, HandleBack callback)
184 {
185     if (!node) {
186         PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_));
187         return RESTOOL_ERROR;
188     }
189     if (!cJSON_IsNumber(node)) {
190         PrintError(GetError(ERR_CODE_JSON_NODE_MISMATCH).FormatCause(node->string, "number").SetPosition(filePath_));
191         return RESTOOL_ERROR;
192     }
193 
194     if (callback(c, std::to_string(node->valueint)) != RESTOOL_SUCCESS) {
195         return RESTOOL_ERROR;
196     }
197     return RESTOOL_SUCCESS;
198 }
199 }
200 }
201 }
202