blob: 1d784600459d8479dbeba76232472e0ea38c3664 [file] [log] [blame]
Ryan Mitchellcd965a32019-09-18 14:52:45 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "idmap2/XmlParser.h"
18
Ryan Mitchellcd965a32019-09-18 14:52:45 -070019#include <memory>
20#include <string>
21#include <utility>
22
23namespace android::idmap2 {
24
25template <typename T>
26ResXMLParser::ResXMLPosition get_tree_position(const T& tree) {
27 ResXMLParser::ResXMLPosition pos{};
28 tree.getPosition(&pos);
29 return pos;
30}
31
32XmlParser::Node::Node(const ResXMLTree& tree) : Node(tree, get_tree_position(tree)) {
33}
34XmlParser::Node::Node(const ResXMLTree& tree, const ResXMLParser::ResXMLPosition& pos)
35 : parser_(tree) {
36 set_position(pos);
37}
38
39bool XmlParser::Node::operator==(const XmlParser::Node& rhs) const {
40 ResXMLParser::ResXMLPosition pos = get_position();
41 ResXMLParser::ResXMLPosition rhs_pos = rhs.get_position();
42 return pos.curExt == rhs_pos.curExt && pos.curNode == rhs_pos.curNode &&
43 pos.eventCode == rhs_pos.eventCode;
44}
45
46bool XmlParser::Node::operator!=(const XmlParser::Node& rhs) const {
47 return !(*this == rhs);
48}
49
50ResXMLParser::ResXMLPosition XmlParser::Node::get_position() const {
51 return get_tree_position(parser_);
52}
53
54void XmlParser::Node::set_position(const ResXMLParser::ResXMLPosition& pos) {
55 parser_.setPosition(pos);
56}
57
58bool XmlParser::Node::Seek(bool inner_child) {
59 if (parser_.getEventType() == XmlParser::Event::END_TAG) {
60 return false;
61 }
62
63 ssize_t depth = 0;
64 XmlParser::Event code;
65 while ((code = parser_.next()) != XmlParser::Event::BAD_DOCUMENT &&
66 code != XmlParser::Event::END_DOCUMENT) {
67 if (code == XmlParser::Event::START_TAG) {
68 if (++depth == (inner_child ? 1 : 0)) {
69 return true;
70 }
71 } else if (code == XmlParser::Event::END_TAG) {
72 if (--depth == (inner_child ? -1 : -2)) {
73 return false;
74 }
75 }
76 }
77
78 return false;
79}
80
81XmlParser::Event XmlParser::Node::event() const {
82 return parser_.getEventType();
83}
84
85std::string XmlParser::Node::name() const {
86 size_t len;
87 const String16 key16(parser_.getElementName(&len));
88 return String8(key16).c_str();
89}
90
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -080091template <typename Func>
92Result<Res_value> FindAttribute(const ResXMLParser& parser, const std::string& label,
93 Func&& predicate) {
94 for (size_t i = 0; i < parser.getAttributeCount(); i++) {
95 if (!predicate(i)) {
96 continue;
97 }
98 Res_value res_value{};
99 if (parser.getAttributeValue(i, &res_value) == BAD_TYPE) {
100 return Error(R"(Bad value for attribute "%s")", label.c_str());
101 }
102 return res_value;
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700103 }
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800104 return Error(R"(Failed to find attribute "%s")", label.c_str());
105}
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700106
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800107Result<std::string> GetStringValue(const ResXMLParser& parser, const Res_value& value,
108 const std::string& label) {
109 switch (value.dataType) {
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700110 case Res_value::TYPE_STRING: {
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800111 if (auto str = parser.getStrings().string8ObjectAt(value.data); str.ok()) {
Tomasz Wasilczykade06312023-08-10 23:54:44 +0000112 return std::string(str->c_str());
Ryan Mitchell80094e32020-11-16 23:08:18 +0000113 }
114 break;
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700115 }
116 case Res_value::TYPE_INT_DEC:
117 case Res_value::TYPE_INT_HEX:
118 case Res_value::TYPE_INT_BOOLEAN: {
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800119 return std::to_string(value.data);
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700120 }
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700121 }
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800122 return Error(R"(Failed to convert attribute "%s" value to a string)", label.c_str());
123}
Ryan Mitchell80094e32020-11-16 23:08:18 +0000124
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800125Result<Res_value> XmlParser::Node::GetAttributeValue(ResourceId attr,
126 const std::string& label) const {
127 return FindAttribute(parser_, label, [&](size_t index) -> bool {
128 return parser_.getAttributeNameResID(index) == attr;
129 });
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700130}
131
132Result<Res_value> XmlParser::Node::GetAttributeValue(const std::string& name) const {
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800133 return FindAttribute(parser_, name, [&](size_t index) -> bool {
134 size_t len;
135 const String16 key16(parser_.getAttributeName(index, &len));
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700136 std::string key = String8(key16).c_str();
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800137 return key == name;
138 });
139}
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700140
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800141Result<std::string> XmlParser::Node::GetAttributeStringValue(ResourceId attr,
142 const std::string& label) const {
143 auto value = GetAttributeValue(attr, label);
144 return value ? GetStringValue(parser_, *value, label) : value.GetError();
145}
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700146
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -0800147Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const {
148 auto value = GetAttributeValue(name);
149 return value ? GetStringValue(parser_, *value, name) : value.GetError();
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700150}
151
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800152XmlParser::XmlParser(std::unique_ptr<ResXMLTree> tree) : tree_(std::move(tree)) {
153}
154
155Result<XmlParser> XmlParser::Create(const void* data, size_t size, bool copy_data) {
156 auto tree = std::make_unique<ResXMLTree>();
157 if (tree->setTo(data, size, copy_data) != NO_ERROR) {
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700158 return Error("Malformed xml block");
159 }
160
161 // Find the beginning of the first tag.
162 XmlParser::Event event;
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800163 while ((event = tree->next()) != XmlParser::Event::BAD_DOCUMENT &&
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700164 event != XmlParser::Event::END_DOCUMENT && event != XmlParser::Event::START_TAG) {
165 }
166
167 if (event == XmlParser::Event::END_DOCUMENT) {
168 return Error("Root tag was not be found");
169 }
170
171 if (event == XmlParser::Event::BAD_DOCUMENT) {
172 return Error("Bad xml document");
173 }
174
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800175 return XmlParser{std::move(tree)};
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700176}
177
178} // namespace android::idmap2