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_REFERENCE_PARSER_H
17 #define OHOS_RESTOOL_REFERENCE_PARSER_H
18 
19 #include <cJSON.h>
20 #include "id_worker.h"
21 #include "resource_data.h"
22 #include "resource_item.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace Restool {
27 class ReferenceParser {
28 public:
29     ReferenceParser();
30     virtual ~ReferenceParser();
31     uint32_t ParseRefInResources(std::map<int64_t, std::vector<ResourceItem>> &items, const std::string &output);
32     uint32_t ParseRefInResourceItem(ResourceItem &resourceItem) const;
33     uint32_t ParseRefInJsonFile(ResourceItem &resourceItem, const std::string &output, const bool isIncrement = false);
34     uint32_t ParseRefInString(std::string &value, bool &update) const;
35     static std::map<int64_t, std::set<int64_t>> &GetLayerIconIds();
36 private:
37     bool ParseRefJson(const std::string &from, const std::string &to);
38     bool ParseRefResourceItemData(const ResourceItem &resourceItem, std::string &data, bool &update) const;
39     bool IsStringOfResourceItem(ResType resType) const;
40     bool IsArrayOfResourceItem(ResType resType) const;
41     bool IsElementRef(const ResourceItem &resourceItem) const;
42     bool IsMediaRef(const ResourceItem &resourceItem) const;
43     bool IsProfileRef(const ResourceItem &resourceItem) const;
44     bool ParseRefString(std::string &key) const;
45     bool ParseRefString(std::string &key, bool &update) const;
46     bool ParseRefImpl(std::string &key, const std::map<std::string, ResType> &refs, bool isSystem) const;
47     bool ParseRefJsonImpl(cJSON *root, bool &needSave) const;
48     const IdWorker &idWorker_;
49     static const std::map<std::string, ResType> ID_REFS;
50     static const std::map<std::string, ResType> ID_OHOS_REFS;
51     static std::map<int64_t, std::set<int64_t>> layerIconIds_;
52     cJSON *root_;
53     bool isParsingMediaJson_;
54     int64_t mediaJsonId_{ INVALID_ID };
55 };
56 }
57 }
58 }
59 #endif