blob: c095f085f1fabeec9e2bfa0054d529227df5f65e [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
135/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 * A visitor interface for the different XML Node subtypes. This will not traverse into
137 * children. Use Visitor for that.
Adam Lesinski75f3a552015-06-03 14:54:23 -0700138 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139struct RawVisitor {
140 virtual ~RawVisitor() = default;
141
142 virtual void visit(Namespace* node) {}
143 virtual void visit(Element* node) {}
144 virtual void visit(Text* text) {}
145};
146
147/**
148 * Visitor whose default implementation visits the children nodes of any node.
149 */
150struct Visitor : public RawVisitor {
151 using RawVisitor::visit;
152
153 void visit(Namespace* node) override {
154 visitChildren(node);
155 }
156
157 void visit(Element* node) override {
158 visitChildren(node);
159 }
160
161 void visit(Text* text) override {
162 visitChildren(text);
163 }
164
165 void visitChildren(Node* node) {
166 for (auto& child : node->children) {
167 child->accept(this);
168 }
169 }
170};
171
172/**
173 * An XML DOM visitor that will record the package name for a namespace prefix.
174 */
175class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
176private:
177 struct PackageDecl {
178 std::u16string prefix;
179 std::u16string package;
180 };
181
182 std::vector<PackageDecl> mPackageDecls;
183
184public:
185 using Visitor::visit;
186
187 void visit(Namespace* ns) override {
188 bool added = false;
189 {
190 Maybe<std::u16string> package = util::extractPackageFromNamespace(ns->namespaceUri);
191 if (package) {
192 mPackageDecls.push_back(PackageDecl{ ns->namespacePrefix, package.value() });
193 added = true;
194 }
195 }
196
197 Visitor::visit(ns);
198
199 if (added) {
200 mPackageDecls.pop_back();
201 }
202 }
203
204 Maybe<ResourceName> transformPackage(const ResourceName& name,
205 const StringPiece16& localPackage) const override {
206 if (name.package.empty()) {
207 return ResourceName{ localPackage.toString(), name.type, name.entry };
208 }
209
210 const auto rend = mPackageDecls.rend();
211 for (auto iter = mPackageDecls.rbegin(); iter != rend; ++iter) {
212 if (name.package == iter->prefix) {
213 if (iter->package.empty()) {
214 return ResourceName{ localPackage.toString(), name.type, name.entry };
215 } else {
216 return ResourceName{ iter->package, name.type, name.entry };
217 }
218 }
219 }
220 return {};
221 }
Adam Lesinski75f3a552015-06-03 14:54:23 -0700222};
223
224// Implementations
225
226template <typename Derived>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227void BaseNode<Derived>::accept(RawVisitor* visitor) {
228 visitor->visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700229}
230
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231template <typename T>
232struct NodeCastImpl : public RawVisitor {
233 using RawVisitor::visit;
234
235 T* value = nullptr;
236
237 void visit(T* v) override {
238 value = v;
239 }
240};
241
242template <typename T>
243T* nodeCast(Node* node) {
244 NodeCastImpl<T> visitor;
245 node->accept(&visitor);
246 return visitor.value;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700247}
248
249} // namespace xml
250} // namespace aapt
251
252#endif // AAPT_XML_DOM_H