blob: e771d8735ac045e9de86c2419303040f9d20a23d [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 Lesinskice5e56e2016-10-21 17:56:45 -070050 void AddChild(std::unique_ptr<Node> child);
51 virtual void Accept(RawVisitor* visitor) = 0;
52 virtual std::unique_ptr<Node> Clone() = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070053};
54
55/**
56 * Base class that implements the visitor methods for a
57 * subclass of Node.
58 */
59template <typename Derived>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070060class BaseNode : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 virtual void Accept(RawVisitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -070063};
64
65/**
66 * A Namespace XML node. Can only have one child.
67 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070068class Namespace : public BaseNode<Namespace> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::string namespace_prefix;
71 std::string namespace_uri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::unique_ptr<Node> Clone() override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
Adam Lesinski75f3a552015-06-03 14:54:23 -070075
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076struct AaptAttribute {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 Maybe<ResourceId> id;
78 aapt::Attribute attribute;
Adam Lesinski75f3a552015-06-03 14:54:23 -070079};
80
81/**
82 * An XML attribute.
83 */
84struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 std::string name;
87 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 Maybe<AaptAttribute> compiled_attribute;
90 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070091};
92
93/**
94 * An Element XML node.
95 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070096class Element : public BaseNode<Element> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 std::string name;
100 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 Attribute* FindAttribute(const StringPiece& ns, const StringPiece& name);
103 xml::Element* FindChild(const StringPiece& ns, const StringPiece& name);
104 xml::Element* FindChildWithAttribute(const StringPiece& ns,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 const StringPiece& name,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 const StringPiece& attr_ns,
107 const StringPiece& attr_name,
108 const StringPiece& attr_value);
109 std::vector<xml::Element*> GetChildElements();
110 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700111};
112
113/**
114 * A Text (CDATA) XML node. Can not have any children.
115 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700116class Text : public BaseNode<Text> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 public:
118 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 std::unique_ptr<Node> Clone() override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700121};
122
123/**
Adam Lesinski467f1712015-11-16 17:35:44 -0800124 * An XML resource with a source, name, and XML tree.
125 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700126class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 public:
128 ResourceFile file;
129 std::unique_ptr<xml::Node> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800130};
131
132/**
Adam Lesinski75f3a552015-06-03 14:54:23 -0700133 * Inflates an XML DOM from a text stream, logging errors to the logger.
134 * Returns the root node on success, or nullptr on failure.
135 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700138
139/**
140 * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
141 * Returns the root node on success, or nullptr on failure.
142 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146Element* FindRootElement(XmlResource* doc);
147Element* FindRootElement(Node* node);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700148
Adam Lesinski75f3a552015-06-03 14:54:23 -0700149/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 * A visitor interface for the different XML Node subtypes. This will not
151 * traverse into
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700153 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700154class RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 public:
156 virtual ~RawVisitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 virtual void Visit(Namespace* node) {}
159 virtual void Visit(Element* node) {}
160 virtual void Visit(Text* text) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161};
162
163/**
164 * Visitor whose default implementation visits the children nodes of any node.
165 */
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700166class Visitor : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 void Visit(Namespace* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 void Visit(Element* node) override { VisitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 void Visit(Text* text) override { VisitChildren(text); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 void VisitChildren(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 for (auto& child : node->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181};
182
183/**
184 * An XML DOM visitor that will record the package name for a namespace prefix.
185 */
186class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 void Visit(Namespace* ns) override;
191 Maybe<ExtractedPackage> TransformPackageAlias(
192 const StringPiece& alias,
193 const StringPiece& local_package) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 private:
196 struct PackageDecl {
197 std::string prefix;
198 ExtractedPackage package;
199 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 std::vector<PackageDecl> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700202};
203
204// Implementations
205
206template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207void BaseNode<Derived>::Accept(RawVisitor* visitor) {
208 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700209}
210
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211template <typename T>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700212class NodeCastImpl : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 void Visit(T* v) override { value = v; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219};
220
221template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222T* NodeCast(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 NodeCastImpl<T> visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700226}
227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228} // namespace xml
229} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231#endif // AAPT_XML_DOM_H