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