blob: 8f3829611f30edb95561b4c2cac0e6a74ec6c43f [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);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700105
Adam Lesinski6b372992017-08-09 10:54:23 -0700106 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700107 const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const;
108
Adam Lesinski6b372992017-08-09 10:54:23 -0700109 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
110 const android::StringPiece& attr_ns,
111 const android::StringPiece& attr_name,
112 const android::StringPiece& attr_value);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700113
114 const Element* FindChildWithAttribute(const android::StringPiece& ns,
115 const android::StringPiece& name,
116 const android::StringPiece& attr_ns,
117 const android::StringPiece& attr_name,
118 const android::StringPiece& attr_value) const;
119
Adam Lesinski6b372992017-08-09 10:54:23 -0700120 std::vector<Element*> GetChildElements();
121
122 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
123 // that knows cloning an element returns an element.
124 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
125
126 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
127
128 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700129 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700130};
131
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700132// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski6b372992017-08-09 10:54:23 -0700133class Text : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 public:
135 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700136
Adam Lesinski6b372992017-08-09 10:54:23 -0700137 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
138
139 void Accept(Visitor* visitor) override;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700140 void Accept(ConstVisitor* visitor) const override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700141};
142
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700143// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700144class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 public:
146 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700147
148 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
149 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
150 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700151 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700152
Adam Lesinski6b372992017-08-09 10:54:23 -0700153 std::unique_ptr<xml::Element> root;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700154
155 std::unique_ptr<XmlResource> Clone() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800156};
157
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700158// Inflates an XML DOM from an InputStream, logging errors to the logger.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700159std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700160
Adam Lesinski8780eb62017-10-31 17:44:39 -0700161// Inflates an XML DOM from a binary ResXMLTree.
162std::unique_ptr<XmlResource> Inflate(const void* data, size_t len,
163 std::string* out_error = nullptr);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700166
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700167// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski6b372992017-08-09 10:54:23 -0700168class Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 virtual ~Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinski6b372992017-08-09 10:54:23 -0700172 virtual void Visit(Element* el) {
173 VisitChildren(el);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700174 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinski6b372992017-08-09 10:54:23 -0700176 virtual void Visit(Text* text) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700177 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178
Adam Lesinski6b372992017-08-09 10:54:23 -0700179 protected:
180 Visitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181
Adam Lesinski6b372992017-08-09 10:54:23 -0700182 void VisitChildren(Element* el) {
183 for (auto& child : el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700187
188 virtual void BeforeVisitElement(Element* el) {
189 }
190 virtual void AfterVisitElement(Element* el) {
191 }
192
193 private:
194 DISALLOW_COPY_AND_ASSIGN(Visitor);
195
196 friend class Element;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197};
198
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700199class ConstVisitor {
200 public:
201 virtual ~ConstVisitor() = default;
202
203 virtual void Visit(const Element* el) {
204 VisitChildren(el);
205 }
206
207 virtual void Visit(const Text* text) {
208 }
209
210 protected:
211 ConstVisitor() = default;
212
213 void VisitChildren(const Element* el) {
214 for (const auto& child : el->children) {
215 child->Accept(this);
216 }
217 }
218
219 virtual void BeforeVisitElement(const Element* el) {
220 }
221
222 virtual void AfterVisitElement(const Element* el) {
223 }
224
225 private:
226 DISALLOW_COPY_AND_ASSIGN(ConstVisitor);
227
228 friend class Element;
229};
230
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700231// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700235
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700236 Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700237
Adam Lesinski6b372992017-08-09 10:54:23 -0700238 protected:
239 PackageAwareVisitor() = default;
240
241 void BeforeVisitElement(Element* el) override;
242 void AfterVisitElement(Element* el) override;
243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 private:
Adam Lesinski6b372992017-08-09 10:54:23 -0700245 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 struct PackageDecl {
248 std::string prefix;
249 ExtractedPackage package;
250 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251
Adam Lesinski6b372992017-08-09 10:54:23 -0700252 std::vector<std::vector<PackageDecl>> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700253};
254
Adam Lesinski6b372992017-08-09 10:54:23 -0700255namespace internal {
Adam Lesinski75f3a552015-06-03 14:54:23 -0700256
Adam Lesinski6b372992017-08-09 10:54:23 -0700257// Base class that overrides the default behaviour and does not descend into child nodes.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700258class NodeCastBase : public ConstVisitor {
Adam Lesinski6b372992017-08-09 10:54:23 -0700259 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700260 void Visit(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700261 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700262 void Visit(const Text* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700263 }
264
265 protected:
266 NodeCastBase() = default;
267
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700268 void BeforeVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700269 }
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700270 void AfterVisitElement(const Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700271 }
272
273 private:
274 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
275};
Adam Lesinski75f3a552015-06-03 14:54:23 -0700276
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277template <typename T>
Adam Lesinski6b372992017-08-09 10:54:23 -0700278class NodeCastImpl : public NodeCastBase {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 public:
Adam Lesinski6b372992017-08-09 10:54:23 -0700280 using NodeCastBase::Visit;
281
282 NodeCastImpl() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700284 const T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700286 void Visit(const T* v) override {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700287 value = v;
288 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700289
290 private:
291 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292};
293
Adam Lesinski6b372992017-08-09 10:54:23 -0700294} // namespace internal
295
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296template <typename T>
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700297const T* NodeCast(const Node* node) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700298 internal::NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700301}
302
Adam Lesinskid3ffa8442017-09-28 13:34:35 -0700303template <typename T>
304T* NodeCast(Node* node) {
305 return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node)));
306}
307
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308} // namespace xml
309} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700310
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311#endif // AAPT_XML_DOM_H