blob: a5b2d10fc9e065c8253825e1d667fff0e71de5ae [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 Lesinskic6284372017-12-04 13:46:23 -0800103 Attribute* FindOrCreateAttribute(const android::StringPiece& ns,
104 const android::StringPiece& name);
Colin Crossdcd58c42018-05-25 22:46:35 -0700105 void RemoveAttribute(const android::StringPiece& ns,
106 const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700107
Adam Lesinski6b372992017-08-09 10:54:23 -0700108 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700109 const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const;
110
Adam Lesinski6b372992017-08-09 10:54:23 -0700111 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
112 const android::StringPiece& attr_ns,
113 const android::StringPiece& attr_name,
114 const android::StringPiece& attr_value);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700115
116 const Element* FindChildWithAttribute(const android::StringPiece& ns,
117 const android::StringPiece& name,
118 const android::StringPiece& attr_ns,
119 const android::StringPiece& attr_name,
120 const android::StringPiece& attr_value) const;
121
Adam Lesinski6b372992017-08-09 10:54:23 -0700122 std::vector<Element*> GetChildElements();
123
124 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
125 // that knows cloning an element returns an element.
126 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
127
128 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
129
130 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700131 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700132};
133
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700134// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700135class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 public:
137 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700138
Adam Lesinski6b372992017-08-09 10:54:23 -0700139 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
140
141 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700142 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700143};
144
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700145// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700146class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 public:
148 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700149
150 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
151 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
152 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700153 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700154
Adam Lesinski6b372992017-08-09 10:54:23 -0700155 std::unique_ptr<xml::Element> root;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700156
157 std::unique_ptr<XmlResource> Clone() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800158};
159
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700160// Inflates an XML DOM from an InputStream, logging errors to the logger.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700161std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700162
Adam Lesinski8780eb62017-10-31 17:44:39 -0700163// Inflates an XML DOM from a binary ResXMLTree.
164std::unique_ptr<XmlResource> Inflate(const void* data, size_t len,
165 std::string* out_error = nullptr);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700168
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700169// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski6b372992017-08-09 10:54:23 -0700170class Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700172 virtual ~Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinski6b372992017-08-09 10:54:23 -0700174 virtual void Visit(Element* el) {
175 VisitChildren(el);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700176 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
Adam Lesinski6b372992017-08-09 10:54:23 -0700178 virtual void Visit(Text* text) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700179 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
Adam Lesinski6b372992017-08-09 10:54:23 -0700181 protected:
182 Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183
Adam Lesinski6b372992017-08-09 10:54:23 -0700184 void VisitChildren(Element* el) {
185 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700189
190 virtual void BeforeVisitElement(Element* el) {
191 }
192 virtual void AfterVisitElement(Element* el) {
193 }
194
195 private:
196 DISALLOW_COPY_AND_ASSIGN(Visitor);
197
198 friend class Element;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199};
200
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700201class ConstVisitor {
202 public:
203 virtual ~ConstVisitor() = default;
204
205 virtual void Visit(const Element* el) {
206 VisitChildren(el);
207 }
208
209 virtual void Visit(const Text* text) {
210 }
211
212 protected:
213 ConstVisitor() = default;
214
215 void VisitChildren(const Element* el) {
216 for (const auto& child : el->children) {
217 child->Accept(this);
218 }
219 }
220
221 virtual void BeforeVisitElement(const Element* el) {
222 }
223
224 virtual void AfterVisitElement(const Element* el) {
225 }
226
227 private:
228 DISALLOW_COPY_AND_ASSIGN(ConstVisitor);
229
230 friend class Element;
231};
232
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700233// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700237
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700238 Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700239
Adam Lesinski6b372992017-08-09 10:54:23 -0700240 protected:
241 PackageAwareVisitor() = default;
242
243 void BeforeVisitElement(Element* el) override;
244 void AfterVisitElement(Element* el) override;
245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700247 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 struct PackageDecl {
250 std::string prefix;
251 ExtractedPackage package;
252 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253
Adam Lesinski6b372992017-08-09 10:54:23 -0700254 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700255};
256
Adam Lesinski6b372992017-08-09 10:54:23 -0700257namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700258
Adam Lesinski6b372992017-08-09 10:54:23 -0700259// Base class that overrides the default behaviour and does not descend into child nodes.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700260class NodeCastBase : public ConstVisitor {
Adam Lesinski6b372992017-08-09 10:54:23 -0700261 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700262 void Visit(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700263 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700264 void Visit(const Text* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700265 }
266
267 protected:
268 NodeCastBase() = default;
269
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700270 void BeforeVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700271 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700272 void AfterVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700273 }
274
275 private:
276 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
277};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700278
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700280class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700282 using NodeCastBase::Visit;
283
284 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700286 const T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700288 void Visit(const T* v) override {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700289 value = v;
290 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700291
292 private:
293 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700294};
295
Adam Lesinski6b372992017-08-09 10:54:23 -0700296} // namespace internal
297
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298template <typename T>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700299const T* NodeCast(const Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700300 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700303}
304
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700305template <typename T>
306T* NodeCast(Node* node) {
307 return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node)));
308}
309
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310} // namespace xml
311} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700312
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313#endif // AAPT_XML_DOM_H