Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <istream> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
| 26 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "Diagnostics.h" |
| 28 | #include "Resource.h" |
| 29 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 31 | #include "xml/XmlUtil.h" |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 33 | namespace aapt { |
| 34 | namespace xml { |
| 35 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 36 | class RawVisitor; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 38 | class Element; |
| 39 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 40 | /** |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 41 | * Base class for all XML nodes. |
| 42 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 43 | class Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | public: |
| 45 | Node* parent = nullptr; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | size_t line_number = 0; |
| 47 | size_t column_number = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | std::string comment; |
| 49 | std::vector<std::unique_ptr<Node>> children; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | virtual ~Node() = default; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 53 | void AppendChild(std::unique_ptr<Node> child); |
| 54 | void InsertChild(size_t index, std::unique_ptr<Node> child); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | virtual void Accept(RawVisitor* visitor) = 0; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 56 | |
| 57 | using ElementCloneFunc = std::function<void(const Element&, Element*)>; |
| 58 | |
| 59 | // Clones the Node subtree, using the given function to decide how to clone an Element. |
| 60 | virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) = 0; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Base class that implements the visitor methods for a |
| 65 | * subclass of Node. |
| 66 | */ |
| 67 | template <typename Derived> |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 68 | class BaseNode : public Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | virtual void Accept(RawVisitor* visitor) override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * A Namespace XML node. Can only have one child. |
| 75 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 76 | class Namespace : public BaseNode<Namespace> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | std::string namespace_prefix; |
| 79 | std::string namespace_uri; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 81 | std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | }; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 84 | struct AaptAttribute { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 85 | explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {}) |
| 86 | : attribute(attr), id(resid) { |
| 87 | } |
| 88 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | aapt::Attribute attribute; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 90 | Maybe<ResourceId> id; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * An XML attribute. |
| 95 | */ |
| 96 | struct Attribute { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | std::string namespace_uri; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | std::string name; |
| 99 | std::string value; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | Maybe<AaptAttribute> compiled_attribute; |
| 102 | std::unique_ptr<Item> compiled_value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * An Element XML node. |
| 107 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 108 | class Element : public BaseNode<Element> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | std::string namespace_uri; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | std::string name; |
| 112 | std::vector<Attribute> attributes; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 114 | Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 115 | const Attribute* FindAttribute(const android::StringPiece& ns, |
| 116 | const android::StringPiece& name) const; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 117 | xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name); |
| 118 | xml::Element* FindChildWithAttribute(const android::StringPiece& ns, |
| 119 | const android::StringPiece& name, |
| 120 | const android::StringPiece& attr_ns, |
| 121 | const android::StringPiece& attr_name, |
| 122 | const android::StringPiece& attr_value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | std::vector<xml::Element*> GetChildElements(); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 124 | std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * A Text (CDATA) XML node. Can not have any children. |
| 129 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 130 | class Text : public BaseNode<Text> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | public: |
| 132 | std::string text; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 134 | std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /** |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 138 | * An XML resource with a source, name, and XML tree. |
| 139 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 140 | class XmlResource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | public: |
| 142 | ResourceFile file; |
Adam Lesinski | ea134e0 | 2017-04-13 12:55:19 -0700 | [diff] [blame] | 143 | |
| 144 | // StringPool must come before the xml::Node. Destructors are called in reverse order, and |
| 145 | // the xml::Node may have StringPool references that need to be destroyed before the StringPool |
| 146 | // is destroyed. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 147 | StringPool string_pool; |
Adam Lesinski | ea134e0 | 2017-04-13 12:55:19 -0700 | [diff] [blame] | 148 | |
| 149 | std::unique_ptr<xml::Node> root; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | /** |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 153 | * Inflates an XML DOM from a text stream, logging errors to the logger. |
| 154 | * Returns the root node on success, or nullptr on failure. |
| 155 | */ |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 156 | std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger. |
| 160 | * Returns the root node on success, or nullptr on failure. |
| 161 | */ |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 162 | std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag, |
| 163 | const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | Element* FindRootElement(XmlResource* doc); |
| 166 | Element* FindRootElement(Node* node); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 167 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 168 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | * A visitor interface for the different XML Node subtypes. This will not |
| 170 | * traverse into |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | * children. Use Visitor for that. |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 172 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 173 | class RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | public: |
| 175 | virtual ~RawVisitor() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | virtual void Visit(Namespace* node) {} |
| 178 | virtual void Visit(Element* node) {} |
| 179 | virtual void Visit(Text* text) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Visitor whose default implementation visits the children nodes of any node. |
| 184 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 185 | class Visitor : public RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | using RawVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | void Visit(Namespace* node) override { VisitChildren(node); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | void Visit(Element* node) override { VisitChildren(node); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | void Visit(Text* text) override { VisitChildren(text); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | void VisitChildren(Node* node) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | for (auto& child : node->children) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | child->Accept(this); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | /** |
| 203 | * An XML DOM visitor that will record the package name for a namespace prefix. |
| 204 | */ |
| 205 | class PackageAwareVisitor : public Visitor, public IPackageDeclStack { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | using Visitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 208 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | void Visit(Namespace* ns) override; |
| 210 | Maybe<ExtractedPackage> TransformPackageAlias( |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 211 | const android::StringPiece& alias, const android::StringPiece& local_package) const override; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | private: |
| 214 | struct PackageDecl { |
| 215 | std::string prefix; |
| 216 | ExtractedPackage package; |
| 217 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | std::vector<PackageDecl> package_decls_; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | // Implementations |
| 223 | |
| 224 | template <typename Derived> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | void BaseNode<Derived>::Accept(RawVisitor* visitor) { |
| 226 | visitor->Visit(static_cast<Derived*>(this)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | template <typename T> |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 230 | class NodeCastImpl : public RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | using RawVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | T* value = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | void Visit(T* v) override { value = v; } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | T* NodeCast(Node* node) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | NodeCastImpl<T> visitor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | node->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | return visitor.value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | } // namespace xml |
| 247 | } // namespace aapt |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 248 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | #endif // AAPT_XML_DOM_H |