blob: 90cdfb6932b150df42e4484b95a81f04b8d2cfde [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
38/**
Adam Lesinski75f3a552015-06-03 14:54:23 -070039 * Base class for all XML nodes.
40 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070041class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 Node* parent = nullptr;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 size_t line_number = 0;
45 size_t column_number = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::string comment;
47 std::vector<std::unique_ptr<Node>> children;
Adam Lesinski75f3a552015-06-03 14:54:23 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080050
Adam Lesinskie343eb12016-10-27 16:31:58 -070051 void AppendChild(std::unique_ptr<Node> child);
52 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 virtual void Accept(RawVisitor* visitor) = 0;
54 virtual std::unique_ptr<Node> Clone() = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070055};
56
57/**
58 * Base class that implements the visitor methods for a
59 * subclass of Node.
60 */
61template <typename Derived>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070062class BaseNode : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 virtual void Accept(RawVisitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -070065};
66
67/**
68 * A Namespace XML node. Can only have one child.
69 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070070class Namespace : public BaseNode<Namespace> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string namespace_prefix;
73 std::string namespace_uri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::unique_ptr<Node> Clone() override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076};
Adam Lesinski75f3a552015-06-03 14:54:23 -070077
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078struct AaptAttribute {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 Maybe<ResourceId> id;
80 aapt::Attribute attribute;
Adam Lesinski75f3a552015-06-03 14:54:23 -070081};
82
83/**
84 * An XML attribute.
85 */
86struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 std::string name;
89 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 Maybe<AaptAttribute> compiled_attribute;
92 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070093};
94
95/**
96 * An Element XML node.
97 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070098class Element : public BaseNode<Element> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 std::string name;
102 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700103
Adam Lesinskid5083f62017-01-16 15:07:21 -0800104 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
105 xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
106 xml::Element* FindChildWithAttribute(const android::StringPiece& ns,
107 const android::StringPiece& name,
108 const android::StringPiece& attr_ns,
109 const android::StringPiece& attr_name,
110 const android::StringPiece& attr_value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 std::vector<xml::Element*> GetChildElements();
112 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700113};
114
115/**
116 * A Text (CDATA) XML node. Can not have any children.
117 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700118class Text : public BaseNode<Text> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 public:
120 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700123};
124
125/**
Adam Lesinski467f1712015-11-16 17:35:44 -0800126 * An XML resource with a source, name, and XML tree.
127 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700128class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 public:
130 ResourceFile file;
131 std::unique_ptr<xml::Node> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800132};
133
134/**
Adam Lesinski75f3a552015-06-03 14:54:23 -0700135 * Inflates an XML DOM from a text stream, logging errors to the logger.
136 * Returns the root node on success, or nullptr on failure.
137 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700140
141/**
142 * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
143 * Returns the root node on success, or nullptr on failure.
144 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148Element* FindRootElement(XmlResource* doc);
149Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700150
Adam Lesinski75f3a552015-06-03 14:54:23 -0700151/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 * A visitor interface for the different XML Node subtypes. This will not
153 * traverse into
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700155 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700156class RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 public:
158 virtual ~RawVisitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 virtual void Visit(Namespace* node) {}
161 virtual void Visit(Element* node) {}
162 virtual void Visit(Text* text) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163};
164
165/**
166 * Visitor whose default implementation visits the children nodes of any node.
167 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700168class Visitor : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 void Visit(Namespace* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 void Visit(Element* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 void Visit(Text* text) override { VisitChildren(text); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 void VisitChildren(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 for (auto& child : node->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183};
184
185/**
186 * An XML DOM visitor that will record the package name for a namespace prefix.
187 */
188class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 void Visit(Namespace* ns) override;
193 Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800194 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 private:
197 struct PackageDecl {
198 std::string prefix;
199 ExtractedPackage package;
200 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 std::vector<PackageDecl> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700203};
204
205// Implementations
206
207template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208void BaseNode<Derived>::Accept(RawVisitor* visitor) {
209 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700210}
211
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212template <typename T>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700213class NodeCastImpl : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 void Visit(T* v) override { value = v; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700220};
221
222template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223T* NodeCast(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700227}
228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229} // namespace xml
230} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232#endif // AAPT_XML_DOM_H