blob: 1542243816266085f281333e25481c2ee2d5b698 [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
17#ifndef AAPT_XML_DOM_H
18#define AAPT_XML_DOM_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <memory>
21#include <string>
22#include <vector>
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "Diagnostics.h"
27#include "Resource.h"
28#include "ResourceValues.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029#include "io/Io.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlUtil.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070032
Adam Lesinski75f3a552015-06-03 14:54:23 -070033namespace aapt {
34namespace xml {
35
Adam Lesinskic744ae82017-05-17 19:28:38 -070036class Element;
Adam Lesinski6b372992017-08-09 10:54:23 -070037class Visitor;
Adam Lesinskic744ae82017-05-17 19:28:38 -070038
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070039// Base class for all XML nodes.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070040class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080043
Adam Lesinski6b372992017-08-09 10:54:23 -070044 Element* parent = nullptr;
45 size_t line_number = 0u;
46 size_t column_number = 0u;
47 std::string comment;
48
49 virtual void Accept(Visitor* visitor) = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -070050
51 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
52
53 // Clones the Node subtree, using the given function to decide how to clone an Element.
Adam Lesinski6b372992017-08-09 10:54:23 -070054 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070055};
56
Adam Lesinski6b372992017-08-09 10:54:23 -070057// A namespace declaration (xmlns:prefix="uri").
58struct NamespaceDecl {
59 std::string prefix;
60 std::string uri;
61 size_t line_number = 0u;
62 size_t column_number = 0u;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063};
Adam Lesinski75f3a552015-06-03 14:54:23 -070064
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065struct AaptAttribute {
Adam Lesinskic744ae82017-05-17 19:28:38 -070066 explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
67 : attribute(attr), id(resid) {
68 }
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 aapt::Attribute attribute;
Adam Lesinskic744ae82017-05-17 19:28:38 -070071 Maybe<ResourceId> id;
Adam Lesinski75f3a552015-06-03 14:54:23 -070072};
73
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070074// An XML attribute.
Adam Lesinski75f3a552015-06-03 14:54:23 -070075struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 std::string name;
78 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 Maybe<AaptAttribute> compiled_attribute;
81 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070082};
83
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070084// An Element XML node.
Adam Lesinski6b372992017-08-09 10:54:23 -070085class Element : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 public:
Adam Lesinski6b372992017-08-09 10:54:23 -070087 // Ordered namespace prefix declarations.
88 std::vector<NamespaceDecl> namespace_decls;
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 std::string name;
92 std::vector<Attribute> attributes;
Adam Lesinski6b372992017-08-09 10:54:23 -070093 std::vector<std::unique_ptr<Node>> children;
94
95 void AppendChild(std::unique_ptr<Node> child);
96 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinski75f3a552015-06-03 14:54:23 -070097
Adam Lesinskid5083f62017-01-16 15:07:21 -080098 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinskic744ae82017-05-17 19:28:38 -070099 const Attribute* FindAttribute(const android::StringPiece& ns,
100 const android::StringPiece& name) const;
Adam Lesinski6b372992017-08-09 10:54:23 -0700101 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
102 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
103 const android::StringPiece& attr_ns,
104 const android::StringPiece& attr_name,
105 const android::StringPiece& attr_value);
106 std::vector<Element*> GetChildElements();
107
108 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
109 // that knows cloning an element returns an element.
110 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
111
112 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
113
114 void Accept(Visitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700115};
116
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700117// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700118class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 public:
120 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700121
Adam Lesinski6b372992017-08-09 10:54:23 -0700122 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
123
124 void Accept(Visitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700125};
126
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700127// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700128class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 public:
130 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700131
132 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
133 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
134 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700135 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700136
Adam Lesinski6b372992017-08-09 10:54:23 -0700137 std::unique_ptr<xml::Element> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800138};
139
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700140// Inflates an XML DOM from an InputStream, logging errors to the logger.
141// Returns the root node on success, or nullptr on failure.
142std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700143
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700144// Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
145// Returns the root node on success, or nullptr on failure.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700146std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
147 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700150
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700151// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski6b372992017-08-09 10:54:23 -0700152class Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700154 virtual ~Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinski6b372992017-08-09 10:54:23 -0700156 virtual void Visit(Element* el) {
157 VisitChildren(el);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700158 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 virtual void Visit(Text* text) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700161 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinski6b372992017-08-09 10:54:23 -0700163 protected:
164 Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinski6b372992017-08-09 10:54:23 -0700166 void VisitChildren(Element* el) {
167 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700171
172 virtual void BeforeVisitElement(Element* el) {
173 }
174 virtual void AfterVisitElement(Element* el) {
175 }
176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(Visitor);
179
180 friend class Element;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181};
182
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700183// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800189 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700190
Adam Lesinski6b372992017-08-09 10:54:23 -0700191 protected:
192 PackageAwareVisitor() = default;
193
194 void BeforeVisitElement(Element* el) override;
195 void AfterVisitElement(Element* el) override;
196
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700198 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 struct PackageDecl {
201 std::string prefix;
202 ExtractedPackage package;
203 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204
Adam Lesinski6b372992017-08-09 10:54:23 -0700205 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700206};
207
Adam Lesinski6b372992017-08-09 10:54:23 -0700208namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700209
Adam Lesinski6b372992017-08-09 10:54:23 -0700210// Base class that overrides the default behaviour and does not descend into child nodes.
211class NodeCastBase : public Visitor {
212 public:
213 void Visit(Element* el) override {
214 }
215 void Visit(Text* el) override {
216 }
217
218 protected:
219 NodeCastBase() = default;
220
221 void BeforeVisitElement(Element* el) override {
222 }
223 void AfterVisitElement(Element* el) override {
224 }
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
228};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700229
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700231class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700233 using NodeCastBase::Visit;
234
235 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700239 void Visit(T* v) override {
240 value = v;
241 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245};
246
Adam Lesinski6b372992017-08-09 10:54:23 -0700247} // namespace internal
248
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250T* NodeCast(Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700251 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700254}
255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256} // namespace xml
257} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259#endif // AAPT_XML_DOM_H