• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, "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     if (mDoc_ == nullptr) {
37         MEDIA_LOGE("XmlParser xmlReadFile failed");
38         return false;
39     }
40     return true;
41 }
42 
Parse()43 bool XmlParser::Parse()
44 {
45     xmlNode *root = xmlDocGetRootElement(mDoc_);
46     if (root == nullptr) {
47         MEDIA_LOGE("XmlParser xmlDocGetRootElement failed");
48         return false;
49     }
50     return ParseInternal(root);
51 }
52 
Destroy()53 void XmlParser::Destroy()
54 {
55     if (mDoc_ != nullptr) {
56         xmlFreeDoc(mDoc_);
57     }
58     return;
59 }
60 
IsNumberArray(const std::vector<std::string> & strArray) const61 bool XmlParser::IsNumberArray(const std::vector<std::string> &strArray) const
62 {
63     for (auto iter = strArray.begin(); iter != strArray.end(); iter++) {
64         for (char const &c : *iter) {
65             if (std::isdigit(c) == 0) {
66                 return false;
67             }
68         }
69     }
70     return true;
71 }
72 
TransStrAsRange(const std::string & str,Range & range) const73 bool XmlParser::TransStrAsRange(const std::string &str, Range &range) const
74 {
75     if (str == "null" || str == "") {
76         MEDIA_LOGD("str is null");
77         return false;
78     }
79     size_t pos = str.find("-");
80     if (pos != str.npos && pos + 1 < str.size()) {
81         std::string head = str.substr(0, pos);
82         std::string tail = str.substr(pos + 1);
83         if (!StrToInt(head, range.minVal)) {
84             MEDIA_LOGE("call StrToInt func false, input str is: %{public}s", head.c_str());
85             return false;
86         }
87         if (!StrToInt(tail, range.maxVal)) {
88             MEDIA_LOGE("call StrToInt func false, input str is: %{public}s", tail.c_str());
89             return false;
90         }
91     } else {
92         MEDIA_LOGD("Can not find the delimiter of \"-\" in : %{public}s", str.c_str());
93         return false;
94     }
95     return true;
96 }
97 
TransMapAsIntegerArray(const std::unordered_map<std::string,int> & capabilityMap,const std::vector<std::string> & spilt) const98 std::vector<int32_t> XmlParser::TransMapAsIntegerArray(
99     const std::unordered_map<std::string, int> &capabilityMap,
100     const std::vector<std::string> &spilt) const
101 {
102     std::vector<int32_t> res;
103     for (auto iter = spilt.begin(); iter != spilt.end(); iter++) {
104         if (capabilityMap.find(*iter) != capabilityMap.end()) {
105             res.emplace_back(capabilityMap.at(*iter));
106         } else {
107             MEDIA_LOGD("can not find %{public}s in capabilityMap", iter->c_str());
108         }
109     }
110     return res;
111 }
112 
TransStrAsIntegerArray(const std::vector<std::string> & spilt) const113 std::vector<int32_t> XmlParser::TransStrAsIntegerArray(const std::vector<std::string> &spilt) const
114 {
115     std::vector<int32_t> array;
116     for (auto iter = spilt.begin(); iter != spilt.end(); iter++) {
117         int32_t num = -1;
118         if (!StrToInt(*iter, num)) {
119             MEDIA_LOGE("call StrToInt func false, input str is: %{public}s", iter->c_str());
120             return array;
121         }
122         array.push_back(num);
123     }
124     return array;
125 }
126 
SpiltKeyList(const std::string & str,const std::string & delim,std::vector<std::string> & spilt) const127 bool XmlParser::SpiltKeyList(const std::string &str, const std::string &delim,
128     std::vector<std::string> &spilt) const
129 {
130     if (str == "") {
131         return false;
132     }
133     std::string strAddDelim = str;
134     if (str.back() != delim.back()) {
135         strAddDelim = str + delim;
136     }
137     size_t size = strAddDelim.size();
138     for (size_t i = 0; i < size; ++i) {
139         size_t pos = strAddDelim.find(delim, i);
140         if (pos != strAddDelim.npos) {
141             std::string s = strAddDelim.substr(i, pos - i);
142             spilt.push_back(s);
143             i = pos + delim.size() - 1;
144         }
145     }
146     return true;
147 }
148 
SetCapabilityStringData(std::unordered_map<std::string,std::string &> dataMap,const std::string & capabilityKey,const std::string & capabilityValue) const149 bool XmlParser::SetCapabilityStringData(std::unordered_map<std::string, std::string&> dataMap,
150     const std::string &capabilityKey, const std::string &capabilityValue) const
151 {
152     dataMap.at(capabilityKey) = capabilityValue;
153     return true;
154 }
155 
SetCapabilityBoolData(std::unordered_map<std::string,bool &> dataMap,const std::string & capabilityKey,const std::string & capabilityValue) const156 bool XmlParser::SetCapabilityBoolData(std::unordered_map<std::string, bool&> dataMap,
157     const std::string &capabilityKey, const std::string &capabilityValue) const
158 {
159     if (capabilityValue == "true") {
160         dataMap.at(capabilityKey) = true;
161     } else if (capabilityValue == "false") {
162         dataMap.at(capabilityKey) = false;
163     } else {
164         MEDIA_LOGD("The value of %{public}s in the configuration file is incorrect.", capabilityValue.c_str());
165         return false;
166     }
167     return true;
168 }
169 
SetCapabilityRangeData(std::unordered_map<std::string,Range &> dataMap,const std::string & capabilityKey,const std::string & capabilityValue) const170 bool XmlParser::SetCapabilityRangeData(std::unordered_map<std::string, Range&> dataMap,
171     const std::string &capabilityKey, const std::string &capabilityValue) const
172 {
173     Range range;
174     bool ret = TransStrAsRange(capabilityValue, range);
175     CHECK_AND_RETURN_RET_LOG(ret != false, false, "failed:can not trans %{public}s", capabilityValue.c_str());
176     dataMap.at(capabilityKey) = range;
177     return true;
178 }
179 }  // namespace Media
180 }  // namespace OHOS