blob: b5e2423d58dc10fdebe60c54ba64d5f2d3f894f2 [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
Adam Lesinski6b372992017-08-09 10:54:23 -070027// Visits each xml Node, removing URI references and nested namespaces.
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070028class XmlVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 explicit XmlVisitor(bool keep_uris) : keep_uris_(keep_uris) {}
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 void Visit(xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -070033 el->namespace_decls.clear();
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 if (!keep_uris_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski6b372992017-08-09 10:54:23 -070037 attr.namespace_uri.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 }
Adam Lesinski6b372992017-08-09 10:54:23 -070039 el->namespace_uri.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 xml::Visitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 }
43
44 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
46
47 bool keep_uris_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070048};
49
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050} // namespace
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070051
Adam Lesinski6b372992017-08-09 10:54:23 -070052bool XmlNamespaceRemover::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 if (!resource->root) {
54 return false;
55 }
Adam Lesinski6b372992017-08-09 10:54:23 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 XmlVisitor visitor(keep_uris_);
58 resource->root->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070060}
61
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062} // namespace aapt