13920e296Sopenharmony_ci/* 23920e296Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33920e296Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43920e296Sopenharmony_ci * you may not use this file except in compliance with the License. 53920e296Sopenharmony_ci * You may obtain a copy of the License at 63920e296Sopenharmony_ci * 73920e296Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83920e296Sopenharmony_ci * 93920e296Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103920e296Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113920e296Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123920e296Sopenharmony_ci * See the License for the specific language governing permissions and 133920e296Sopenharmony_ci * limitations under the License. 143920e296Sopenharmony_ci */ 153920e296Sopenharmony_ci 163920e296Sopenharmony_ci#include "resconfig_parser.h" 173920e296Sopenharmony_ci#include <iostream> 183920e296Sopenharmony_ci#include "restool_errors.h" 193920e296Sopenharmony_ci 203920e296Sopenharmony_cinamespace OHOS { 213920e296Sopenharmony_cinamespace Global { 223920e296Sopenharmony_cinamespace Restool { 233920e296Sopenharmony_ciusing namespace std; 243920e296Sopenharmony_ciResConfigParser::ResConfigParser() : root_(nullptr) {} 253920e296Sopenharmony_ciResConfigParser::~ResConfigParser() 263920e296Sopenharmony_ci{ 273920e296Sopenharmony_ci if (root_) { 283920e296Sopenharmony_ci cJSON_Delete(root_); 293920e296Sopenharmony_ci } 303920e296Sopenharmony_ci} 313920e296Sopenharmony_ciuint32_t ResConfigParser::Init(const string &filePath, HandleBack callback) 323920e296Sopenharmony_ci{ 333920e296Sopenharmony_ci if (!ResourceUtil::OpenJsonFile(filePath, &root_)) { 343920e296Sopenharmony_ci return RESTOOL_ERROR; 353920e296Sopenharmony_ci } 363920e296Sopenharmony_ci if (!root_ || !cJSON_IsObject(root_)) { 373920e296Sopenharmony_ci cerr << "Error: JSON file parsing failed, please check the JSON file." << NEW_LINE_PATH << filePath << endl; 383920e296Sopenharmony_ci return RESTOOL_ERROR; 393920e296Sopenharmony_ci } 403920e296Sopenharmony_ci if (!callback) { 413920e296Sopenharmony_ci return RESTOOL_SUCCESS; 423920e296Sopenharmony_ci } 433920e296Sopenharmony_ci 443920e296Sopenharmony_ci InitFileListCommand(callback); 453920e296Sopenharmony_ci 463920e296Sopenharmony_ci for (cJSON *item = root_->child; item; item = item->next) { 473920e296Sopenharmony_ci auto handler = fileListHandles_.find(item->string); 483920e296Sopenharmony_ci if (handler == fileListHandles_.end()) { 493920e296Sopenharmony_ci cout << "Warning: unsupport " << item->string << endl; 503920e296Sopenharmony_ci continue; 513920e296Sopenharmony_ci } 523920e296Sopenharmony_ci if (handler->second(item) != RESTOOL_SUCCESS) { 533920e296Sopenharmony_ci return RESTOOL_ERROR; 543920e296Sopenharmony_ci } 553920e296Sopenharmony_ci } 563920e296Sopenharmony_ci 573920e296Sopenharmony_ci callback(Option::FORCEWRITE, ""); 583920e296Sopenharmony_ci return RESTOOL_SUCCESS; 593920e296Sopenharmony_ci} 603920e296Sopenharmony_ci 613920e296Sopenharmony_ci// below private 623920e296Sopenharmony_civoid ResConfigParser::InitFileListCommand(HandleBack callback) 633920e296Sopenharmony_ci{ 643920e296Sopenharmony_ci using namespace placeholders; 653920e296Sopenharmony_ci fileListHandles_.emplace("configPath", bind(&ResConfigParser::GetString, this, _1, 663920e296Sopenharmony_ci Option::JSON, callback)); 673920e296Sopenharmony_ci fileListHandles_.emplace("packageName", bind(&ResConfigParser::GetString, this, _1, 683920e296Sopenharmony_ci Option::PACKAGENAME, callback)); 693920e296Sopenharmony_ci fileListHandles_.emplace("output", bind(&ResConfigParser::GetString, this, _1, 703920e296Sopenharmony_ci Option::OUTPUTPATH, callback)); 713920e296Sopenharmony_ci fileListHandles_.emplace("startId", bind(&ResConfigParser::GetString, this, _1, 723920e296Sopenharmony_ci Option::STARTID, callback)); 733920e296Sopenharmony_ci fileListHandles_.emplace("entryCompiledResource", bind(&ResConfigParser::GetString, this, _1, 743920e296Sopenharmony_ci Option::DEPENDENTRY, callback)); 753920e296Sopenharmony_ci fileListHandles_.emplace("ids", bind(&ResConfigParser::GetString, this, _1, 763920e296Sopenharmony_ci Option::IDS, callback)); 773920e296Sopenharmony_ci fileListHandles_.emplace("definedIds", bind(&ResConfigParser::GetString, this, _1, 783920e296Sopenharmony_ci Option::DEFINED_IDS, callback)); 793920e296Sopenharmony_ci fileListHandles_.emplace("applicationResource", bind(&ResConfigParser::GetString, this, _1, 803920e296Sopenharmony_ci Option::INPUTPATH, callback)); 813920e296Sopenharmony_ci fileListHandles_.emplace("ResourceTable", bind(&ResConfigParser::GetArray, this, _1, 823920e296Sopenharmony_ci Option::RESHEADER, callback)); 833920e296Sopenharmony_ci fileListHandles_.emplace("moduleResources", bind(&ResConfigParser::GetArray, this, _1, 843920e296Sopenharmony_ci Option::INPUTPATH, callback)); 853920e296Sopenharmony_ci fileListHandles_.emplace("dependencies", bind(&ResConfigParser::GetArray, this, _1, 863920e296Sopenharmony_ci Option::INPUTPATH, callback)); 873920e296Sopenharmony_ci fileListHandles_.emplace("moduleNames", bind(&ResConfigParser::GetModuleNames, this, _1, 883920e296Sopenharmony_ci Option::MODULES, callback)); 893920e296Sopenharmony_ci fileListHandles_.emplace("iconCheck", bind(&ResConfigParser::GetBool, this, _1, 903920e296Sopenharmony_ci Option::ICON_CHECK, callback)); 913920e296Sopenharmony_ci fileListHandles_.emplace("definedSysIds", bind(&ResConfigParser::GetString, this, _1, 923920e296Sopenharmony_ci Option::DEFINED_SYSIDS, callback)); 933920e296Sopenharmony_ci fileListHandles_.emplace("compression", bind(&ResConfigParser::GetString, this, _1, 943920e296Sopenharmony_ci Option::COMPRESSED_CONFIG, callback)); 953920e296Sopenharmony_ci} 963920e296Sopenharmony_ci 973920e296Sopenharmony_ciuint32_t ResConfigParser::GetString(const cJSON *node, int c, HandleBack callback) 983920e296Sopenharmony_ci{ 993920e296Sopenharmony_ci if (!node || !cJSON_IsString(node)) { 1003920e296Sopenharmony_ci cerr << "Error: GetString node not string. Option = " << c << endl; 1013920e296Sopenharmony_ci return RESTOOL_ERROR; 1023920e296Sopenharmony_ci } 1033920e296Sopenharmony_ci 1043920e296Sopenharmony_ci if (callback(c, node->valuestring) != RESTOOL_SUCCESS) { 1053920e296Sopenharmony_ci return RESTOOL_ERROR; 1063920e296Sopenharmony_ci } 1073920e296Sopenharmony_ci return RESTOOL_SUCCESS; 1083920e296Sopenharmony_ci} 1093920e296Sopenharmony_ci 1103920e296Sopenharmony_ciuint32_t ResConfigParser::GetArray(const cJSON *node, int c, HandleBack callback) 1113920e296Sopenharmony_ci{ 1123920e296Sopenharmony_ci if (!node || !cJSON_IsArray(node)) { 1133920e296Sopenharmony_ci cerr << "Error: GetArray node not array. Option = " << c << endl; 1143920e296Sopenharmony_ci return RESTOOL_ERROR; 1153920e296Sopenharmony_ci } 1163920e296Sopenharmony_ci 1173920e296Sopenharmony_ci for (cJSON *item = node->child; item; item = item->next) { 1183920e296Sopenharmony_ci if (!cJSON_IsString(item)) { 1193920e296Sopenharmony_ci return RESTOOL_ERROR; 1203920e296Sopenharmony_ci } 1213920e296Sopenharmony_ci if (callback(c, item->valuestring) != RESTOOL_SUCCESS) { 1223920e296Sopenharmony_ci return RESTOOL_ERROR; 1233920e296Sopenharmony_ci } 1243920e296Sopenharmony_ci } 1253920e296Sopenharmony_ci return RESTOOL_SUCCESS; 1263920e296Sopenharmony_ci} 1273920e296Sopenharmony_ci 1283920e296Sopenharmony_ciuint32_t ResConfigParser::GetModuleNames(const cJSON *node, int c, HandleBack callback) 1293920e296Sopenharmony_ci{ 1303920e296Sopenharmony_ci if (!node) { 1313920e296Sopenharmony_ci cerr << "Error: GetModuleNames node is null. Option = " << c << endl; 1323920e296Sopenharmony_ci return RESTOOL_ERROR; 1333920e296Sopenharmony_ci } 1343920e296Sopenharmony_ci if (cJSON_IsString(node)) { 1353920e296Sopenharmony_ci return GetString(node, c, callback); 1363920e296Sopenharmony_ci } 1373920e296Sopenharmony_ci string moduleNames; 1383920e296Sopenharmony_ci if (GetArray(node, c, [&moduleNames](int c, const string &argValue) { 1393920e296Sopenharmony_ci if (!moduleNames.empty()) { 1403920e296Sopenharmony_ci moduleNames.append(","); 1413920e296Sopenharmony_ci } 1423920e296Sopenharmony_ci moduleNames.append(argValue); 1433920e296Sopenharmony_ci return RESTOOL_SUCCESS; 1443920e296Sopenharmony_ci }) != RESTOOL_SUCCESS) { 1453920e296Sopenharmony_ci return RESTOOL_ERROR; 1463920e296Sopenharmony_ci } 1473920e296Sopenharmony_ci 1483920e296Sopenharmony_ci if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) { 1493920e296Sopenharmony_ci return RESTOOL_ERROR; 1503920e296Sopenharmony_ci } 1513920e296Sopenharmony_ci return RESTOOL_SUCCESS; 1523920e296Sopenharmony_ci} 1533920e296Sopenharmony_ci 1543920e296Sopenharmony_ciuint32_t ResConfigParser::GetBool(const cJSON *node, int c, HandleBack callback) 1553920e296Sopenharmony_ci{ 1563920e296Sopenharmony_ci if (!node || !cJSON_IsBool(node)) { 1573920e296Sopenharmony_ci cerr << "Error: GetBool node not bool. Option = " << c << endl; 1583920e296Sopenharmony_ci return RESTOOL_ERROR; 1593920e296Sopenharmony_ci } 1603920e296Sopenharmony_ci 1613920e296Sopenharmony_ci if (cJSON_IsTrue(node) == 1 && callback(c, "") != RESTOOL_SUCCESS) { 1623920e296Sopenharmony_ci return RESTOOL_ERROR; 1633920e296Sopenharmony_ci } 1643920e296Sopenharmony_ci return RESTOOL_SUCCESS; 1653920e296Sopenharmony_ci} 1663920e296Sopenharmony_ci} 1673920e296Sopenharmony_ci} 1683920e296Sopenharmony_ci} 169