1 /*
2  * Copyright (C) 2023-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 XML_PARSER
17 #define XML_PARSER
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <iostream>
21 #include <map>
22 
23 constexpr auto XML_TAG_DOCUMENT_HEADER = "WifiConfigStoreData";
ConvertStringToBool(const std::string str)24 inline bool ConvertStringToBool(const std::string str)
25 {
26     if (str == "true") {
27         return true;
28     } else {
29         return false;
30     }
31 }
32 namespace OHOS {
33 namespace Wifi {
34 enum PrimType {
35     INT,
36     LONG,
37     BOOLEAN,
38 };
39 
40 class XmlParser {
41 public:
42     virtual ~XmlParser();
43     /**
44      * @Description load a Configuration xml
45      *
46      * @param xmlPath - path of config xml
47      * @return bool - true for succ false for fail
48     */
49     bool LoadConfiguration(const char *xmlPath);
50 
51     /**
52      * @Description load xml in memory
53      *
54      * @param xml - memory data
55      * @return bool - true for succ false for fail
56     */
57     bool LoadConfigurationMemory(const char *xml);
58 
59     /**
60      * @Description parse Configuration xml
61      *
62      * @return bool - true for succ false for fail
63     */
64     bool Parse();
65 
66     /**
67      * @Description get xml node name value
68      *
69      * @return std::string - node name value
70     */
71     std::string GetNameValue(xmlNodePtr node);
72 
73     /**
74      * @Description get xml node value
75      *
76      * @param node - xmlNodePtr
77      * @return std::string - node value
78     */
79     std::string GetNodeValue(xmlNodePtr node);
80 
81     /**
82      * @Description get xml node string content
83      *
84      * @param node - xmlNodePtr
85      * @return std::string - xml node string content
86     */
87     std::string GetStringValue(xmlNodePtr node);
88 
89     /**
90      * @Description get xml node prime value eg:int bool
91      *
92      * @param node - xmlNodePtr
93      * @param type - PrimType,eg INT,BOOL
94      * @return T - prime value eg:int bool
95     */
96     template<typename T>
GetPrimValue(xmlNodePtr node,const PrimType type)97     T GetPrimValue(xmlNodePtr node, const PrimType type)
98     {
99         T primValue{};
100         xmlChar* value = xmlGetProp(node, (const xmlChar*)"value");
101         std::string valueString = std::string(reinterpret_cast<char *>(value));
102         xmlFree(value);
103         switch (type) {
104             case PrimType::INT:
105                 primValue = std::stoi(valueString);
106                 break;
107             case PrimType::LONG:
108                 primValue = std::stol(valueString);
109                 break;
110             case PrimType::BOOLEAN:
111                 primValue = ConvertStringToBool(valueString);
112                 break;
113             default: {
114                 break;
115             }
116         }
117         return reinterpret_cast<T>(primValue);
118     };
119 
120     /**
121      * @Description get xml node string Array value
122      *
123      * @param innode - xmlNodePtr
124      * @return std::vector<std::string> - prime value eg:int bool
125     */
126     std::vector<std::string> GetStringArrValue(xmlNodePtr innode);
127 
128     /**
129      * @Description get xml node byte Array value
130      *
131      * @param innode - xmlNodePtr
132      * @return std::vector<unsigned char> - byte Array value
133     */
134     std::vector<unsigned char> GetByteArrValue(xmlNodePtr node);
135 
136     /**
137      * @Description get xml node string map value
138      *
139      * @param innode - xmlNodePtr
140      * @return std::map<std::string, std::string> - node string map value
141     */
142     std::map<std::string, std::string> GetStringMapValue(xmlNodePtr innode);
143 
144     /**
145      * @Description check doc is vaild
146      *
147      * @param node - xmlNodePtr
148      * @return bool - true for valid false for unvalid
149     */
150     bool IsDocValid(xmlNodePtr node);
151 
152 private:
153     virtual bool ParseInternal(xmlNodePtr node) = 0;
154     void Destroy();
155 
156 private:
157     xmlDoc *mDoc_ = nullptr;
158 };
159 }
160 }
161 #endif