1 /*
2  * Copyright (c) 2022 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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_CONFIG_READER_H
17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_CONFIG_READER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include "config_info.h"
22 #include "libxml/parser.h"
23 #include "libxml/xpath.h"
24 
25 namespace OHOS {
26 namespace ResourceSchedule {
27 class ConfigReader {
28 public:
29     /**
30      * Load the config file, parse the xml stream and construct the objects.
31      *
32      * @param configFile The xml filepath.
33      * @return Returns true if parse successfully.
34      */
35     bool LoadFromConfigContent(const std::string& content);
36 
37     /**
38      * Xml parse successfully, the config object has resolved.
39      *
40      * @param pluginName The plugin name.
41      * @param configName The config name.
42      * @return Returns the config.
43      */
44     PluginConfig GetConfig(const std::string& pluginName, const std::string& configName);
45 
46     void Dump(std::string &result);
47 
48     void DumpItem(const Item& item, std::string &result);
49 
50 private:
51     static bool IsInvalidNode(const xmlNode& currNode);
52     void ParseProperties(const xmlNode& currNode, std::map<std::string, std::string>& properties);
53     void ParseSubItem(const xmlNode& currNode, Item& item);
54     void ParseItem(const xmlNode& parentNode, PluginConfig& pluginConfig);
55     void ParseConfig(const xmlNode& parentNode, PluginConfigMap& pluginConfigMap);
56     void MergeConfigList(std::map<std::string, PluginConfigMap>& pluginConfigs);
57     void MergePluginConfigMap(PluginConfigMap& curPluginConfigMap, const PluginConfigMap& nextPluginConfigMap);
58     bool ParsePluginConfig(const xmlNode& currNode, std::map<std::string, PluginConfigMap>& pluginConfigs);
59 
60     std::mutex configMutex_;
61     std::map<std::string, PluginConfigMap> allPluginConfigs_;
62 };
63 } // namespace ResourceSchedule
64 } // namespace OHOS
65 
66 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_CONFIG_READER_H
67