blob: 6e8d80d04e745f70eb8039e18d02203605e8d906 [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
17#include "ResourceTable.h"
18#include "link/Linkers.h"
19
20#include <algorithm>
21
22namespace aapt {
23
24namespace {
25
26/**
27 * Visits each xml Node, removing URI references and nested namespaces.
28 */
29class XmlVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 public:
31 XmlVisitor(bool keepUris) : mKeepUris(keepUris) {}
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070032
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 void visit(xml::Element* el) override {
34 // Strip namespaces
35 for (auto& child : el->children) {
36 while (child && xml::nodeCast<xml::Namespace>(child.get())) {
37 if (child->children.empty()) {
38 child = {};
39 } else {
40 child = std::move(child->children.front());
41 child->parent = el;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070042 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070044 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 el->children.erase(
46 std::remove_if(el->children.begin(), el->children.end(),
47 [](const std::unique_ptr<xml::Node>& child) -> bool {
48 return child == nullptr;
49 }),
50 el->children.end());
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 if (!mKeepUris) {
53 for (xml::Attribute& attr : el->attributes) {
54 attr.namespaceUri = std::string();
55 }
56 el->namespaceUri = std::string();
57 }
58 xml::Visitor::visit(el);
59 }
60
61 private:
62 bool mKeepUris;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070063};
64
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065} // namespace
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067bool XmlNamespaceRemover::consume(IAaptContext* context,
68 xml::XmlResource* resource) {
69 if (!resource->root) {
70 return false;
71 }
72 // Replace any root namespaces until the root is a non-namespace node
73 while (xml::nodeCast<xml::Namespace>(resource->root.get())) {
74 if (resource->root->children.empty()) {
75 break;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070076 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 resource->root = std::move(resource->root->children.front());
78 resource->root->parent = nullptr;
79 }
80 XmlVisitor visitor(mKeepUris);
81 resource->root->accept(&visitor);
82 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070083}
84
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085} // namespace aapt