1 /*
2  * Copyright (c) 2024-2024 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/xml_element.h"
17 
18 namespace OHOS {
19 namespace Media {
20 namespace Plugins {
21 namespace HttpPlugin {
XmlElement(xmlNodePtr xmlNode)22 XmlElement::XmlElement(xmlNodePtr xmlNode)
23 {
24     xmlNodePtr_ = xmlNode;
25 }
26 
GetParent()27 std::shared_ptr<XmlElement> XmlElement::GetParent()
28 {
29     if (xmlNodePtr_ != nullptr) {
30         xmlNodePtr parent = xmlNodePtr_->parent;
31         if (parent != nullptr) {
32             return std::make_shared<XmlElement>(parent);
33         }
34     }
35     return nullptr;
36 }
37 
GetChild()38 std::shared_ptr<XmlElement> XmlElement::GetChild()
39 {
40     if (xmlNodePtr_ != nullptr) {
41         xmlNodePtr children = xmlNodePtr_->children;
42         if (children != nullptr) {
43             return std::make_shared<XmlElement>(children);
44         }
45     }
46     return nullptr;
47 }
48 
GetSiblingPrev()49 std::shared_ptr<XmlElement> XmlElement::GetSiblingPrev()
50 {
51     if (xmlNodePtr_ != nullptr) {
52         xmlNodePtr prev = xmlNodePtr_->prev;
53         if (prev != nullptr) {
54             return std::make_shared<XmlElement>(prev);
55         }
56     }
57     return nullptr;
58 }
59 
GetSiblingNext()60 std::shared_ptr<XmlElement> XmlElement::GetSiblingNext()
61 {
62     if (xmlNodePtr_ != nullptr) {
63         xmlNodePtr next = xmlNodePtr_->next;
64         if (next != nullptr) {
65             return std::make_shared<XmlElement>(next);
66         }
67     }
68     return nullptr;
69 }
70 
GetLast()71 std::shared_ptr<XmlElement> XmlElement::GetLast()
72 {
73     if (xmlNodePtr_ != nullptr) {
74         xmlNodePtr last = xmlNodePtr_->last;
75         if (last != nullptr) {
76             return std::make_shared<XmlElement>(last);
77         }
78     }
79     return nullptr;
80 }
81 
GetXmlNode()82 xmlNodePtr XmlElement::GetXmlNode()
83 {
84     return xmlNodePtr_;
85 }
86 
GetName()87 std::string XmlElement::GetName()
88 {
89     if (xmlNodePtr_ != nullptr) {
90         const xmlChar *name = xmlNodePtr_->name;
91         if (name != nullptr) {
92             std::string nameValue(reinterpret_cast<const char *>(name));
93             return nameValue;
94         }
95     }
96     return "";
97 }
98 
GetText()99 std::string XmlElement::GetText()
100 {
101     if (xmlNodePtr_ != nullptr) {
102         xmlChar *content = xmlNodeGetContent(xmlNodePtr_);
103         if (content != nullptr) {
104             std::string contentValue(reinterpret_cast<char *>(content));
105             return contentValue;
106         }
107     }
108     return "";
109 }
110 
GetAttribute(const std::string & attrName)111 std::string XmlElement::GetAttribute(const std::string &attrName)
112 {
113     if (xmlNodePtr_ != nullptr && !attrName.empty()) {
114         if (xmlHasProp(xmlNodePtr_, BAD_CAST attrName.c_str())) {
115             xmlChar *attr = xmlGetProp(xmlNodePtr_, BAD_CAST attrName.c_str());
116             if (attr != nullptr) {
117                 std::string attrValue(reinterpret_cast<char *>(attr));
118                 xmlFree(attr);
119                 return attrValue;
120             }
121         }
122     }
123     return "";
124 }
125 } // namespace HttpPluginLite
126 } // namespace Plugin
127 } // namespace Media
128 } // namespace OHOS