1 /*
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 "link/Linkers.h"
18 
19 #include <algorithm>
20 
21 #include "ResourceTable.h"
22 
23 namespace aapt {
24 
25 namespace {
26 
27 // Visits each xml Node, removing URI references and nested namespaces.
28 class XmlVisitor : public xml::Visitor {
29  public:
XmlVisitor(bool keep_uris)30   explicit XmlVisitor(bool keep_uris) : keep_uris_(keep_uris) {}
31 
Visit(xml::Element * el)32   void Visit(xml::Element* el) override {
33     el->namespace_decls.clear();
34 
35     if (!keep_uris_) {
36       for (xml::Attribute& attr : el->attributes) {
37         attr.namespace_uri.clear();
38       }
39       el->namespace_uri.clear();
40     }
41     xml::Visitor::Visit(el);
42   }
43 
44  private:
45   DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
46 
47   bool keep_uris_;
48 };
49 
50 }  // namespace
51 
Consume(IAaptContext * context,xml::XmlResource * resource)52 bool XmlNamespaceRemover::Consume(IAaptContext* context, xml::XmlResource* resource) {
53   if (!resource->root) {
54     return false;
55   }
56 
57   XmlVisitor visitor(keep_uris_);
58   resource->root->Accept(&visitor);
59   return true;
60 }
61 
62 }  // namespace aapt
63