1 /*
2  * Copyright (C) 2021 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_PARSE_H
17 #define XML_PARSE_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "base_def.h"
23 
24 namespace utility {
25 #define SIZEOF_0X 2
26 #define BASE_16 16
27 class XmlParse {
28 public:
29     /**
30      * @brief Construct a new Xml Parse object
31      *
32      * @since 6
33      */
34     XmlParse();
35 
36     /**
37      * @brief Destroy the Xml Parse object
38      *
39      * @since 6
40      */
41     ~XmlParse();
42 
43     /**
44      * @brief Load XML document form file.
45      *
46      * @param path File path.
47      * @return Success load xml document return true, else return false.
48      * @since 6
49      */
50     bool Load(const std::string &path);
51 
52     /**
53      * @brief Parse XML document.
54      *
55      * @return Success parse XML document return true, else return false.
56      * @since 6
57      */
58     bool Parse();
59 
60     /**
61      * @brief Store back to XML document.
62      *
63      * @return Success store back to XML document return true, else return false.
64      * @since 6
65      */
66     bool Save();
67 
68     /**
69      * @brief Get specified property value.
70      *        Value type is int.
71      *
72      * @param section Xml section.
73      * @param subSection Xml subSection.
74      * @param property Xml property.
75      * @param value Value type is int.
76      * @return Success get specified property's value return true, else return false.
77      * @since 6
78      */
79     bool GetValue(const std::string &section, const std::string &subSection, const std::string &property, int &value);
80 
81     /**
82      * @brief Get specified property value.
83      *        Value type is string.
84      *
85      * @param section Xml section.
86      * @param subSection Xml subSection.
87      * @param property Xml property.
88      * @param value Value type is string.
89      * @return Success get specified property's value return true, else return false.
90      * @since 6
91      */
92     bool GetValue(
93         const std::string &section, const std::string &subSection, const std::string &property, std::string &value);
94 
95     /**
96      * @brief Get specified property value.
97      *        Value type is bool.
98      *
99      * @param section Xml section.
100      * @param subSection Xml subSection.
101      * @param property Xml property.
102      * @param value Value type is bool.
103      * @return Success get specified property's value return true, else return false.
104      * @since 6
105      */
106     bool GetValue(const std::string &section, const std::string &subSection, const std::string &property, bool &value);
107 
108     /**
109      * @brief Set specified property value.
110      *        Value type is int.
111      *
112      * @param section Xml section.
113      * @param subSection Xml subSection.
114      * @param property Xml property.
115      * @param value Value type is const int.
116      * @return Success set specified property's value return true, else return false.
117      * @since 6
118      */
119     bool SetValue(
120         const std::string &section, const std::string &subSection, const std::string &property, const int &value);
121 
122     /**
123      * @brief Set specified property value. Value type is string.
124      *
125      * @param section Xml section.
126      * @param subSection Xml subSection.
127      * @param property Xml property.
128      * @param value Value type is const string.
129      * @return Success set specified property's value return true, else return false.
130      * @since 6
131      */
132     bool SetValue(const std::string &section, const std::string &subSection, const std::string &property,
133         const std::string &value);
134 
135     /**
136      * @brief Set specified property value. Value type is bool.
137      *
138      * @param section Xml section.
139      * @param subSection Xml subSection.
140      * @param property Xml property.
141      * @param value Value type is const bool.
142      * @return Success set specified property's value return true, else return false.
143      * @since 6
144      */
145     bool SetValue(
146         const std::string &section, const std::string &subSection, const std::string &property, const bool &value);
147 
148     /**
149      * @brief Whether XML document has specified section.
150      *
151      * @param section Xml section.
152      * @param subSection Xml subSection.
153      * @return XML document has specified section return true, else return false.
154      * @since 6
155      */
156     bool HasSection(const std::string &section, const std::string &subSection);
157 
158     /**
159      * @brief Get Address
160      * @param section Xml section.
161      * @param subSections
162      * @return Specified section has one or Mutiple subSections return true, else return false.
163      * @since 6
164      */
165     bool GetSubSections(const std::string &section, std::vector<std::string> &subSections);
166 
167     /**
168      * @brief Whether XML document has specified property.
169      *
170      * @param section Xml section.
171      * @param subSection
172      * @param property
173      * @return XML document has specified property return true, else return false.
174      * @since 6
175      */
176     bool HasProperty(const std::string &section, const std::string &subSection, const std::string &property);
177 
178     /**
179      * @brief Remove XML document specified section.
180      *
181      * @param section Xml section.
182      * @param subSection Xml subSection.
183      * @return Success remove XML document specified section return true, else return false.
184      * @since 6
185      */
186     bool RemoveSection(const std::string &section, const std::string &subSection);
187 
188     /**
189      * @brief Remove XML document specified property.
190      *
191      * @param section Xml section.
192      * @param subSection Xml subSection.
193      * @param property Xml property.
194      * @return Success remove XML document specified property return true, else return false.
195      * @since 6
196      */
197     bool RemoveProperty(const std::string &section, const std::string &subSection, const std::string &property);
198 
199     /**
200      * @brief Get specified property value.
201      *        Value type is int.
202      *
203      * @param section Xml section.
204      * @param property Xml property.
205      * @param value Value type is int.
206      * @return Success get specified property's value return true, else return false.
207      * @since 6
208      */
209     bool GetValue(const std::string &section, const std::string &property, int &value);
210 
211     /**
212      * @brief Get specified property value.
213      *        Value type is string.
214      * @param section Xml section.
215      * @param property Xml property.
216      * @param value Value type is string.
217      * @return Success get specified property's value return true, else return false.
218      * @since 6
219      */
220     bool GetValue(const std::string &section, const std::string &property, std::string &value);
221 
222     /**
223      * @brief Get specified property value.
224      *        Value type is bool.
225      * @param section Xml section.
226      * @param property Xml property.
227      * @param value Value type is bool.
228      * @return Success get specified property's value return true, else return false.
229      * @since 6
230      */
231     bool GetValue(const std::string &section, const std::string &property, bool &value);
232 
233     /**
234      * @brief Set specified property value.
235      *        Value type is int.
236      *
237      * @param section Xml section.
238      * @param property Xml property.
239      * @param value Value type is const int.
240      * @return Success set specified property's value return true, else return false.
241      * @since 6
242      */
243     bool SetValue(const std::string &section, const std::string &property, const int &value);
244 
245     /**
246      * @brief Set specified property value.
247      *        Value type is string.
248      *
249      * @param section Xml section.
250      * @param property Xml property.
251      * @param value Value type is const string.
252      * @return Success set specified property's value return true, else return false.
253      * @since 6
254      */
255     bool SetValue(const std::string &section, const std::string &property, const std::string &value);
256 
257     /**
258      * @brief Set specified property value.
259      *        Value type is bool.
260      * @param section Xml section.
261      * @param property Xml property.
262      * @param value Value type is const bool.
263      * @return Success set specified property's value return true, else return false.
264      * @since 6
265      */
266     bool SetValue(const std::string &section, const std::string &property, const bool &value);
267 
268     /**
269      * @brief Whether XML document has specified section.
270      *
271      * @param section Xml section.
272      * @return XML document has specified section return true, else return false.
273      * @since 6
274      */
275     bool HasSection(const std::string &section);
276 
277     /**
278      * @brief Whether XML document has specified property.
279      *
280      * @param section Xml section.
281      * @param property
282      * @return XML document has specified property return true, else return false
283      * @since 6
284      */
285     bool HasProperty(const std::string &section, const std::string &property);
286 
287     /**
288      * @brief Remove XML document specified section.
289      * @param section Xml section.
290      * @return Success remove XML document specified section return true, else return false.
291      * @since 6
292      */
293     bool RemoveSection(const std::string &section);
294 
295     /**
296      * @brief Remove XML document specified property.
297      * @param section Xml section.
298      * @param property Xml property.
299      * @return Success remove XML document specified property return true, else return false.
300      * @since 6
301      */
302     bool RemoveProperty(const std::string &section, const std::string &property);
303 
304 private:
305     std::string filePath_ {""};
306 
307     DECLARE_IMPL();
308 };
309 };  // namespace utility
310 
311 #endif  // XML_PARSE_H