blob: 4a278f632a4ae28cbb471c4e1fdad7f532f212a3 [file] [log] [blame]
Adam Lesinski75f3a552015-06-03 14:54:23 -07001/*
2 * Copyright (C) 2015 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
Adam Lesinski75f3a552015-06-03 14:54:23 -070017#include "XmlDom.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <expat.h>
Adam Lesinski75f3a552015-06-03 14:54:23 -070020
Adam Lesinski75f3a552015-06-03 14:54:23 -070021#include <memory>
22#include <stack>
23#include <string>
24#include <tuple>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "android-base/logging.h"
27
Adam Lesinskid0f492d2017-04-03 18:12:45 -070028#include "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "XmlPullParser.h"
30#include "util/Util.h"
31
Adam Lesinskid5083f62017-01-16 15:07:21 -080032using android::StringPiece;
33using android::StringPiece16;
34
Adam Lesinski75f3a552015-06-03 14:54:23 -070035namespace aapt {
36namespace xml {
37
38constexpr char kXmlNamespaceSep = 1;
39
40struct Stack {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 std::unique_ptr<xml::Node> root;
42 std::stack<xml::Node*> node_stack;
43 std::string pending_comment;
Adam Lesinskiac6edc52017-03-02 19:31:28 -080044 std::unique_ptr<xml::Text> last_text_node;
45 util::StringBuilder pending_text;
Adam Lesinski75f3a552015-06-03 14:54:23 -070046};
47
48/**
49 * Extracts the namespace and name of an expanded element or attribute name.
50 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051static void SplitName(const char* name, std::string* out_ns,
52 std::string* out_name) {
53 const char* p = name;
54 while (*p != 0 && *p != kXmlNamespaceSep) {
55 p++;
56 }
Adam Lesinski75f3a552015-06-03 14:54:23 -070057
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 if (*p == 0) {
59 out_ns->clear();
Adam Lesinskid5083f62017-01-16 15:07:21 -080060 out_name->assign(name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 } else {
Adam Lesinskid5083f62017-01-16 15:07:21 -080062 out_ns->assign(name, (p - name));
63 out_name->assign(p + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 }
Adam Lesinski75f3a552015-06-03 14:54:23 -070065}
66
Adam Lesinskiac6edc52017-03-02 19:31:28 -080067static void FinishPendingText(Stack* stack) {
68 if (stack->last_text_node != nullptr) {
69 if (!stack->pending_text.IsEmpty()) {
70 stack->last_text_node->text = stack->pending_text.ToString();
71 stack->pending_text = {};
72 stack->node_stack.top()->AppendChild(std::move(stack->last_text_node));
73 } else {
74 // Drop an empty text node.
75 stack->last_text_node = nullptr;
76 }
77 }
78}
79
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080static void AddToStack(Stack* stack, XML_Parser parser,
81 std::unique_ptr<Node> node) {
82 node->line_number = XML_GetCurrentLineNumber(parser);
83 node->column_number = XML_GetCurrentColumnNumber(parser);
Adam Lesinski75f3a552015-06-03 14:54:23 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 Node* this_node = node.get();
86 if (!stack->node_stack.empty()) {
Adam Lesinskie343eb12016-10-27 16:31:58 -070087 stack->node_stack.top()->AppendChild(std::move(node));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 } else {
89 stack->root = std::move(node);
90 }
Adam Lesinski75f3a552015-06-03 14:54:23 -070091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (!NodeCast<Text>(this_node)) {
93 stack->node_stack.push(this_node);
94 }
Adam Lesinski75f3a552015-06-03 14:54:23 -070095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
98 const char* uri) {
99 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
100 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800101 FinishPendingText(stack);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 std::unique_ptr<Namespace> ns = util::make_unique<Namespace>();
104 if (prefix) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800105 ns->namespace_prefix = prefix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 if (uri) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 ns->namespace_uri = uri;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 AddToStack(stack, parser, std::move(ns));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700113}
114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix) {
116 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
117 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800118 FinishPendingText(stack);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 CHECK(!stack->node_stack.empty());
121 stack->node_stack.pop();
Adam Lesinski75f3a552015-06-03 14:54:23 -0700122}
123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124static bool less_attribute(const Attribute& lhs, const Attribute& rhs) {
125 return std::tie(lhs.namespace_uri, lhs.name, lhs.value) <
126 std::tie(rhs.namespace_uri, rhs.name, rhs.value);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700127}
128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129static void XMLCALL StartElementHandler(void* user_data, const char* name,
130 const char** attrs) {
131 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
132 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800133 FinishPendingText(stack);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 std::unique_ptr<Element> el = util::make_unique<Element>();
136 SplitName(name, &el->namespace_uri, &el->name);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 while (*attrs) {
139 Attribute attribute;
140 SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800141 util::StringBuilder builder;
142 builder.Append(*attrs++);
143 attribute.value = builder.ToString();
Adam Lesinski75f3a552015-06-03 14:54:23 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 // Insert in sorted order.
146 auto iter = std::lower_bound(el->attributes.begin(), el->attributes.end(),
147 attribute, less_attribute);
148 el->attributes.insert(iter, std::move(attribute));
149 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 el->comment = std::move(stack->pending_comment);
152 AddToStack(stack, parser, std::move(el));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700153}
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155static void XMLCALL EndElementHandler(void* user_data, const char* name) {
156 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
157 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800158 FinishPendingText(stack);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 CHECK(!stack->node_stack.empty());
161 // stack->nodeStack.top()->comment = std::move(stack->pendingComment);
162 stack->node_stack.pop();
Adam Lesinski75f3a552015-06-03 14:54:23 -0700163}
164
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800165static void XMLCALL CharacterDataHandler(void* user_data, const char* s, int len) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
167 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700168
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800169 const StringPiece str(s, len);
170 if (str.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 return;
172 }
173
174 // See if we can just append the text to a previous text node.
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800175 if (stack->last_text_node != nullptr) {
176 stack->pending_text.Append(str);
177 return;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700179
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800180 stack->last_text_node = util::make_unique<Text>();
181 stack->last_text_node->line_number = XML_GetCurrentLineNumber(parser);
182 stack->last_text_node->column_number = XML_GetCurrentColumnNumber(parser);
183 stack->pending_text.Append(str);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700184}
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186static void XMLCALL CommentDataHandler(void* user_data, const char* comment) {
187 XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
188 Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800189 FinishPendingText(stack);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 if (!stack->pending_comment.empty()) {
192 stack->pending_comment += '\n';
193 }
194 stack->pending_comment += comment;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700195}
196
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700197std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 Stack stack;
Adam Lesinski803c7c82016-04-06 16:09:43 -0700199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 XML_Parser parser = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
201 XML_SetUserData(parser, &stack);
202 XML_UseParserAsHandlerArg(parser);
203 XML_SetElementHandler(parser, StartElementHandler, EndElementHandler);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204 XML_SetNamespaceDeclHandler(parser, StartNamespaceHandler, EndNamespaceHandler);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 XML_SetCharacterDataHandler(parser, CharacterDataHandler);
206 XML_SetCommentHandler(parser, CommentDataHandler);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 char buffer[1024];
209 while (!in->eof()) {
210 in->read(buffer, sizeof(buffer) / sizeof(buffer[0]));
211 if (in->bad() && !in->eof()) {
212 stack.root = {};
213 diag->Error(DiagMessage(source) << strerror(errno));
214 break;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700215 }
216
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700217 if (XML_Parse(parser, buffer, in->gcount(), in->eof()) == XML_STATUS_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 stack.root = {};
219 diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser)))
220 << XML_ErrorString(XML_GetErrorCode(parser)));
221 break;
222 }
223 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 XML_ParserFree(parser);
226 if (stack.root) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700227 return util::make_unique<XmlResource>(ResourceFile{{}, {}, source}, std::move(stack.root));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 }
229 return {};
230}
Adam Lesinski75f3a552015-06-03 14:54:23 -0700231
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700232static void CopyAttributes(Element* el, android::ResXMLParser* parser, StringPool* out_pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 const size_t attr_count = parser->getAttributeCount();
234 if (attr_count > 0) {
235 el->attributes.reserve(attr_count);
236 for (size_t i = 0; i < attr_count; i++) {
237 Attribute attr;
238 size_t len;
239 const char16_t* str16 = parser->getAttributeNamespace(i, &len);
240 if (str16) {
241 attr.namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
242 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 str16 = parser->getAttributeName(i, &len);
245 if (str16) {
246 attr.name = util::Utf16ToUtf8(StringPiece16(str16, len));
247 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 str16 = parser->getAttributeStringValue(i, &len);
250 if (str16) {
251 attr.value = util::Utf16ToUtf8(StringPiece16(str16, len));
252 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700253
254 android::Res_value res_value;
255 if (parser->getAttributeValue(i, &res_value) > 0) {
256 attr.compiled_value = ResourceUtils::ParseBinaryResValue(
257 ResourceType::kAnim, {}, parser->getStrings(), res_value, out_pool);
258 }
259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 el->attributes.push_back(std::move(attr));
261 }
262 }
263}
Adam Lesinski75f3a552015-06-03 14:54:23 -0700264
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700265std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
266 const Source& source) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 // We import the android namespace because on Windows NO_ERROR is a macro, not
268 // an enum, which
269 // causes errors when qualifying it with android::
270 using namespace android;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700271
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700272 StringPool string_pool;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 std::unique_ptr<Node> root;
274 std::stack<Node*> node_stack;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 ResXMLTree tree;
277 if (tree.setTo(data, data_len) != NO_ERROR) {
278 return {};
279 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 ResXMLParser::event_code_t code;
282 while ((code = tree.next()) != ResXMLParser::BAD_DOCUMENT &&
283 code != ResXMLParser::END_DOCUMENT) {
284 std::unique_ptr<Node> new_node;
285 switch (code) {
286 case ResXMLParser::START_NAMESPACE: {
287 std::unique_ptr<Namespace> node = util::make_unique<Namespace>();
288 size_t len;
289 const char16_t* str16 = tree.getNamespacePrefix(&len);
290 if (str16) {
291 node->namespace_prefix = util::Utf16ToUtf8(StringPiece16(str16, len));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700292 }
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 str16 = tree.getNamespaceUri(&len);
295 if (str16) {
296 node->namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700297 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 new_node = std::move(node);
299 break;
300 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 case ResXMLParser::START_TAG: {
303 std::unique_ptr<Element> node = util::make_unique<Element>();
304 size_t len;
305 const char16_t* str16 = tree.getElementNamespace(&len);
306 if (str16) {
307 node->namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700308 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 str16 = tree.getElementName(&len);
311 if (str16) {
312 node->name = util::Utf16ToUtf8(StringPiece16(str16, len));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700313 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700315 CopyAttributes(node.get(), &tree, &string_pool);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316
317 new_node = std::move(node);
318 break;
319 }
320
321 case ResXMLParser::TEXT: {
322 std::unique_ptr<Text> node = util::make_unique<Text>();
323 size_t len;
324 const char16_t* str16 = tree.getText(&len);
325 if (str16) {
326 node->text = util::Utf16ToUtf8(StringPiece16(str16, len));
327 }
328 new_node = std::move(node);
329 break;
330 }
331
332 case ResXMLParser::END_NAMESPACE:
333 case ResXMLParser::END_TAG:
334 CHECK(!node_stack.empty());
335 node_stack.pop();
336 break;
337
338 default:
339 LOG(FATAL) << "unhandled XML chunk type";
340 break;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700341 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342
343 if (new_node) {
344 new_node->line_number = tree.getLineNumber();
345
346 Node* this_node = new_node.get();
347 if (!root) {
348 CHECK(node_stack.empty()) << "node stack should be empty";
349 root = std::move(new_node);
350 } else {
351 CHECK(!node_stack.empty()) << "node stack should not be empty";
Adam Lesinskie343eb12016-10-27 16:31:58 -0700352 node_stack.top()->AppendChild(std::move(new_node));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 }
354
355 if (!NodeCast<Text>(this_node)) {
356 node_stack.push(this_node);
357 }
358 }
359 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700360 return util::make_unique<XmlResource>(ResourceFile{}, std::move(root), std::move(string_pool));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361}
362
363std::unique_ptr<Node> Namespace::Clone() {
364 auto ns = util::make_unique<Namespace>();
365 ns->comment = comment;
366 ns->line_number = line_number;
367 ns->column_number = column_number;
368 ns->namespace_prefix = namespace_prefix;
369 ns->namespace_uri = namespace_uri;
370
371 ns->children.reserve(children.size());
372 for (const std::unique_ptr<xml::Node>& child : children) {
Adam Lesinskie343eb12016-10-27 16:31:58 -0700373 ns->AppendChild(child->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 }
375 return std::move(ns);
376}
377
378Element* FindRootElement(XmlResource* doc) {
379 return FindRootElement(doc->root.get());
380}
381
382Element* FindRootElement(Node* node) {
383 if (!node) {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700384 return nullptr;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 Element* el = nullptr;
388 while ((el = NodeCast<Element>(node)) == nullptr) {
389 if (node->children.empty()) {
390 return nullptr;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700391 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 // We are looking for the first element, and namespaces can only have one
393 // child.
394 node = node->children.front().get();
395 }
396 return el;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700397}
398
Adam Lesinskie343eb12016-10-27 16:31:58 -0700399void Node::AppendChild(std::unique_ptr<Node> child) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 child->parent = this;
401 children.push_back(std::move(child));
402}
Adam Lesinski75f3a552015-06-03 14:54:23 -0700403
Adam Lesinskie343eb12016-10-27 16:31:58 -0700404void Node::InsertChild(size_t index, std::unique_ptr<Node> child) {
405 child->parent = this;
406 children.insert(children.begin() + index, std::move(child));
407}
408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409Attribute* Element::FindAttribute(const StringPiece& ns,
410 const StringPiece& name) {
411 for (auto& attr : attributes) {
412 if (ns == attr.namespace_uri && name == attr.name) {
413 return &attr;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700414 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 }
416 return nullptr;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700417}
418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) {
420 return FindChildWithAttribute(ns, name, {}, {}, {});
421}
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423Element* Element::FindChildWithAttribute(const StringPiece& ns,
424 const StringPiece& name,
425 const StringPiece& attr_ns,
426 const StringPiece& attr_name,
427 const StringPiece& attr_value) {
428 for (auto& child_node : children) {
429 Node* child = child_node.get();
430 while (NodeCast<Namespace>(child)) {
431 if (child->children.empty()) {
432 break;
433 }
434 child = child->children[0].get();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700435 }
436
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 if (Element* el = NodeCast<Element>(child)) {
438 if (ns == el->namespace_uri && name == el->name) {
439 if (attr_ns.empty() && attr_name.empty()) {
440 return el;
441 }
442
443 Attribute* attr = el->FindAttribute(attr_ns, attr_name);
444 if (attr && attr_value == attr->value) {
445 return el;
446 }
447 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700448 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 }
450 return nullptr;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700451}
452
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453std::vector<Element*> Element::GetChildElements() {
454 std::vector<Element*> elements;
455 for (auto& child_node : children) {
456 Node* child = child_node.get();
457 while (NodeCast<Namespace>(child)) {
458 if (child->children.empty()) {
459 break;
460 }
461 child = child->children[0].get();
462 }
463
464 if (Element* el = NodeCast<Element>(child)) {
465 elements.push_back(el);
466 }
467 }
468 return elements;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700469}
470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471std::unique_ptr<Node> Element::Clone() {
472 auto el = util::make_unique<Element>();
473 el->comment = comment;
474 el->line_number = line_number;
475 el->column_number = column_number;
476 el->name = name;
477 el->namespace_uri = namespace_uri;
Adam Lesinski467f1712015-11-16 17:35:44 -0800478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 el->attributes.reserve(attributes.size());
480 for (xml::Attribute& attr : attributes) {
481 // Don't copy compiled values or attributes.
482 el->attributes.push_back(
483 xml::Attribute{attr.namespace_uri, attr.name, attr.value});
484 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 el->children.reserve(children.size());
487 for (const std::unique_ptr<xml::Node>& child : children) {
Adam Lesinskie343eb12016-10-27 16:31:58 -0700488 el->AppendChild(child->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 }
490 return std::move(el);
Adam Lesinski467f1712015-11-16 17:35:44 -0800491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493std::unique_ptr<Node> Text::Clone() {
494 auto t = util::make_unique<Text>();
495 t->comment = comment;
496 t->line_number = line_number;
497 t->column_number = column_number;
498 t->text = text;
499 return std::move(t);
Adam Lesinski467f1712015-11-16 17:35:44 -0800500}
501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502void PackageAwareVisitor::Visit(Namespace* ns) {
503 bool added = false;
504 if (Maybe<ExtractedPackage> maybe_package =
505 ExtractPackageFromNamespace(ns->namespace_uri)) {
506 ExtractedPackage& package = maybe_package.value();
507 package_decls_.push_back(
508 PackageDecl{ns->namespace_prefix, std::move(package)});
509 added = true;
510 }
511
512 Visitor::Visit(ns);
513
514 if (added) {
515 package_decls_.pop_back();
516 }
517}
518
519Maybe<ExtractedPackage> PackageAwareVisitor::TransformPackageAlias(
520 const StringPiece& alias, const StringPiece& local_package) const {
521 if (alias.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800522 return ExtractedPackage{local_package.to_string(), false /* private */};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 }
524
525 const auto rend = package_decls_.rend();
526 for (auto iter = package_decls_.rbegin(); iter != rend; ++iter) {
527 if (alias == iter->prefix) {
528 if (iter->package.package.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800529 return ExtractedPackage{local_package.to_string(), iter->package.private_namespace};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 }
531 return iter->package;
532 }
533 }
534 return {};
535}
536
537} // namespace xml
538} // namespace aapt