blob: 24aa5660ae3096d6886019a79d71d5d415aae0f4 [file] [log] [blame]
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -07001/*
2 * Copyright (C) 2016 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
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070017#include "link/Linkers.h"
18
19#include <algorithm>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "ResourceTable.h"
22
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070023namespace aapt {
24
25namespace {
26
27/**
28 * Visits each xml Node, removing URI references and nested namespaces.
29 */
30class XmlVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 explicit XmlVisitor(bool keep_uris) : keep_uris_(keep_uris) {}
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 void Visit(xml::Element* el) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 // Strip namespaces
36 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 while (child && xml::NodeCast<xml::Namespace>(child.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 if (child->children.empty()) {
39 child = {};
40 } else {
41 child = std::move(child->children.front());
42 child->parent = el;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070043 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070045 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 el->children.erase(
47 std::remove_if(el->children.begin(), el->children.end(),
48 [](const std::unique_ptr<xml::Node>& child) -> bool {
49 return child == nullptr;
50 }),
51 el->children.end());
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 if (!keep_uris_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 for (xml::Attribute& attr : el->attributes) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 attr.namespace_uri = std::string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 el->namespace_uri = std::string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 xml::Visitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 }
61
62 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
64
65 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070066};
67
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068} // namespace
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070bool XmlNamespaceRemover::Consume(IAaptContext* context,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 xml::XmlResource* resource) {
72 if (!resource->root) {
73 return false;
74 }
75 // Replace any root namespaces until the root is a non-namespace node
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 while (xml::NodeCast<xml::Namespace>(resource->root.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 if (resource->root->children.empty()) {
78 break;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070079 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 resource->root = std::move(resource->root->children.front());
81 resource->root->parent = nullptr;
82 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 XmlVisitor visitor(keep_uris_);
84 resource->root->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070086}
87
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088} // namespace aapt