1 /*
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
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 #include "androidfw/StringPiece.h"
25
26 #include "Diagnostics.h"
27 #include "Resource.h"
28 #include "ResourceValues.h"
29 #include "io/Io.h"
30 #include "util/Util.h"
31 #include "xml/XmlUtil.h"
32
33 namespace aapt {
34 namespace xml {
35
36 class Element;
37 class Visitor;
38
39 // Base class for all XML nodes.
40 class Node {
41 public:
42 virtual ~Node() = default;
43
44 Element* parent = nullptr;
45 size_t line_number = 0u;
46 size_t column_number = 0u;
47 std::string comment;
48
49 virtual void Accept(Visitor* visitor) = 0;
50
51 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
52
53 // Clones the Node subtree, using the given function to decide how to clone an Element.
54 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0;
55 };
56
57 // A namespace declaration (xmlns:prefix="uri").
58 struct NamespaceDecl {
59 std::string prefix;
60 std::string uri;
61 size_t line_number = 0u;
62 size_t column_number = 0u;
63 };
64
65 struct AaptAttribute {
66 explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
attributeAaptAttribute67 : attribute(attr), id(resid) {
68 }
69
70 aapt::Attribute attribute;
71 Maybe<ResourceId> id;
72 };
73
74 // An XML attribute.
75 struct Attribute {
76 std::string namespace_uri;
77 std::string name;
78 std::string value;
79
80 Maybe<AaptAttribute> compiled_attribute;
81 std::unique_ptr<Item> compiled_value;
82 };
83
84 // An Element XML node.
85 class Element : public Node {
86 public:
87 // Ordered namespace prefix declarations.
88 std::vector<NamespaceDecl> namespace_decls;
89
90 std::string namespace_uri;
91 std::string name;
92 std::vector<Attribute> attributes;
93 std::vector<std::unique_ptr<Node>> children;
94
95 void AppendChild(std::unique_ptr<Node> child);
96 void InsertChild(size_t index, std::unique_ptr<Node> child);
97
98 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
99 const Attribute* FindAttribute(const android::StringPiece& ns,
100 const android::StringPiece& name) const;
101 Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
102 Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name,
103 const android::StringPiece& attr_ns,
104 const android::StringPiece& attr_name,
105 const android::StringPiece& attr_value);
106 std::vector<Element*> GetChildElements();
107
108 // Due to overriding of subtypes not working with unique_ptr, define a convenience Clone method
109 // that knows cloning an element returns an element.
110 std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const;
111
112 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
113
114 void Accept(Visitor* visitor) override;
115 };
116
117 // A Text (CDATA) XML node. Can not have any children.
118 class Text : public Node {
119 public:
120 std::string text;
121
122 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override;
123
124 void Accept(Visitor* visitor) override;
125 };
126
127 // An XML resource with a source, name, and XML tree.
128 class XmlResource {
129 public:
130 ResourceFile file;
131
132 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
133 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
134 // is destroyed.
135 StringPool string_pool;
136
137 std::unique_ptr<xml::Element> root;
138 };
139
140 // Inflates an XML DOM from an InputStream, logging errors to the logger.
141 // Returns the root node on success, or nullptr on failure.
142 std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
143
144 // Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
145 // Returns the root node on success, or nullptr on failure.
146 std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
147 const Source& source);
148
149 Element* FindRootElement(Node* node);
150
151 // Visitor whose default implementation visits the children nodes of any node.
152 class Visitor {
153 public:
154 virtual ~Visitor() = default;
155
Visit(Element * el)156 virtual void Visit(Element* el) {
157 VisitChildren(el);
158 }
159
Visit(Text * text)160 virtual void Visit(Text* text) {
161 }
162
163 protected:
164 Visitor() = default;
165
VisitChildren(Element * el)166 void VisitChildren(Element* el) {
167 for (auto& child : el->children) {
168 child->Accept(this);
169 }
170 }
171
BeforeVisitElement(Element * el)172 virtual void BeforeVisitElement(Element* el) {
173 }
AfterVisitElement(Element * el)174 virtual void AfterVisitElement(Element* el) {
175 }
176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(Visitor);
179
180 friend class Element;
181 };
182
183 // An XML DOM visitor that will record the package name for a namespace prefix.
184 class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
185 public:
186 using Visitor::Visit;
187
188 Maybe<ExtractedPackage> TransformPackageAlias(
189 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
190
191 protected:
192 PackageAwareVisitor() = default;
193
194 void BeforeVisitElement(Element* el) override;
195 void AfterVisitElement(Element* el) override;
196
197 private:
198 DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor);
199
200 struct PackageDecl {
201 std::string prefix;
202 ExtractedPackage package;
203 };
204
205 std::vector<std::vector<PackageDecl>> package_decls_;
206 };
207
208 namespace internal {
209
210 // Base class that overrides the default behaviour and does not descend into child nodes.
211 class NodeCastBase : public Visitor {
212 public:
Visit(Element * el)213 void Visit(Element* el) override {
214 }
Visit(Text * el)215 void Visit(Text* el) override {
216 }
217
218 protected:
219 NodeCastBase() = default;
220
BeforeVisitElement(Element * el)221 void BeforeVisitElement(Element* el) override {
222 }
AfterVisitElement(Element * el)223 void AfterVisitElement(Element* el) override {
224 }
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(NodeCastBase);
228 };
229
230 template <typename T>
231 class NodeCastImpl : public NodeCastBase {
232 public:
233 using NodeCastBase::Visit;
234
235 NodeCastImpl() = default;
236
237 T* value = nullptr;
238
Visit(T * v)239 void Visit(T* v) override {
240 value = v;
241 }
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(NodeCastImpl);
245 };
246
247 } // namespace internal
248
249 template <typename T>
NodeCast(Node * node)250 T* NodeCast(Node* node) {
251 internal::NodeCastImpl<T> visitor;
252 node->Accept(&visitor);
253 return visitor.value;
254 }
255
256 } // namespace xml
257 } // namespace aapt
258
259 #endif // AAPT_XML_DOM_H
260