blob: 54a70333fad266f1bcf2bf4903f24a8effd532fe [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 <memory>
21#include <string>
22#include <vector>
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "Diagnostics.h"
27#include "Resource.h"
28#include "ResourceValues.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029#include "io/Io.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlUtil.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070032
Adam Lesinski75f3a552015-06-03 14:54:23 -070033namespace aapt {
34namespace xml {
35
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070036class RawVisitor;
Adam Lesinski75f3a552015-06-03 14:54:23 -070037
Adam Lesinskic744ae82017-05-17 19:28:38 -070038class Element;
39
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070040// Base class for all XML nodes.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070041class Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 Node* parent = nullptr;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 size_t line_number = 0;
45 size_t column_number = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::string comment;
47 std::vector<std::unique_ptr<Node>> children;
Adam Lesinski75f3a552015-06-03 14:54:23 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 virtual ~Node() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080050
Adam Lesinskie343eb12016-10-27 16:31:58 -070051 void AppendChild(std::unique_ptr<Node> child);
52 void InsertChild(size_t index, std::unique_ptr<Node> child);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 virtual void Accept(RawVisitor* visitor) = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -070054
55 using ElementCloneFunc = std::function<void(const Element&, Element*)>;
56
57 // Clones the Node subtree, using the given function to decide how to clone an Element.
58 virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) = 0;
Adam Lesinski75f3a552015-06-03 14:54:23 -070059};
60
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070061// Base class that implements the visitor methods for a subclass of Node.
Adam Lesinski75f3a552015-06-03 14:54:23 -070062template <typename Derived>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070063class BaseNode : public Node {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 virtual void Accept(RawVisitor* visitor) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -070066};
67
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070068// A Namespace XML node. Can only have one child.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070069class Namespace : public BaseNode<Namespace> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::string namespace_prefix;
72 std::string namespace_uri;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070073
Adam Lesinskic744ae82017-05-17 19:28:38 -070074 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075};
Adam Lesinski75f3a552015-06-03 14:54:23 -070076
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077struct AaptAttribute {
Adam Lesinskic744ae82017-05-17 19:28:38 -070078 explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
79 : attribute(attr), id(resid) {
80 }
81
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 aapt::Attribute attribute;
Adam Lesinskic744ae82017-05-17 19:28:38 -070083 Maybe<ResourceId> id;
Adam Lesinski75f3a552015-06-03 14:54:23 -070084};
85
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070086// An XML attribute.
Adam Lesinski75f3a552015-06-03 14:54:23 -070087struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 std::string name;
90 std::string value;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 Maybe<AaptAttribute> compiled_attribute;
93 std::unique_ptr<Item> compiled_value;
Adam Lesinski75f3a552015-06-03 14:54:23 -070094};
95
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070096// An Element XML node.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070097class Element : public BaseNode<Element> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 std::string name;
101 std::vector<Attribute> attributes;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700102
Adam Lesinskid5083f62017-01-16 15:07:21 -0800103 Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700104 const Attribute* FindAttribute(const android::StringPiece& ns,
105 const android::StringPiece& name) const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
107 xml::Element* FindChildWithAttribute(const android::StringPiece& ns,
108 const android::StringPiece& name,
109 const android::StringPiece& attr_ns,
110 const android::StringPiece& attr_name,
111 const android::StringPiece& attr_value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 std::vector<xml::Element*> GetChildElements();
Adam Lesinskic744ae82017-05-17 19:28:38 -0700113 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700114};
115
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700116// A Text (CDATA) XML node. Can not have any children.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700117class Text : public BaseNode<Text> {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 public:
119 std::string text;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700120
Adam Lesinskic744ae82017-05-17 19:28:38 -0700121 std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700122};
123
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700124// An XML resource with a source, name, and XML tree.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700125class XmlResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 public:
127 ResourceFile file;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700128
129 // StringPool must come before the xml::Node. Destructors are called in reverse order, and
130 // the xml::Node may have StringPool references that need to be destroyed before the StringPool
131 // is destroyed.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 StringPool string_pool;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700133
134 std::unique_ptr<xml::Node> root;
Adam Lesinski467f1712015-11-16 17:35:44 -0800135};
136
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700137// Inflates an XML DOM from an InputStream, logging errors to the logger.
138// Returns the root node on success, or nullptr on failure.
139std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
Adam Lesinski75f3a552015-06-03 14:54:23 -0700140
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700141// Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
142// Returns the root node on success, or nullptr on failure.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
144 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 Lesinskiefeb7af2017-08-02 14:57:43 -0700149// A visitor interface for the different XML Node subtypes. This will not traverse into children.
150// Use Visitor for that.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700151class RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 public:
153 virtual ~RawVisitor() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 virtual void Visit(Namespace* node) {}
156 virtual void Visit(Element* node) {}
157 virtual void Visit(Text* text) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158};
159
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700160// Visitor whose default implementation visits the children nodes of any node.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700161class Visitor : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700165 void Visit(Namespace* node) override {
166 VisitChildren(node);
167 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700169 void Visit(Element* node) override {
170 VisitChildren(node);
171 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700173 void Visit(Text* text) override {
174 VisitChildren(text);
175 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 void VisitChildren(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 for (auto& child : node->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 child->Accept(this);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182};
183
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700184// An XML DOM visitor that will record the package name for a namespace prefix.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 using Visitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 void Visit(Namespace* ns) override;
190 Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800191 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 private:
194 struct PackageDecl {
195 std::string prefix;
196 ExtractedPackage package;
197 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 std::vector<PackageDecl> package_decls_;
Adam Lesinski75f3a552015-06-03 14:54:23 -0700200};
201
202// Implementations
203
204template <typename Derived>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205void BaseNode<Derived>::Accept(RawVisitor* visitor) {
206 visitor->Visit(static_cast<Derived*>(this));
Adam Lesinski75f3a552015-06-03 14:54:23 -0700207}
208
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209template <typename T>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700210class NodeCastImpl : public RawVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 using RawVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 T* value = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700216 void Visit(T* v) override {
217 value = v;
218 }
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