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 /**
17 * @addtogroup Bluetooth
18 * @{
19 *
20 * @brief Defines map mce xml .
21 *
22 */
23
24 /**
25 * @file map_mce_xml.cpp
26 *
27 * @brief map mce xml source file .
28 *
29 */
30
31 #include "map_mce_xml.h"
32 #include <cstring>
33
34 namespace OHOS {
35 namespace bluetooth {
MceXmlAttribute()36 MceXmlAttribute::MceXmlAttribute() : value_("")
37 {
38 // do nothing
39 }
40
SetAttribute(const std::string & value)41 void MceXmlAttribute::SetAttribute(const std::string &value)
42 {
43 value_ = value;
44 }
45
AsString() const46 std::string MceXmlAttribute::AsString() const
47 {
48 return value_;
49 }
50
AsInt()51 int MceXmlAttribute::AsInt()
52 {
53 if (value_ != "") {
54 int num = atoi(value_.c_str());
55 return num;
56 } else {
57 return 0;
58 }
59 }
60
MceXmlNode()61 MceXmlNode::MceXmlNode()
62 {
63 empty_ = true;
64 }
65
SetNode(xmlNode node)66 void MceXmlNode::SetNode(xmlNode node)
67 {
68 node_ = node;
69 empty_ = false;
70 }
71
Attribute(std::string name) const72 MceXmlAttribute MceXmlNode::Attribute(std::string name) const
73 {
74 xmlChar *value = xmlGetProp(&node_, (const xmlChar *)name.c_str());
75 std::string str;
76 MceXmlAttribute mceAttr;
77 if (value != nullptr) {
78 str = (char *)value;
79 mceAttr.SetAttribute(str);
80 xmlFree(value);
81 } else {
82 str = "";
83 mceAttr.SetAttribute(str);
84 }
85 return mceAttr;
86 }
87
Empty() const88 bool MceXmlNode::Empty() const
89 {
90 return empty_;
91 }
92
Name() const93 std::string MceXmlNode::Name() const
94 {
95 std::string retStr((const char *)node_.name);
96 return retStr;
97 }
98
FirstChild() const99 MceXmlNode MceXmlNode::FirstChild() const
100 {
101 xmlNodePtr first = xmlFirstElementChild((xmlNodePtr)&node_);
102 MceXmlNode node;
103 if (first != nullptr) {
104 node.SetNode(*first);
105 }
106 return node;
107 }
108
NextSibling() const109 MceXmlNode MceXmlNode::NextSibling() const
110 {
111 xmlNodePtr next = xmlNextElementSibling((xmlNodePtr)&node_);
112 MceXmlNode node;
113 if (next != nullptr) {
114 node.SetNode(*next);
115 }
116 return node;
117 }
118
~MceXmlDoc()119 MceXmlDoc::~MceXmlDoc()
120 {
121 if (pDoc_ != nullptr) {
122 xmlFreeDoc(pDoc_);
123 }
124 }
125
LoadString(std::string content)126 void MceXmlDoc::LoadString(std::string content)
127 {
128 if (content != "") {
129 pDoc_ = xmlReadMemory(content.c_str(), int(content.size()), NULL, "UTF-8", XML_PARSE_RECOVER);
130 }
131 }
132
Child(std::string name) const133 MceXmlNode MceXmlDoc::Child(std::string name) const
134 {
135 xmlNodePtr root = xmlDocGetRootElement(pDoc_);
136 MceXmlNode node;
137 if (root != nullptr) {
138 if (!xmlStrcmp(root->name, (const xmlChar *)name.c_str())) {
139 node.SetNode(*root);
140 }
141 }
142 return node;
143 }
144 } // namespace bluetooth
145 } // namespace OHOS
146