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 if (numChar == nullptr) {
120 WIFI_LOGE("GetStringArrValue numChar failed");
121 return stringArr;
122 }
123 std::string temp = std::string(reinterpret_cast<char *>(numChar));
124 int num = CheckDataLegal(temp);
125 xmlFree(numChar);
126 if (num == 0) {
127 return stringArr;
128 }
129 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
130 if (xmlStrcmp(node->name, BAD_CAST"item") == 0) {
131 xmlChar* value = xmlGetProp(node, BAD_CAST"value");
132 if (value == nullptr) {
133 WIFI_LOGE("GetStringArrValue value failed");
134 return stringArr;
135 }
136 stringArr.push_back(std::string(reinterpret_cast<char *>(value)));
137 xmlFree(value);
138 }
139 }
140 return stringArr;
141 }
142
GetByteArrValue(xmlNodePtr node)143 std::vector<unsigned char> XmlParser::GetByteArrValue(xmlNodePtr node)
144 {
145 std::vector<unsigned char> byteArr{};
146 if (node == nullptr) {
147 return byteArr;
148 }
149 xmlChar* numChar = xmlGetProp(node, BAD_CAST"num");
150 if (numChar == nullptr) {
151 WIFI_LOGE("GetByteArrValue numChar failed");
152 return byteArr;
153 }
154 std::string temp = std::string(reinterpret_cast<char *>(numChar));
155 int num = CheckDataLegal(temp);
156 xmlChar *value = xmlNodeGetContent(node);
157 std::string valueStr = std::string(reinterpret_cast<char *>(value));
158 xmlFree(numChar);
159 xmlFree(value);
160 if (valueStr.length() != 2 * static_cast<size_t>(num)) { // byte length check
161 return byteArr;
162 }
163 for (size_t i = 0; i < valueStr.length(); i += 2) { // trans string to byte
164 std::string byteString = valueStr.substr(i, 2);
165 unsigned char byte = static_cast<unsigned char>(CheckDataLegalHex(byteString)); // hex
166 byteArr.push_back(byte);
167 }
168 return byteArr;
169 }
170
GetStringMapValue(xmlNodePtr innode)171 std::map<std::string, std::string> XmlParser::GetStringMapValue(xmlNodePtr innode)
172 {
173 std::map<std::string, std::string> strMap{};
174 if (innode == nullptr) {
175 return strMap;
176 }
177 for (xmlNodePtr node = innode->children; node != nullptr; node = node->next) {
178 std::string name;
179 std::string value;
180 if (xmlStrcmp(node->name, BAD_CAST"string") == 0) {
181 xmlChar* xname = xmlGetProp(node, BAD_CAST"name");
182 if (xname == nullptr) {
183 WIFI_LOGE("GetStringMapValue xname failed");
184 return strMap;
185 }
186 xmlChar* xvalue = xmlNodeGetContent(node);
187 name = std::string(reinterpret_cast<char *>(xname));
188 value = std::string(reinterpret_cast<char *>(xvalue));
189 strMap[name] = value;
190 xmlFree(xname);
191 xmlFree(xvalue);
192 }
193 }
194 return strMap;
195 }
196
IsDocValid(xmlNodePtr node)197 bool XmlParser::IsDocValid(xmlNodePtr node)
198 {
199 if (node == nullptr) {
200 return false;
201 }
202 return (xmlStrcmp(node->name, BAD_CAST(XML_TAG_DOCUMENT_HEADER)) == 0);
203 }
204 }
205 }