blob: 2dc99d6931484e82e760bdc98c1f0e7fa76ad59f [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 <istream>
21#include <memory>
22#include <string>
23#include <vector>
24
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
26
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "Diagnostics.h"
28#include "Resource.h"
29#include "ResourceValues.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 Lesinski5eeaadd2016-08-25 12:26:56 -070036class RawVisitor;
Adam Lesinski75f3a552015-06-03 14:54:23 -070037
Adam Lesinskic744ae82017-05-17 19:28:38 -070038class Element;
39
Adam Lesinski75f3a552015-06-03 14:54:23 -070040/**
Adam Lesinski75f3a552015-06-03 14:54:23 -070041 * Base class for all XML nodes.
42 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070043class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 public:
45 Node* parent = nullptr;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 size_t line_number = 0;
47 size_t column_number = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 std::string comment;
49 std::vector<std::unique_ptr<Node>> children;
Adam Lesinski75f3a552015-06-03 14:54:23 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080052
Adam Lesinskie343eb12016-10-27 16:31:58 -070053 void AppendChild(std::unique_ptr<Node> child);
54 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 virtual void Accept(RawVisitor* visitor) = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -070056
57 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
58
59 // Clones the Node subtree, using the given function to decide how to clone an Element.
60 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070061};
62
63/**
64 * Base class that implements the visitor methods for a
65 * subclass of Node.
66 */
67template <typename Derived>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070068class BaseNode : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 virtual void Accept(RawVisitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -070071};
72
73/**
74 * A Namespace XML node. Can only have one child.
75 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070076class Namespace : public BaseNode<Namespace> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::string namespace_prefix;
79 std::string namespace_uri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070080
Adam Lesinskic744ae82017-05-17 19:28:38 -070081 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082};
Adam Lesinski75f3a552015-06-03 14:54:23 -070083
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084struct AaptAttribute {
Adam Lesinskic744ae82017-05-17 19:28:38 -070085 explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
86 : attribute(attr), id(resid) {
87 }
88
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 aapt::Attribute attribute;
Adam Lesinskic744ae82017-05-17 19:28:38 -070090 Maybe<ResourceId> id;
Adam Lesinski75f3a552015-06-03 14:54:23 -070091};
92
93/**
94 * An XML attribute.
95 */
96struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 std::string name;
99 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 Maybe<AaptAttribute> compiled_attribute;
102 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700103};
104
105/**
106 * An Element XML node.
107 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700108class Element : public BaseNode<Element> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 std::string name;
112 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700113
Adam Lesinskid5083f62017-01-16 15:07:21 -0800114 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700115 const Attribute* FindAttribute(const android::StringPiece& ns,
116 const android::StringPiece& name) const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800117 xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
118 xml::Element* FindChildWithAttribute(const android::StringPiece& ns,
119 const android::StringPiece& name,
120 const android::StringPiece& attr_ns,
121 const android::StringPiece& attr_name,
122 const android::StringPiece& attr_value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 std::vector<xml::Element*> GetChildElements();
Adam Lesinskic744ae82017-05-17 19:28:38 -0700124 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700125};
126
127/**
128 * A Text (CDATA) XML node. Can not have any children.
129 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700130class Text : public BaseNode<Text> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 public:
132 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700133
Adam Lesinskic744ae82017-05-17 19:28:38 -0700134 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700135};
136
137/**
Adam Lesinski467f1712015-11-16 17:35:44 -0800138 * An XML resource with a source, name, and XML tree.
139 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700140class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 public:
142 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700143
144 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
145 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
146 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700147 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700148
149 std::unique_ptr<xml::Node> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800150};
151
152/**
Adam Lesinski75f3a552015-06-03 14:54:23 -0700153 * Inflates an XML DOM from a text stream, logging errors to the logger.
154 * Returns the root node on success, or nullptr on failure.
155 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700156std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700157
158/**
159 * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
160 * Returns the root node on success, or nullptr on failure.
161 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700162std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
163 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165Element* FindRootElement(XmlResource* doc);
166Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700167
Adam Lesinski75f3a552015-06-03 14:54:23 -0700168/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 * A visitor interface for the different XML Node subtypes. This will not
170 * traverse into
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700172 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700173class RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 public:
175 virtual ~RawVisitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 virtual void Visit(Namespace* node) {}
178 virtual void Visit(Element* node) {}
179 virtual void Visit(Text* text) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180};
181
182/**
183 * Visitor whose default implementation visits the children nodes of any node.
184 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700185class Visitor : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 void Visit(Namespace* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 void Visit(Element* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 void Visit(Text* text) override { VisitChildren(text); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 void VisitChildren(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 for (auto& child : node->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200};
201
202/**
203 * An XML DOM visitor that will record the package name for a namespace prefix.
204 */
205class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 void Visit(Namespace* ns) override;
210 Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800211 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 private:
214 struct PackageDecl {
215 std::string prefix;
216 ExtractedPackage package;
217 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 std::vector<PackageDecl> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700220};
221
222// Implementations
223
224template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225void BaseNode<Derived>::Accept(RawVisitor* visitor) {
226 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700227}
228
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229template <typename T>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700230class NodeCastImpl : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 void Visit(T* v) override { value = v; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237};
238
239template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240T* NodeCast(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700244}
245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246} // namespace xml
247} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249#endif // AAPT_XML_DOM_H