blob: 7c55b64566f20b19ad0adaa67577f88ffad721fb [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
19#include <iostream>
20#include <map>
21#include <memory>
22#include <string>
23#include <utility>
24
25namespace android::idmap2 {
26
27template <typename T>
28ResXMLParser::ResXMLPosition get_tree_position(const T& tree) {
29 ResXMLParser::ResXMLPosition pos{};
30 tree.getPosition(&pos);
31 return pos;
32}
33
34XmlParser::Node::Node(const ResXMLTree& tree) : Node(tree, get_tree_position(tree)) {
35}
36XmlParser::Node::Node(const ResXMLTree& tree, const ResXMLParser::ResXMLPosition& pos)
37 : parser_(tree) {
38 set_position(pos);
39}
40
41bool XmlParser::Node::operator==(const XmlParser::Node& rhs) const {
42 ResXMLParser::ResXMLPosition pos = get_position();
43 ResXMLParser::ResXMLPosition rhs_pos = rhs.get_position();
44 return pos.curExt == rhs_pos.curExt && pos.curNode == rhs_pos.curNode &&
45 pos.eventCode == rhs_pos.eventCode;
46}
47
48bool XmlParser::Node::operator!=(const XmlParser::Node& rhs) const {
49 return !(*this == rhs);
50}
51
52ResXMLParser::ResXMLPosition XmlParser::Node::get_position() const {
53 return get_tree_position(parser_);
54}
55
56void XmlParser::Node::set_position(const ResXMLParser::ResXMLPosition& pos) {
57 parser_.setPosition(pos);
58}
59
60bool XmlParser::Node::Seek(bool inner_child) {
61 if (parser_.getEventType() == XmlParser::Event::END_TAG) {
62 return false;
63 }
64
65 ssize_t depth = 0;
66 XmlParser::Event code;
67 while ((code = parser_.next()) != XmlParser::Event::BAD_DOCUMENT &&
68 code != XmlParser::Event::END_DOCUMENT) {
69 if (code == XmlParser::Event::START_TAG) {
70 if (++depth == (inner_child ? 1 : 0)) {
71 return true;
72 }
73 } else if (code == XmlParser::Event::END_TAG) {
74 if (--depth == (inner_child ? -1 : -2)) {
75 return false;
76 }
77 }
78 }
79
80 return false;
81}
82
83XmlParser::Event XmlParser::Node::event() const {
84 return parser_.getEventType();
85}
86
87std::string XmlParser::Node::name() const {
88 size_t len;
89 const String16 key16(parser_.getElementName(&len));
90 return String8(key16).c_str();
91}
92
93Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const {
94 auto value = GetAttributeValue(name);
95 if (!value) {
96 return value.GetError();
97 }
98
99 switch ((*value).dataType) {
100 case Res_value::TYPE_STRING: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700101 if (auto str = parser_.getStrings().string8ObjectAt((*value).data)) {
102 return std::string(str->string());
103 }
104 break;
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700105 }
106 case Res_value::TYPE_INT_DEC:
107 case Res_value::TYPE_INT_HEX:
108 case Res_value::TYPE_INT_BOOLEAN: {
109 return std::to_string((*value).data);
110 }
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700111 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700112
113 return Error(R"(Failed to convert attribute "%s" value to a string)", name.c_str());
Ryan Mitchellcd965a32019-09-18 14:52:45 -0700114}
115
116Result<Res_value> XmlParser::Node::GetAttributeValue(const std::string& name) const {
117 size_t len;
118 for (size_t i = 0; i < parser_.getAttributeCount(); i++) {
119 const String16 key16(parser_.getAttributeName(i, &len));
120 std::string key = String8(key16).c_str();
121 if (key != name) {
122 continue;
123 }
124
125 Res_value res_value{};
126 if (parser_.getAttributeValue(i, &res_value) == BAD_TYPE) {
127 return Error(R"(Bad value for attribute "%s")", name.c_str());
128 }
129
130 return res_value;
131 }
132
133 return Error(R"(Failed to find attribute "%s")", name.c_str());
134}
135
136Result<std::unique_ptr<const XmlParser>> XmlParser::Create(const void* data, size_t size,
137 bool copy_data) {
138 auto parser = std::unique_ptr<const XmlParser>(new XmlParser());
139 if (parser->tree_.setTo(data, size, copy_data) != NO_ERROR) {
140 return Error("Malformed xml block");
141 }
142
143 // Find the beginning of the first tag.
144 XmlParser::Event event;
145 while ((event = parser->tree_.next()) != XmlParser::Event::BAD_DOCUMENT &&
146 event != XmlParser::Event::END_DOCUMENT && event != XmlParser::Event::START_TAG) {
147 }
148
149 if (event == XmlParser::Event::END_DOCUMENT) {
150 return Error("Root tag was not be found");
151 }
152
153 if (event == XmlParser::Event::BAD_DOCUMENT) {
154 return Error("Bad xml document");
155 }
156
157 return parser;
158}
159
160XmlParser::~XmlParser() {
161 tree_.uninit();
162}
163
164} // namespace android::idmap2