blob: 063d7b9210c168b915f63bd56dfbd26535cae559 [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 Lesinski6b372992017-08-09 10:54:23 -0700103 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
104 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
105 const android::StringPiece& attr_ns,
106 const android::StringPiece& attr_name,
107 const android::StringPiece& attr_value);
108 std::vector<Element*> GetChildElements();
109
110 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
111 // that knows cloning an element returns an element.
112 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
113
114 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
115
116 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700117 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700118};
119
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700120// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700121class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 public:
123 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700124
Adam Lesinski6b372992017-08-09 10:54:23 -0700125 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
126
127 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700128 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700129};
130
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700131// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700132class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 public:
134 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700135
136 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
137 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
138 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700139 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700140
Adam Lesinski6b372992017-08-09 10:54:23 -0700141 std::unique_ptr<xml::Element> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800142};
143
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700144// Inflates an XML DOM from an InputStream, logging errors to the logger.
145// Returns the root node on success, or nullptr on failure.
146std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700147
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700148// Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
149// Returns the root node on success, or nullptr on failure.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700150std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
151 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700154
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700155// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski6b372992017-08-09 10:54:23 -0700156class Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700158 virtual ~Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 virtual void Visit(Element* el) {
161 VisitChildren(el);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700162 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163
Adam Lesinski6b372992017-08-09 10:54:23 -0700164 virtual void Visit(Text* text) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700165 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinski6b372992017-08-09 10:54:23 -0700167 protected:
168 Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 void VisitChildren(Element* el) {
171 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700175
176 virtual void BeforeVisitElement(Element* el) {
177 }
178 virtual void AfterVisitElement(Element* el) {
179 }
180
181 private:
182 DISALLOW_COPY_AND_ASSIGN(Visitor);
183
184 friend class Element;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185};
186
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700187class ConstVisitor {
188 public:
189 virtual ~ConstVisitor() = default;
190
191 virtual void Visit(const Element* el) {
192 VisitChildren(el);
193 }
194
195 virtual void Visit(const Text* text) {
196 }
197
198 protected:
199 ConstVisitor() = default;
200
201 void VisitChildren(const Element* el) {
202 for (const auto& child : el->children) {
203 child->Accept(this);
204 }
205 }
206
207 virtual void BeforeVisitElement(const Element* el) {
208 }
209
210 virtual void AfterVisitElement(const Element* el) {
211 }
212
213 private:
214 DISALLOW_COPY_AND_ASSIGN(ConstVisitor);
215
216 friend class Element;
217};
218
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700219// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700220class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700223
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700224 Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700225
Adam Lesinski6b372992017-08-09 10:54:23 -0700226 protected:
227 PackageAwareVisitor() = default;
228
229 void BeforeVisitElement(Element* el) override;
230 void AfterVisitElement(Element* el) override;
231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700233 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 struct PackageDecl {
236 std::string prefix;
237 ExtractedPackage package;
238 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239
Adam Lesinski6b372992017-08-09 10:54:23 -0700240 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700241};
242
Adam Lesinski6b372992017-08-09 10:54:23 -0700243namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700244
Adam Lesinski6b372992017-08-09 10:54:23 -0700245// Base class that overrides the default behaviour and does not descend into child nodes.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700246class NodeCastBase : public ConstVisitor {
Adam Lesinski6b372992017-08-09 10:54:23 -0700247 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700248 void Visit(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700249 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700250 void Visit(const Text* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700251 }
252
253 protected:
254 NodeCastBase() = default;
255
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700256 void BeforeVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700257 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700258 void AfterVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700259 }
260
261 private:
262 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
263};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700264
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700266class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700268 using NodeCastBase::Visit;
269
270 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700272 const T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700274 void Visit(const T* v) override {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700275 value = v;
276 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700277
278 private:
279 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280};
281
Adam Lesinski6b372992017-08-09 10:54:23 -0700282} // namespace internal
283
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284template <typename T>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700285const T* NodeCast(const Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700286 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700289}
290
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700291template <typename T>
292T* NodeCast(Node* node) {
293 return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node)));
294}
295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296} // namespace xml
297} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299#endif // AAPT_XML_DOM_H