1 /*
2  * Copyright (c) 2023 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 HGM_XML_PARSER_H
17 #define HGM_XML_PARSER_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 
25 #include "hgm_command.h"
26 #include "hgm_log.h"
27 
28 namespace OHOS::Rosen {
29 class XMLParser {
30 public:
31     int32_t LoadConfiguration(const char* fileDir);
32     int32_t Parse();
33     void Destroy();
34 
XMLParser()35     explicit XMLParser() : xmlDocument_(nullptr) {}
36 
~XMLParser()37     ~XMLParser()
38     {
39         Destroy();
40     }
41 
GetParsedData()42     std::unique_ptr<PolicyConfigData> GetParsedData()
43     {
44         return std::move(mParsedData_);
45     }
46 
47     static bool IsNumber(const std::string& str);
48 
49 private:
50     static int32_t GetHgmXmlNodeAsInt(xmlNode &node);
51     bool ParseInternal(xmlNode &node);
52     int32_t ParseParam(xmlNode &node);
53     int32_t ParseParams(xmlNode &node);
54     int32_t ParseStrategyConfig(xmlNode &node);
55     int32_t ParseScreenConfig(xmlNode &node);
56     int32_t ParseSubScreenConfig(xmlNode &node, PolicyConfigData::ScreenSetting& screenSetting);
57     int32_t ParseSimplex(xmlNode &node, std::unordered_map<std::string, std::string> &config,
58                          const std::string &valueName = "value", const std::string &keyName = "name");
59     int32_t ParserDynamicSetting(xmlNode &node, PolicyConfigData::DynamicSettingMap &dynamicSettingMap);
60     int32_t ParseSceneList(xmlNode &node, PolicyConfigData::SceneConfigMap &sceneList);
61     int32_t ParseMultiAppStrategy(xmlNode &node, PolicyConfigData::ScreenSetting &screenSetting);
62     int32_t ParseAppTypes(xmlNode &node, std::unordered_map<int32_t, std::string> &appTypes);
63     int32_t ParseVideoFrameVoteConfig(xmlNode &node);
64     std::string ExtractPropertyValue(const std::string &propName, xmlNode &node);
65     void ParseBufferStrategyList(xmlNode &node, PolicyConfigData::StrategyConfig &strategy);
66     int32_t ParseSubSequentParams(xmlNode &node, std::string &paraName);
67     void ParseAppBufferList(xmlNode &node);
68     xmlDoc *xmlDocument_ = nullptr;
69     std::unique_ptr<PolicyConfigData> mParsedData_ = nullptr;
70 };
71 } // namespace OHOS::Rosen
72 #endif // HGM_XML_PARSER_H