blob: cf06ba5cc50edf54bfc592ab4cd21595dd644d74 [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 Lesinskid3ffa8442017-09-28 13:34:35 -070038class ConstVisitor;
Adam Lesinskic744ae82017-05-17 19:28:38 -070039
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070040// Base class for all XML nodes.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070041class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080044
Adam Lesinski6b372992017-08-09 10:54:23 -070045 Element* parent = nullptr;
46 size_t line_number = 0u;
47 size_t column_number = 0u;
48 std::string comment;
49
50 virtual void Accept(Visitor* visitor) = 0;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070051 virtual void Accept(ConstVisitor* visitor) const = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -070052
53 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
54
55 // Clones the Node subtree, using the given function to decide how to clone an Element.
Adam Lesinski6b372992017-08-09 10:54:23 -070056 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070057};
58
Adam Lesinski6b372992017-08-09 10:54:23 -070059// A namespace declaration (xmlns:prefix="uri").
60struct NamespaceDecl {
61 std::string prefix;
62 std::string uri;
63 size_t line_number = 0u;
64 size_t column_number = 0u;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065};
Adam Lesinski75f3a552015-06-03 14:54:23 -070066
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067struct AaptAttribute {
Adam Lesinskic744ae82017-05-17 19:28:38 -070068 explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
69 : attribute(attr), id(resid) {
70 }
71
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 aapt::Attribute attribute;
Adam Lesinskic744ae82017-05-17 19:28:38 -070073 Maybe<ResourceId> id;
Adam Lesinski75f3a552015-06-03 14:54:23 -070074};
75
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076// An XML attribute.
Adam Lesinski75f3a552015-06-03 14:54:23 -070077struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 std::string name;
80 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Maybe<AaptAttribute> compiled_attribute;
83 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070084};
85
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070086// An Element XML node.
Adam Lesinski6b372992017-08-09 10:54:23 -070087class Element : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 public:
Adam Lesinski6b372992017-08-09 10:54:23 -070089 // Ordered namespace prefix declarations.
90 std::vector<NamespaceDecl> namespace_decls;
91
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::string name;
94 std::vector<Attribute> attributes;
Adam Lesinski6b372992017-08-09 10:54:23 -070095 std::vector<std::unique_ptr<Node>> children;
96
97 void AppendChild(std::unique_ptr<Node> child);
98 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinski75f3a552015-06-03 14:54:23 -070099
Adam Lesinskid5083f62017-01-16 15:07:21 -0800100 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700101 const Attribute* FindAttribute(const android::StringPiece& ns,
102 const android::StringPiece& name) const;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700103
Adam Lesinski6b372992017-08-09 10:54:23 -0700104 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700105 const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const;
106
Adam Lesinski6b372992017-08-09 10:54:23 -0700107 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
108 const android::StringPiece& attr_ns,
109 const android::StringPiece& attr_name,
110 const android::StringPiece& attr_value);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700111
112 const Element* FindChildWithAttribute(const android::StringPiece& ns,
113 const android::StringPiece& name,
114 const android::StringPiece& attr_ns,
115 const android::StringPiece& attr_name,
116 const android::StringPiece& attr_value) const;
117
Adam Lesinski6b372992017-08-09 10:54:23 -0700118 std::vector<Element*> GetChildElements();
119
120 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
121 // that knows cloning an element returns an element.
122 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
123
124 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
125
126 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700127 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700128};
129
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700130// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700131class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 public:
133 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700134
Adam Lesinski6b372992017-08-09 10:54:23 -0700135 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
136
137 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700138 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700139};
140
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700141// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700142class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 public:
144 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700145
146 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
147 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
148 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700149 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700150
Adam Lesinski6b372992017-08-09 10:54:23 -0700151 std::unique_ptr<xml::Element> root;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700152
153 std::unique_ptr<XmlResource> Clone() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800154};
155
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700156// Inflates an XML DOM from an InputStream, logging errors to the logger.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700157std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700158
Adam Lesinski8780eb62017-10-31 17:44:39 -0700159// Inflates an XML DOM from a binary ResXMLTree.
160std::unique_ptr<XmlResource> Inflate(const void* data, size_t len,
161 std::string* out_error = nullptr);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700164
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700165// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski6b372992017-08-09 10:54:23 -0700166class Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700168 virtual ~Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 virtual void Visit(Element* el) {
171 VisitChildren(el);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700172 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinski6b372992017-08-09 10:54:23 -0700174 virtual void Visit(Text* text) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700175 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
Adam Lesinski6b372992017-08-09 10:54:23 -0700177 protected:
178 Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179
Adam Lesinski6b372992017-08-09 10:54:23 -0700180 void VisitChildren(Element* el) {
181 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700185
186 virtual void BeforeVisitElement(Element* el) {
187 }
188 virtual void AfterVisitElement(Element* el) {
189 }
190
191 private:
192 DISALLOW_COPY_AND_ASSIGN(Visitor);
193
194 friend class Element;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195};
196
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700197class ConstVisitor {
198 public:
199 virtual ~ConstVisitor() = default;
200
201 virtual void Visit(const Element* el) {
202 VisitChildren(el);
203 }
204
205 virtual void Visit(const Text* text) {
206 }
207
208 protected:
209 ConstVisitor() = default;
210
211 void VisitChildren(const Element* el) {
212 for (const auto& child : el->children) {
213 child->Accept(this);
214 }
215 }
216
217 virtual void BeforeVisitElement(const Element* el) {
218 }
219
220 virtual void AfterVisitElement(const Element* el) {
221 }
222
223 private:
224 DISALLOW_COPY_AND_ASSIGN(ConstVisitor);
225
226 friend class Element;
227};
228
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700229// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700233
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700234 Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700235
Adam Lesinski6b372992017-08-09 10:54:23 -0700236 protected:
237 PackageAwareVisitor() = default;
238
239 void BeforeVisitElement(Element* el) override;
240 void AfterVisitElement(Element* el) override;
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700243 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 struct PackageDecl {
246 std::string prefix;
247 ExtractedPackage package;
248 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249
Adam Lesinski6b372992017-08-09 10:54:23 -0700250 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700251};
252
Adam Lesinski6b372992017-08-09 10:54:23 -0700253namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700254
Adam Lesinski6b372992017-08-09 10:54:23 -0700255// Base class that overrides the default behaviour and does not descend into child nodes.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700256class NodeCastBase : public ConstVisitor {
Adam Lesinski6b372992017-08-09 10:54:23 -0700257 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700258 void Visit(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700259 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700260 void Visit(const Text* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700261 }
262
263 protected:
264 NodeCastBase() = default;
265
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700266 void BeforeVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700267 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700268 void AfterVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700269 }
270
271 private:
272 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
273};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700274
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700275template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700276class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700278 using NodeCastBase::Visit;
279
280 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700282 const T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700284 void Visit(const T* v) override {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700285 value = v;
286 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700287
288 private:
289 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700290};
291
Adam Lesinski6b372992017-08-09 10:54:23 -0700292} // namespace internal
293
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700294template <typename T>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700295const T* NodeCast(const Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700296 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700299}
300
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700301template <typename T>
302T* NodeCast(Node* node) {
303 return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node)));
304}
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306} // namespace xml
307} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700308
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309#endif // AAPT_XML_DOM_H