1 /*
2 * Copyright (C) 2022 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_parse.h"
17 #include "media_errors.h"
18 #include "media_log.h"
19 #include "string_ex.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "XmlParser"};
23 }
24
25 namespace OHOS {
26 namespace Media {
~XmlParser()27 XmlParser::~XmlParser()
28 {
29 Destroy();
30 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
31 }
32
LoadConfiguration(const char * xmlPath)33 bool XmlParser::LoadConfiguration(const char *xmlPath)
34 {
35 mDoc_ = xmlReadFile(xmlPath, nullptr, 0);
36 CHECK_AND_RETURN_RET_LOG(mDoc_ != nullptr, false, "XmlParser xmlReadFile failed");
37 return true;
38 }
39
Parse()40 bool XmlParser::Parse()
41 {
42 xmlNode *root = xmlDocGetRootElement(mDoc_);
43 CHECK_AND_RETURN_RET_LOG(root != nullptr, false, "XmlParser xmlDocGetRootElement failed");
44 return ParseInternal(root);
45 }
46
Destroy()47 void XmlParser::Destroy()
48 {
49 if (mDoc_ != nullptr) {
50 xmlFreeDoc(mDoc_);
51 }
52 return;
53 }
54
IsNumberArray(const std::vector<std::string> & strArray) const55 bool XmlParser::IsNumberArray(const std::vector<std::string> &strArray) const
56 {
57 for (auto iter = strArray.begin(); iter != strArray.end(); iter++) {
58 for (char const &c : *iter) {
59 if (std::isdigit(c) == 0) {
60 return false;
61 }
62 }
63 }
64 return true;
65 }
66
TransStrAsRange(const std::string & str,Range & range) const67 bool XmlParser::TransStrAsRange(const std::string &str, Range &range) const
68 {
69 CHECK_AND_RETURN_RET_LOG(str != "null", false, "str is null");
70 CHECK_AND_RETURN_RET_LOG(str != "", false, "str is empty");
71 size_t pos = str.find("-");
72 if (pos != str.npos && pos + 1 < str.size()) {
73 std::string head = str.substr(0, pos);
74 std::string tail = str.substr(pos + 1);
75 bool ret = StrToInt(head, range.minVal);
76 CHECK_AND_RETURN_RET_LOG(ret == true, false,
77 "call StrToInt func false, input head is: %{public}s", head.c_str());
78
79 ret = StrToInt(tail, range.maxVal);
80 CHECK_AND_RETURN_RET_LOG(ret == true, false,
81 "call StrToInt func false, input tail is: %{public}s", tail.c_str());
82 } else {
83 MEDIA_LOGD("Can not find the delimiter of \"-\" in : %{public}s", str.c_str());
84 return false;
85 }
86 return true;
87 }
88
TransStrAsIntegerArray(const std::vector<std::string> & spilt) const89 std::vector<int32_t> XmlParser::TransStrAsIntegerArray(const std::vector<std::string> &spilt) const
90 {
91 std::vector<int32_t> array;
92 for (auto iter = spilt.begin(); iter != spilt.end(); iter++) {
93 int32_t num = -1;
94 bool ret = StrToInt(*iter, num);
95 CHECK_AND_RETURN_RET_LOG(ret == true, array,
96 "call StrToInt func false, input str is: %{public}s", iter->c_str());
97 array.push_back(num);
98 }
99 return array;
100 }
101
SpiltKeyList(const std::string & str,const std::string & delim,std::vector<std::string> & spilt) const102 bool XmlParser::SpiltKeyList(const std::string &str, const std::string &delim,
103 std::vector<std::string> &spilt) const
104 {
105 CHECK_AND_RETURN_RET_LOG(str != "", false, "str is null");
106 std::string strAddDelim = str;
107 if (str.back() != delim.back()) {
108 strAddDelim = str + delim;
109 }
110 size_t size = strAddDelim.size();
111 for (size_t i = 0; i < size; ++i) {
112 size_t pos = strAddDelim.find(delim, i);
113 if (pos != strAddDelim.npos) {
114 std::string s = strAddDelim.substr(i, pos - i);
115 spilt.push_back(s);
116 i = pos + delim.size() - 1;
117 }
118 }
119 return true;
120 }
121
SetCapabilityStringData(std::unordered_map<std::string,std::string &> dataMap,const std::string & capabilityKey,const std::string & capabilityValue) const122 bool XmlParser::SetCapabilityStringData(std::unordered_map<std::string, std::string&> dataMap,
123 const std::string &capabilityKey, const std::string &capabilityValue) const
124 {
125 dataMap.at(capabilityKey) = capabilityValue;
126 return true;
127 }
128
SetCapabilityRangeData(std::unordered_map<std::string,Range &> dataMap,const std::string & capabilityKey,const std::string & capabilityValue) const129 bool XmlParser::SetCapabilityRangeData(std::unordered_map<std::string, Range&> dataMap,
130 const std::string &capabilityKey, const std::string &capabilityValue) const
131 {
132 Range range;
133 bool ret = TransStrAsRange(capabilityValue, range);
134 CHECK_AND_RETURN_RET_LOG(ret != false, false, "failed:can not trans %{public}s", capabilityValue.c_str());
135 dataMap.at(capabilityKey) = range;
136 return true;
137 }
138 } // namespace Media
139 } // namespace OHOS