1 /* 2 * Copyright (c) 2021 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 #ifndef OHOS_RESTOOL_JSON_COMPILER_H 17 #define OHOS_RESTOOL_JSON_COMPILER_H 18 19 #include <functional> 20 #include "i_resource_compiler.h" 21 #include "resource_util.h" 22 23 namespace OHOS { 24 namespace Global { 25 namespace Restool { 26 class JsonCompiler : public IResourceCompiler { 27 public: 28 JsonCompiler(ResType type, const std::string &output); 29 virtual ~JsonCompiler(); 30 protected: 31 uint32_t CompileSingleFile(const FileInfo &fileInfo) override; 32 private: 33 void InitParser(); 34 bool ParseJsonArrayLevel(const Json::Value &arrayNode, const FileInfo &fileInfo); 35 bool ParseJsonObjectLevel(const Json::Value &objectNode, const FileInfo &fileInfo); 36 37 using HandleResource = std::function<bool(const Json::Value&, ResourceItem&)>; 38 bool HandleString(const Json::Value &objectNode, ResourceItem &resourceItem) const; 39 bool HandleInteger(const Json::Value &objectNode, ResourceItem &resourceItem) const; 40 bool HandleBoolean(const Json::Value &objectNode, ResourceItem &resourceItem) const; 41 bool HandleColor(const Json::Value &objectNode, ResourceItem &resourceItem) const; 42 bool HandleFloat(const Json::Value &objectNode, ResourceItem &resourceItem) const; 43 bool HandleStringArray(const Json::Value &objectNode, ResourceItem &resourceItem) const; 44 bool HandleIntegerArray(const Json::Value &objectNode, ResourceItem &resourceItem) const; 45 bool HandleTheme(const Json::Value &objectNode, ResourceItem &resourceItem) const; 46 bool HandlePattern(const Json::Value &objectNode, ResourceItem &resourceItem) const; 47 bool HandlePlural(const Json::Value &objectNode, ResourceItem &resourceItem) const; 48 49 bool PushString(const std::string &value, ResourceItem &resourceItem) const; 50 bool CheckJsonStringValue(const Json::Value &valueNode, const ResourceItem &resourceItem) const; 51 bool CheckJsonIntegerValue(const Json::Value &valueNode, const ResourceItem &resourceItem) const; 52 using HandleValue = std::function<bool(const Json::Value&, const ResourceItem&, std::vector<std::string>&)>; 53 bool ParseValueArray(const Json::Value &objectNode, ResourceItem &resourceItem, 54 const std::vector<std::string> &extra, HandleValue callback) const; 55 bool ParseParent(const Json::Value &objectNode, const ResourceItem &resourceItem, 56 std::vector<std::string> &extra) const; 57 bool ParseAttribute(const Json::Value &arrayItem, const ResourceItem &resourceItem, 58 std::vector<std::string> &values) const; 59 bool CheckPluralValue(const Json::Value &arrayItem, const ResourceItem &resourceItem) const; 60 bool CheckColorValue(const char *s) const; 61 std::map<ResType, HandleResource> handles_; 62 static const std::string TAG_NAME; 63 static const std::string TAG_VALUE; 64 static const std::string TAG_PARENT; 65 static const std::string TAG_QUANTITY; 66 static const std::vector<std::string> QUANTITY_ATTRS; 67 }; 68 } 69 } 70 } 71 #endif