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_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
24namespace OHOS {
25namespace Global {
26namespace Restool {
27class ConfigParser {
28public:
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;
50    const std::map<std::string, std::set<uint32_t>> GetCheckNode() const
51    {
52        return jsonCheckIds_;
53    }
54    void SetDependEntry(const bool isDenpend)
55    {
56        dependEntry = isDenpend;
57    }
58    bool IsDependEntry()
59    {
60        return dependEntry;
61    }
62    bool IsHar()
63    {
64        return moduleType_ == ModuleType::HAR;
65    }
66    static void SetUseModule()
67    {
68        useModule_ = true;
69    };
70    static std::string GetConfigName()
71    {
72        return useModule_ ? MODULE_JSON : CONFIG_JSON;
73    };
74private:
75    bool ParseModule(cJSON *moduleNode);
76    bool ParseDistro(cJSON *distroNode);
77    bool ParseAbilities(const cJSON *abilities);
78    bool ParseAbilitiy(const cJSON *ability, bool &isMainAbility);
79    bool IsMainAbility(const cJSON *skills);
80    bool IsHomeAction(const cJSON *actions);
81    bool dependEntry = false;
82    bool ParseRefImpl(cJSON *parent, const std::string &key, cJSON *node);
83    bool ParseJsonArrayRef(cJSON *parent, const std::string &key, cJSON *node);
84    bool ParseJsonStringRef(cJSON *parent, const std::string &key, cJSON *node);
85    bool GetRefIdFromString(std::string &value, bool &update, const std::string &match) const;
86    bool ParseModuleType(const std::string &type);
87    bool ParseAbilitiesForDepend(cJSON *moduleNode);
88    void AddCheckNode(const std::string &key, uint32_t id);
89    std::string filePath_;
90    std::string packageName_;
91    std::string moduleName_;
92    ModuleType moduleType_;
93    std::string mainAbility_;
94    int64_t abilityIconId_;
95    int64_t abilityLabelId_;
96    std::map<std::string, std::set<uint32_t>> jsonCheckIds_;
97    static const std::map<std::string, ModuleType> MODULE_TYPES;
98    static const std::map<std::string, std::string> JSON_STRING_IDS;
99    static const std::map<std::string, std::string> JSON_ARRAY_IDS;
100    static bool useModule_;
101    cJSON *root_;
102};
103}
104}
105}
106#endif