• 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     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     fileListHandles_.emplace("compression", bind(&ResConfigParser::GetString, this, _1,
94         Option::COMPRESSED_CONFIG, callback));
95 }
96 
GetString(const cJSON * node,int c,HandleBack callback)97 uint32_t ResConfigParser::GetString(const cJSON *node, int c, HandleBack callback)
98 {
99     if (!node || !cJSON_IsString(node)) {
100         cerr << "Error: GetString node not string. Option = " << c << endl;
101         return RESTOOL_ERROR;
102     }
103 
104     if (callback(c, node->valuestring) != RESTOOL_SUCCESS) {
105         return RESTOOL_ERROR;
106     }
107     return RESTOOL_SUCCESS;
108 }
109 
GetArray(const cJSON * node,int c,HandleBack callback)110 uint32_t ResConfigParser::GetArray(const cJSON *node, int c, HandleBack callback)
111 {
112     if (!node || !cJSON_IsArray(node)) {
113         cerr << "Error: GetArray node not array. Option = " << c << endl;
114         return RESTOOL_ERROR;
115     }
116 
117     for (cJSON *item = node->child; item; item = item->next) {
118         if (!cJSON_IsString(item)) {
119             return RESTOOL_ERROR;
120         }
121         if (callback(c, item->valuestring) != RESTOOL_SUCCESS) {
122             return RESTOOL_ERROR;
123         }
124     }
125     return RESTOOL_SUCCESS;
126 }
127 
GetModuleNames(const cJSON * node,int c,HandleBack callback)128 uint32_t ResConfigParser::GetModuleNames(const cJSON *node, int c, HandleBack callback)
129 {
130     if (!node) {
131         cerr << "Error: GetModuleNames node is null. Option = " << c << endl;
132         return RESTOOL_ERROR;
133     }
134     if (cJSON_IsString(node)) {
135         return GetString(node, c, callback);
136     }
137     string moduleNames;
138     if (GetArray(node, c, [&moduleNames](int c, const string &argValue) {
139         if (!moduleNames.empty()) {
140             moduleNames.append(",");
141         }
142         moduleNames.append(argValue);
143         return RESTOOL_SUCCESS;
144     }) != RESTOOL_SUCCESS) {
145         return RESTOOL_ERROR;
146     }
147 
148     if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) {
149         return RESTOOL_ERROR;
150     }
151     return RESTOOL_SUCCESS;
152 }
153 
GetBool(const cJSON * node,int c,HandleBack callback)154 uint32_t ResConfigParser::GetBool(const cJSON *node, int c, HandleBack callback)
155 {
156     if (!node || !cJSON_IsBool(node)) {
157         cerr << "Error: GetBool node not bool. Option = " << c << endl;
158         return RESTOOL_ERROR;
159     }
160 
161     if (cJSON_IsTrue(node) == 1 && callback(c, "") != RESTOOL_SUCCESS) {
162         return RESTOOL_ERROR;
163     }
164     return RESTOOL_SUCCESS;
165 }
166 }
167 }
168 }
169