• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "xml_parser.h"
17 #include "wifi_logger.h"
18 #include <vector>
19 
20 namespace OHOS {
21 namespace Wifi {
22 DEFINE_WIFILOG_LABEL("XmlParser");
23 
~XmlParser()24 XmlParser::~XmlParser()
25 {
26     Destroy();
27 }
28 
Destroy()29 void XmlParser::Destroy()
30 {
31     if (mDoc_ != nullptr) {
32         xmlFreeDoc(mDoc_);
33         xmlCleanupParser();
34         mDoc_ = nullptr;
35     }
36 }
37 
LoadConfiguration(const char * xmlPath)38 bool XmlParser::LoadConfiguration(const char *xmlPath)
39 {
40     Destroy(); // clear old doc before load new doc
41     mDoc_ = xmlReadFile(xmlPath, nullptr, XML_PARSE_NOBLANKS);
42     if (mDoc_ == nullptr) {
43         WIFI_LOGE("LoadConfiguration fail");
44         return false;
45     }
46     return true;
47 }
48 
LoadConfigurationMemory(const char * xml)49 bool XmlParser::LoadConfigurationMemory(const char *xml)
50 {
51     if (xml == nullptr) {
52         WIFI_LOGE("LoadConfigurationMemory xml is nullptr");
53         return false;
54     }
55     Destroy(); // clear old doc before load new doc
56     mDoc_ = xmlReadMemory(xml, strlen(xml), nullptr, nullptr, 0);
57     if (mDoc_ == nullptr) {
58         WIFI_LOGE("LoadConfigurationMemory fail");
59         return false;
60     }
61     return true;
62 }
63 
Parse()64 bool XmlParser::Parse()
65 {
66     xmlNodePtr root = xmlDocGetRootElement(mDoc_);
67     if (root == nullptr) {
68         WIFI_LOGE("Parse root null");
69         return false;
70     }
71     return ParseInternal(root);
72 }
73 
GetNameValue(xmlNodePtr node)74 std::string XmlParser::GetNameValue(xmlNodePtr node)
75 {
76     if (node == nullptr || GetNodeValue(node).empty()) {
77         return "";
78     }
79     xmlChar *value = xmlGetProp(node, BAD_CAST"name");
80     if (value != nullptr) {
81         std::string result = std::string(reinterpret_cast<char *>(value));
82         xmlFree(value);
83         return result;
84     } else {
85         return "";
86     }
87 }
88 
GetNodeValue(xmlNodePtr node)89 std::string XmlParser::GetNodeValue(xmlNodePtr node)
90 {
91     if (node == nullptr) {
92         return "";
93     }
94     std::string nodeValue = std::string(reinterpret_cast<const char *>(node->name));
95     if (nodeValue.empty() || nodeValue == "null") {
96         return "";
97     }
98     return nodeValue;
99 }
100 
GetStringValue(xmlNodePtr node)101 std::string XmlParser::GetStringValue(xmlNodePtr node)
102 {
103     if (node == nullptr) {
104         return "";
105     }
106     xmlChar *value = xmlNodeGetContent(node);
107     std::string result = std::string(reinterpret_cast<char *>(value));
108     xmlFree(value);
109     return result;
110 }
111 
GetStringArrValue(xmlNodePtr innode)112 std::vector<std::string> XmlParser::GetStringArrValue(xmlNodePtr innode)
113 {
114     std::vector<std::string> stringArr{};
115     if (innode == nullptr) {
116         return stringArr;
117     }
118     xmlChar* numChar = xmlGetProp(innode, BAD_CAST"num");
119     int num = std::stoi(std::string(reinterpret_cast<char *>(numChar)));
120     xmlFree(numChar);
121     if (num == 0) {
122         return stringArr;
123     }
124     for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
125         if (xmlStrcmp(node->name, BAD_CAST"item") == 0) {
126             xmlChar* value = xmlGetProp(node, BAD_CAST"value");
127             stringArr.push_back(std::string(reinterpret_cast<char *>(value)));
128             xmlFree(value);
129         }
130     }
131     return stringArr;
132 }
133 
GetByteArrValue(xmlNodePtr node)134 std::vector<unsigned char> XmlParser::GetByteArrValue(xmlNodePtr node)
135 {
136     std::vector<unsigned char> byteArr{};
137     if (node == nullptr) {
138         return byteArr;
139     }
140     xmlChar* numChar = xmlGetProp(node, BAD_CAST"num");
141     int num = std::stoi(std::string(reinterpret_cast<char *>(numChar)));
142     xmlChar *value = xmlNodeGetContent(node);
143     std::string valueStr = std::string(reinterpret_cast<char *>(value));
144     xmlFree(numChar);
145     xmlFree(value);
146     if (valueStr.length() != 2 * static_cast<size_t>(num)) { // byte length check
147         return byteArr;
148     }
149     for (size_t i = 0; i < valueStr.length(); i += 2) { // trans string to byte
150         std::string byteString = valueStr.substr(i, 2);
151         unsigned char byte = static_cast<unsigned char>(std::stoi(byteString, nullptr, 16)); // hex
152         byteArr.push_back(byte);
153     }
154     return byteArr;
155 }
156 
GetStringMapValue(xmlNodePtr innode)157 std::map<std::string, std::string> XmlParser::GetStringMapValue(xmlNodePtr innode)
158 {
159     std::map<std::string, std::string> strMap{};
160     if (innode == nullptr) {
161         return strMap;
162     }
163     for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
164         std::string name;
165         std::string value;
166         if (xmlStrcmp(node->name, BAD_CAST"string") == 0) {
167             xmlChar* xname = xmlGetProp(node, BAD_CAST"name");
168             xmlChar* xvalue = xmlNodeGetContent(node);
169             name = std::string(reinterpret_cast<char *>(xname));
170             value = std::string(reinterpret_cast<char *>(xvalue));
171             strMap[name] = value;
172             xmlFree(xname);
173             xmlFree(xvalue);
174         }
175     }
176     return strMap;
177 }
178 
IsDocValid(xmlNodePtr node)179 bool XmlParser::IsDocValid(xmlNodePtr node)
180 {
181     if (node == nullptr) {
182         return false;
183     }
184     return (xmlStrcmp(node->name, BAD_CAST(XML_TAG_DOCUMENT_HEADER))  == 0);
185 }
186 }
187 }