• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_CONFIG_PARSER_H
17 #define OHOS_RESTOOL_CONFIG_PARSER_H
18 
19 #include <functional>
20 #include <set>
21 #include <cJSON.h>
22 #include "resource_util.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace Restool {
27 class ConfigParser {
28 public:
29     enum class ModuleType {
30         NONE = 0,
31         HAR = 1,
32         ENTRY = 2,
33         FEATURE = 3,
34         SHARED = 4,
35     };
36 
37     ConfigParser();
38     explicit ConfigParser(const std::string &filePath);
39     virtual ~ConfigParser();
40     uint32_t Init();
41     const std::string &GetPackageName() const;
42     const std::string &GetModuleName() const;
43     int64_t GetAbilityIconId() const;
44     int64_t GetAbilityLabelId() const;
45     bool SetAppIcon(std::string &icon, int64_t id);
46     bool SetAppLabel(std::string &label, int64_t id);
47     ModuleType GetModuleType() const;
48     uint32_t ParseRefence();
49     uint32_t Save(const std::string &filePath) const;
GetCheckNode()50     const std::map<std::string, std::set<uint32_t>> GetCheckNode() const
51     {
52         return jsonCheckIds_;
53     }
SetDependEntry(const bool isDenpend)54     void SetDependEntry(const bool isDenpend)
55     {
56         dependEntry = isDenpend;
57     }
IsDependEntry()58     bool IsDependEntry()
59     {
60         return dependEntry;
61     }
IsHar()62     bool IsHar()
63     {
64         return moduleType_ == ModuleType::HAR;
65     }
GetConfigFilePath()66     std::string GetConfigFilePath()
67     {
68         return filePath_;
69     }
isSupportNewModule()70     inline bool isSupportNewModule()
71     {
72         return newModule_;
73     }
SetUseModule()74     static void SetUseModule()
75     {
76         useModule_ = true;
77     };
GetConfigName()78     static std::string GetConfigName()
79     {
80         return useModule_ ? MODULE_JSON : CONFIG_JSON;
81     };
82 private:
83     bool ParseModule(cJSON *moduleNode);
84     bool ParseDistro(cJSON *distroNode);
85     bool ParseAbilities(const cJSON *abilities);
86     bool ParseAbilitiy(const cJSON *ability, bool &isMainAbility);
87     bool IsMainAbility(const cJSON *skills);
88     bool IsHomeAction(const cJSON *actions);
89     bool dependEntry = false;
90     bool ParseRefImpl(cJSON *parent, const std::string &key, cJSON *node);
91     bool ParseJsonArrayRef(cJSON *parent, const std::string &key, cJSON *node);
92     bool ParseJsonStringRef(cJSON *parent, const std::string &key, cJSON *node);
93     bool GetRefIdFromString(std::string &value, bool &update, const std::string &match) const;
94     bool ParseModuleType(const std::string &type);
95     bool ParseAbilitiesForDepend(cJSON *moduleNode);
96     void AddCheckNode(const std::string &key, uint32_t id);
97     std::string filePath_;
98     std::string packageName_;
99     std::string moduleName_;
100     ModuleType moduleType_;
101     std::string mainAbility_;
102     int64_t abilityIconId_;
103     int64_t abilityLabelId_;
104     std::map<std::string, std::set<uint32_t>> jsonCheckIds_;
105     static const std::map<std::string, ModuleType> MODULE_TYPES;
106     static const std::map<std::string, std::string> JSON_STRING_IDS;
107     static const std::map<std::string, std::string> JSON_ARRAY_IDS;
108     static bool useModule_;
109     cJSON *root_;
110     bool newModule_ = false;
111 };
112 }
113 }
114 }
115 #endif