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_parser.h"
17 #include "common/log.h"
18
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_STREAM_SOURCE, "HiStreamer" };
21 }
22
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
26 namespace HttpPlugin {
ParseFromBuffer(const char * buf,int32_t length)27 int32_t XmlParser::ParseFromBuffer(const char *buf, int32_t length)
28 {
29 if (buf == nullptr || length == 0) {
30 MEDIA_LOG_E("ParseFromBuffer buffer is empty, length: " PUBLIC_LOG_D32, length);
31 return -1;
32 }
33
34 xmlInitParser();
35 xmlDocPtr doc = xmlParseMemory(buf, length);
36 if (doc == nullptr) {
37 MEDIA_LOG_E("ParseFromBuffer error.");
38 return -1;
39 }
40
41 xmlDocPtr_ = doc;
42 return 0;
43 }
44
ParseFromString(const std::string & xmlStr)45 int32_t XmlParser::ParseFromString(const std::string &xmlStr)
46 {
47 if (xmlStr.empty()) {
48 MEDIA_LOG_E("ParseFromString buffer is empty.");
49 return -1;
50 }
51
52 const xmlChar *docData = reinterpret_cast<const unsigned char *>(xmlStr.c_str());
53 if (docData == nullptr) {
54 MEDIA_LOG_E("ParseFromString xmlStr is error.");
55 return -1;
56 }
57
58 xmlInitParser();
59 xmlDocPtr doc = xmlParseDoc(docData);
60 if (doc == nullptr) {
61 MEDIA_LOG_E("ParseFromString error.");
62 return -1;
63 }
64
65 xmlDocPtr_ = doc;
66 return 0;
67 }
68
ParseFromFile(const std::string & filePath)69 int32_t XmlParser::ParseFromFile(const std::string &filePath)
70 {
71 if (filePath.empty()) {
72 MEDIA_LOG_E("ParseFromString filePath is empty.");
73 return -1;
74 }
75
76 xmlInitParser();
77 xmlDocPtr doc = xmlParseFile(filePath.c_str());
78 if (doc == nullptr) {
79 MEDIA_LOG_E("ParseFromFile error.");
80 return -1;
81 }
82
83 xmlDocPtr_ = doc;
84 return 0;
85 }
86
GetRootElement()87 std::shared_ptr<XmlElement> XmlParser::GetRootElement()
88 {
89 if (xmlDocPtr_ != nullptr) {
90 xmlNodePtr rootNode = xmlDocGetRootElement(xmlDocPtr_);
91 if (rootNode != nullptr) {
92 return std::make_shared<XmlElement>(rootNode);
93 }
94 }
95 return nullptr;
96 }
97
GetAttribute(std::shared_ptr<XmlElement> element,const std::string attrName,std::string & value)98 int32_t XmlParser::GetAttribute(std::shared_ptr<XmlElement> element, const std::string attrName, std::string &value)
99 {
100 if (element == nullptr) {
101 return -1;
102 }
103
104 value = element->GetAttribute(attrName);
105 return 0;
106 }
107
SkipElementNameSpace(std::string & elementName) const108 void XmlParser::SkipElementNameSpace(std::string &elementName) const
109 {
110 std::string::size_type colonLastIndex = elementName.find_last_of(":");
111 if (colonLastIndex != std::string::npos) {
112 elementName = elementName.substr(colonLastIndex + 1);
113 }
114 }
115
DestroyDoc()116 void XmlParser::DestroyDoc()
117 {
118 if (xmlDocPtr_ != nullptr) {
119 xmlFreeDoc(xmlDocPtr_);
120 xmlCleanupParser();
121 xmlMemoryDump();
122 }
123 }
124 } // namespace HttpPluginLite
125 } // namespace Plugin
126 } // namespace Media
127 } // namespace OHOS