blob: 932303edf41a86e5243e35810dea5685ebb79f8f [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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "Diagnostics.h"
21#include "Resource.h"
22#include "ResourceValues.h"
23#include "util/StringPiece.h"
24#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "xml/XmlUtil.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070026
27#include <istream>
Adam Lesinski75f3a552015-06-03 14:54:23 -070028#include <memory>
29#include <string>
30#include <vector>
31
32namespace 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;
43 size_t lineNumber = 0;
44 size_t columnNumber = 0;
45 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 Lesinskicacb28f2016-10-19 12:18:14 -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:
62 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:
70 std::string namespacePrefix;
71 std::string namespaceUri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -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 Lesinskicacb28f2016-10-19 12:18:14 -070085 std::string namespaceUri;
86 std::string name;
87 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 Maybe<AaptAttribute> compiledAttribute;
90 std::unique_ptr<Item> compiledValue;
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:
98 std::string namespaceUri;
99 std::string name;
100 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700101
Adam Lesinskicacb28f2016-10-19 12:18:14 -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,
105 const StringPiece& name,
106 const StringPiece& attrNs,
107 const StringPiece& attrName,
108 const StringPiece& attrValue);
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 Lesinskicacb28f2016-10-19 12:18:14 -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 Lesinskicacb28f2016-10-19 12:18:14 -0700136std::unique_ptr<XmlResource> inflate(std::istream* in, IDiagnostics* diag,
137 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 Lesinskicacb28f2016-10-19 12:18:14 -0700143std::unique_ptr<XmlResource> inflate(const void* data, size_t dataLen,
144 IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700145
Adam Lesinski467f1712015-11-16 17:35:44 -0800146Element* findRootElement(XmlResource* doc);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700147Element* findRootElement(Node* node);
148
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 Lesinskicacb28f2016-10-19 12:18:14 -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:
168 using RawVisitor::visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 void visit(Namespace* node) override { visitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 void visit(Element* node) override { visitChildren(node); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 void visit(Text* text) override { visitChildren(text); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 void visitChildren(Node* node) {
177 for (auto& child : node->children) {
178 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:
188 using Visitor::visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 void visit(Namespace* ns) override;
191 Maybe<ExtractedPackage> transformPackageAlias(
192 const StringPiece& alias, const StringPiece& localPackage) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700193
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 private:
195 struct PackageDecl {
196 std::string prefix;
197 ExtractedPackage package;
198 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 std::vector<PackageDecl> mPackageDecls;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700201};
202
203// Implementations
204
205template <typename Derived>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206void BaseNode<Derived>::accept(RawVisitor* visitor) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 visitor->visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700208}
209
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210template <typename T>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700211class NodeCastImpl : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 public:
213 using RawVisitor::visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 void visit(T* v) override { value = v; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218};
219
220template <typename T>
221T* nodeCast(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 NodeCastImpl<T> visitor;
223 node->accept(&visitor);
224 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700225}
226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227} // namespace xml
228} // namespace aapt
Adam Lesinski75f3a552015-06-03 14:54:23 -0700229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230#endif // AAPT_XML_DOM_H