blob: b1987f1fd24563a2731134598db6d0362325ceea [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"
25
26#include "process/IResourceTableConsumer.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070027
28#include <istream>
Elliott Hughes51348d22015-07-21 11:39:21 -070029#include <expat.h>
Adam Lesinski75f3a552015-06-03 14:54:23 -070030#include <memory>
31#include <string>
32#include <vector>
33
34namespace aapt {
35namespace xml {
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037struct RawVisitor;
Adam Lesinski75f3a552015-06-03 14:54:23 -070038
39/**
40 * The type of node. Can be used to downcast to the concrete XML node
41 * class.
42 */
43enum class NodeType {
44 kNamespace,
45 kElement,
46 kText,
47};
48
49/**
50 * Base class for all XML nodes.
51 */
52struct Node {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053 Node* parent = nullptr;
54 size_t lineNumber = 0;
55 size_t columnNumber = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070056 std::u16string comment;
57 std::vector<std::unique_ptr<Node>> children;
58
Adam Lesinski75f3a552015-06-03 14:54:23 -070059 void addChild(std::unique_ptr<Node> child);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 virtual void accept(RawVisitor* visitor) = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070061 virtual ~Node() {}
62};
63
64/**
65 * Base class that implements the visitor methods for a
66 * subclass of Node.
67 */
68template <typename Derived>
69struct BaseNode : public Node {
Adam Lesinski1ab598f2015-08-14 14:26:04 -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 */
76struct Namespace : public BaseNode<Namespace> {
77 std::u16string namespacePrefix;
78 std::u16string namespaceUri;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079};
Adam Lesinski75f3a552015-06-03 14:54:23 -070080
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081struct AaptAttribute {
82 ResourceId id;
83 aapt::Attribute attribute;
Adam Lesinski75f3a552015-06-03 14:54:23 -070084};
85
86/**
87 * An XML attribute.
88 */
89struct Attribute {
90 std::u16string namespaceUri;
91 std::u16string name;
92 std::u16string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093
94 Maybe<AaptAttribute> compiledAttribute;
95 std::unique_ptr<Item> compiledValue;
Adam Lesinski75f3a552015-06-03 14:54:23 -070096};
97
98/**
99 * An Element XML node.
100 */
101struct Element : public BaseNode<Element> {
102 std::u16string namespaceUri;
103 std::u16string name;
104 std::vector<Attribute> attributes;
105
Adam Lesinski75f3a552015-06-03 14:54:23 -0700106 Attribute* findAttribute(const StringPiece16& ns, const StringPiece16& name);
107 xml::Element* findChild(const StringPiece16& ns, const StringPiece16& name);
108 xml::Element* findChildWithAttribute(const StringPiece16& ns, const StringPiece16& name,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 const StringPiece16& attrNs,
110 const StringPiece16& attrName,
111 const StringPiece16& attrValue);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700112 std::vector<xml::Element*> getChildElements();
113};
114
115/**
116 * A Text (CDATA) XML node. Can not have any children.
117 */
118struct Text : public BaseNode<Text> {
119 std::u16string text;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700120};
121
122/**
123 * Inflates an XML DOM from a text stream, logging errors to the logger.
124 * Returns the root node on success, or nullptr on failure.
125 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126std::unique_ptr<XmlResource> inflate(std::istream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700127
128/**
129 * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
130 * Returns the root node on success, or nullptr on failure.
131 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132std::unique_ptr<XmlResource> inflate(const void* data, size_t dataLen, IDiagnostics* diag,
133 const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700134
Adam Lesinskica5638f2015-10-21 14:42:43 -0700135Element* findRootElement(Node* node);
136
Adam Lesinski75f3a552015-06-03 14:54:23 -0700137/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 * A visitor interface for the different XML Node subtypes. This will not traverse into
139 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700140 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141struct RawVisitor {
142 virtual ~RawVisitor() = default;
143
144 virtual void visit(Namespace* node) {}
145 virtual void visit(Element* node) {}
146 virtual void visit(Text* text) {}
147};
148
149/**
150 * Visitor whose default implementation visits the children nodes of any node.
151 */
152struct Visitor : public RawVisitor {
153 using RawVisitor::visit;
154
155 void visit(Namespace* node) override {
156 visitChildren(node);
157 }
158
159 void visit(Element* node) override {
160 visitChildren(node);
161 }
162
163 void visit(Text* text) override {
164 visitChildren(text);
165 }
166
167 void visitChildren(Node* node) {
168 for (auto& child : node->children) {
169 child->accept(this);
170 }
171 }
172};
173
174/**
175 * An XML DOM visitor that will record the package name for a namespace prefix.
176 */
177class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
178private:
179 struct PackageDecl {
180 std::u16string prefix;
181 std::u16string package;
182 };
183
184 std::vector<PackageDecl> mPackageDecls;
185
186public:
187 using Visitor::visit;
188
189 void visit(Namespace* ns) override {
190 bool added = false;
191 {
192 Maybe<std::u16string> package = util::extractPackageFromNamespace(ns->namespaceUri);
193 if (package) {
194 mPackageDecls.push_back(PackageDecl{ ns->namespacePrefix, package.value() });
195 added = true;
196 }
197 }
198
199 Visitor::visit(ns);
200
201 if (added) {
202 mPackageDecls.pop_back();
203 }
204 }
205
206 Maybe<ResourceName> transformPackage(const ResourceName& name,
207 const StringPiece16& localPackage) const override {
208 if (name.package.empty()) {
209 return ResourceName{ localPackage.toString(), name.type, name.entry };
210 }
211
212 const auto rend = mPackageDecls.rend();
213 for (auto iter = mPackageDecls.rbegin(); iter != rend; ++iter) {
214 if (name.package == iter->prefix) {
215 if (iter->package.empty()) {
216 return ResourceName{ localPackage.toString(), name.type, name.entry };
217 } else {
218 return ResourceName{ iter->package, name.type, name.entry };
219 }
220 }
221 }
222 return {};
223 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700224};
225
226// Implementations
227
228template <typename Derived>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229void BaseNode<Derived>::accept(RawVisitor* visitor) {
230 visitor->visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700231}
232
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233template <typename T>
234struct NodeCastImpl : public RawVisitor {
235 using RawVisitor::visit;
236
237 T* value = nullptr;
238
239 void visit(T* v) override {
240 value = v;
241 }
242};
243
244template <typename T>
245T* nodeCast(Node* node) {
246 NodeCastImpl<T> visitor;
247 node->accept(&visitor);
248 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700249}
250
251} // namespace xml
252} // namespace aapt
253
254#endif // AAPT_XML_DOM_H