blob: 720fe357bfa10fda805b1a57cd2ad85a75608700 [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 Lesinski1ab598f2015-08-14 14:26:04 -070025#include "Diagnostics.h"
26#include "Resource.h"
27#include "ResourceValues.h"
28#include "util/StringPiece.h"
29#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 Lesinski5eeaadd2016-08-25 12:26:56 -070035class RawVisitor;
Adam Lesinski75f3a552015-06-03 14:54:23 -070036
37/**
Adam Lesinski75f3a552015-06-03 14:54:23 -070038 * Base class for all XML nodes.
39 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070040class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 public:
42 Node* parent = nullptr;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 size_t line_number = 0;
44 size_t column_number = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 std::string comment;
46 std::vector<std::unique_ptr<Node>> children;
Adam Lesinski75f3a552015-06-03 14:54:23 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080049
Adam Lesinskie343eb12016-10-27 16:31:58 -070050 void AppendChild(std::unique_ptr<Node> child);
51 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 virtual void Accept(RawVisitor* visitor) = 0;
53 virtual std::unique_ptr<Node> Clone() = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070054};
55
56/**
57 * Base class that implements the visitor methods for a
58 * subclass of Node.
59 */
60template <typename Derived>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061class BaseNode : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 virtual void Accept(RawVisitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -070064};
65
66/**
67 * A Namespace XML node. Can only have one child.
68 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070069class Namespace : public BaseNode<Namespace> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::string namespace_prefix;
72 std::string namespace_uri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::unique_ptr<Node> Clone() override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075};
Adam Lesinski75f3a552015-06-03 14:54:23 -070076
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077struct AaptAttribute {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 Maybe<ResourceId> id;
79 aapt::Attribute attribute;
Adam Lesinski75f3a552015-06-03 14:54:23 -070080};
81
82/**
83 * An XML attribute.
84 */
85struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 std::string name;
88 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 Maybe<AaptAttribute> compiled_attribute;
91 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070092};
93
94/**
95 * An Element XML node.
96 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070097class Element : public BaseNode<Element> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 std::string name;
101 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 Attribute* FindAttribute(const StringPiece& ns, const StringPiece& name);
104 xml::Element* FindChild(const StringPiece& ns, const StringPiece& name);
105 xml::Element* FindChildWithAttribute(const StringPiece& ns,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 const StringPiece& name,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 const StringPiece& attr_ns,
108 const StringPiece& attr_name,
109 const StringPiece& attr_value);
110 std::vector<xml::Element*> GetChildElements();
111 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700112};
113
114/**
115 * A Text (CDATA) XML node. Can not have any children.
116 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700117class Text : public BaseNode<Text> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 public:
119 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700122};
123
124/**
Adam Lesinski467f1712015-11-16 17:35:44 -0800125 * An XML resource with a source, name, and XML tree.
126 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700127class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 public:
129 ResourceFile file;
130 std::unique_ptr<xml::Node> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800131};
132
133/**
Adam Lesinski75f3a552015-06-03 14:54:23 -0700134 * Inflates an XML DOM from a text stream, logging errors to the logger.
135 * Returns the root node on success, or nullptr on failure.
136 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700139
140/**
141 * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
142 * Returns the root node on success, or nullptr on failure.
143 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147Element* FindRootElement(XmlResource* doc);
148Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700149
Adam Lesinski75f3a552015-06-03 14:54:23 -0700150/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 * A visitor interface for the different XML Node subtypes. This will not
152 * traverse into
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700154 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700155class RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 public:
157 virtual ~RawVisitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 virtual void Visit(Namespace* node) {}
160 virtual void Visit(Element* node) {}
161 virtual void Visit(Text* text) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162};
163
164/**
165 * Visitor whose default implementation visits the children nodes of any node.
166 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700167class Visitor : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 void Visit(Namespace* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 void Visit(Element* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 void Visit(Text* text) override { VisitChildren(text); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 void VisitChildren(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 for (auto& child : node->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182};
183
184/**
185 * An XML DOM visitor that will record the package name for a namespace prefix.
186 */
187class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 void Visit(Namespace* ns) override;
192 Maybe<ExtractedPackage> TransformPackageAlias(
193 const StringPiece& alias,
194 const 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