blob: 5bc55b6b68a1496f211f9429a1f4fcb8d6dd91af [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 Lesinski1ab598f2015-08-14 14:26:04 -070024#include "Resource.h"
25#include "ResourceValues.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000026#include "androidfw/IDiagnostics.h"
27#include "androidfw/StringPiece.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070028#include "io/Io.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080030#include "xml/XmlUtil.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070031
Adam Lesinski75f3a552015-06-03 14:54:23 -070032namespace aapt {
33namespace xml {
34
Adam Lesinskic744ae82017-05-17 19:28:38 -070035class Element;
Adam Lesinski6b372992017-08-09 10:54:23 -070036class Visitor;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070037class ConstVisitor;
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 Lesinskid3ffa8442017-09-28 13:34:35 -070050 virtual void Accept(ConstVisitor* visitor) const = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -070051
52 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
53
54 // Clones the Node subtree, using the given function to decide how to clone an Element.
Adam Lesinski6b372992017-08-09 10:54:23 -070055 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070056};
57
Adam Lesinski6b372992017-08-09 10:54:23 -070058// A namespace declaration (xmlns:prefix="uri").
59struct NamespaceDecl {
60 std::string prefix;
61 std::string uri;
62 size_t line_number = 0u;
63 size_t column_number = 0u;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064};
Adam Lesinski75f3a552015-06-03 14:54:23 -070065
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066struct AaptAttribute {
Ryan Mitchell4382e442021-07-14 12:53:01 -070067 explicit AaptAttribute(const ::aapt::Attribute& attr, const std::optional<ResourceId>& resid = {})
Adam Lesinskic744ae82017-05-17 19:28:38 -070068 : attribute(attr), id(resid) {
69 }
70
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 aapt::Attribute attribute;
Ryan Mitchell4382e442021-07-14 12:53:01 -070072 std::optional<ResourceId> id;
Adam Lesinski75f3a552015-06-03 14:54:23 -070073};
74
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070075// An XML attribute.
Adam Lesinski75f3a552015-06-03 14:54:23 -070076struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 std::string name;
79 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
Ryan Mitchell4382e442021-07-14 12:53:01 -070081 std::optional<AaptAttribute> compiled_attribute;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070083};
84
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070085// An Element XML node.
Adam Lesinski6b372992017-08-09 10:54:23 -070086class Element : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 public:
Adam Lesinski6b372992017-08-09 10:54:23 -070088 // Ordered namespace prefix declarations.
89 std::vector<NamespaceDecl> namespace_decls;
90
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 std::string name;
93 std::vector<Attribute> attributes;
Adam Lesinski6b372992017-08-09 10:54:23 -070094 std::vector<std::unique_ptr<Node>> children;
95
96 void AppendChild(std::unique_ptr<Node> child);
97 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinski75f3a552015-06-03 14:54:23 -070098
Adam Lesinskid5083f62017-01-16 15:07:21 -080099 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700100 const Attribute* FindAttribute(const android::StringPiece& ns,
101 const android::StringPiece& name) const;
Adam Lesinskic6284372017-12-04 13:46:23 -0800102 Attribute* FindOrCreateAttribute(const android::StringPiece& ns,
103 const android::StringPiece& name);
Colin Crossdcd58c42018-05-25 22:46:35 -0700104 void RemoveAttribute(const android::StringPiece& ns,
105 const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700106
Adam Lesinski6b372992017-08-09 10:54:23 -0700107 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700108 const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const;
109
Adam Lesinski6b372992017-08-09 10:54:23 -0700110 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
111 const android::StringPiece& attr_ns,
112 const android::StringPiece& attr_name,
113 const android::StringPiece& attr_value);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700114
115 const Element* FindChildWithAttribute(const android::StringPiece& ns,
116 const android::StringPiece& name,
117 const android::StringPiece& attr_ns,
118 const android::StringPiece& attr_name,
119 const android::StringPiece& attr_value) const;
120
Adam Lesinski6b372992017-08-09 10:54:23 -0700121 std::vector<Element*> GetChildElements();
122
123 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
124 // that knows cloning an element returns an element.
125 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
126
127 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
128
129 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700130 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700131};
132
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700133// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700134class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 public:
136 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700137
Adam Lesinski6b372992017-08-09 10:54:23 -0700138 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
139
140 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700141 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700142};
143
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700144// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700145class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 public:
147 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700148
149 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
150 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
151 // is destroyed.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000152 android::StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700153
Adam Lesinski6b372992017-08-09 10:54:23 -0700154 std::unique_ptr<xml::Element> root;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700155
156 std::unique_ptr<XmlResource> Clone() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800157};
158
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700159// Inflates an XML DOM from an InputStream, logging errors to the logger.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000160std::unique_ptr<XmlResource> Inflate(io::InputStream* in, android::IDiagnostics* diag,
161 const android::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
Ryan Mitchell4382e442021-07-14 12:53:01 -0700238 std::optional<ExtractedPackage> TransformPackageAlias(
239 const android::StringPiece& alias) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700240
Adam Lesinski6b372992017-08-09 10:54:23 -0700241 protected:
242 PackageAwareVisitor() = default;
243
244 void BeforeVisitElement(Element* el) override;
245 void AfterVisitElement(Element* el) override;
246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700248 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 struct PackageDecl {
251 std::string prefix;
252 ExtractedPackage package;
253 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinski6b372992017-08-09 10:54:23 -0700255 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700256};
257
Adam Lesinski6b372992017-08-09 10:54:23 -0700258namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700259
Adam Lesinski6b372992017-08-09 10:54:23 -0700260// Base class that overrides the default behaviour and does not descend into child nodes.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700261class NodeCastBase : public ConstVisitor {
Adam Lesinski6b372992017-08-09 10:54:23 -0700262 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700263 void Visit(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700264 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700265 void Visit(const Text* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700266 }
267
268 protected:
269 NodeCastBase() = default;
270
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700271 void BeforeVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700272 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700273 void AfterVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700274 }
275
276 private:
277 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
278};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700279
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700281class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700283 using NodeCastBase::Visit;
284
285 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700287 const T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700288
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700289 void Visit(const T* v) override {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700290 value = v;
291 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700292
293 private:
294 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295};
296
Adam Lesinski6b372992017-08-09 10:54:23 -0700297} // namespace internal
298
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299template <typename T>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700300const T* NodeCast(const Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700301 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700304}
305
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700306template <typename T>
307T* NodeCast(Node* node) {
308 return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node)));
309}
310
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311} // namespace xml
312} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700313
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314#endif // AAPT_XML_DOM_H