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 <memory> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
| 25 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "Diagnostics.h" |
| 27 | #include "Resource.h" |
| 28 | #include "ResourceValues.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 29 | #include "io/Io.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 | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 36 | class Element; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 37 | class Visitor; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 39 | // Base class for all XML nodes. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 40 | class Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | virtual ~Node() = default; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 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; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 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. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 54 | virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 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; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | }; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | struct AaptAttribute { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 66 | explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {}) |
| 67 | : attribute(attr), id(resid) { |
| 68 | } |
| 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | aapt::Attribute attribute; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 71 | Maybe<ResourceId> id; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 74 | // An XML attribute. |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 75 | struct Attribute { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | std::string namespace_uri; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | std::string name; |
| 78 | std::string value; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | Maybe<AaptAttribute> compiled_attribute; |
| 81 | std::unique_ptr<Item> compiled_value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 84 | // An Element XML node. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 85 | class Element : public Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | public: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 87 | // Ordered namespace prefix declarations. |
| 88 | std::vector<NamespaceDecl> namespace_decls; |
| 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | std::string namespace_uri; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | std::string name; |
| 92 | std::vector<Attribute> attributes; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 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); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 98 | Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 99 | const Attribute* FindAttribute(const android::StringPiece& ns, |
| 100 | const android::StringPiece& name) const; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 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; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 117 | // A Text (CDATA) XML node. Can not have any children. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 118 | class Text : public Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | public: |
| 120 | std::string text; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 122 | std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override; |
| 123 | |
| 124 | void Accept(Visitor* visitor) override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 127 | // An XML resource with a source, name, and XML tree. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 128 | class XmlResource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | public: |
| 130 | ResourceFile file; |
Adam Lesinski | ea134e0 | 2017-04-13 12:55:19 -0700 | [diff] [blame] | 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. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 135 | StringPool string_pool; |
Adam Lesinski | ea134e0 | 2017-04-13 12:55:19 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 137 | std::unique_ptr<xml::Element> root; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 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); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 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. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 146 | std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag, |
| 147 | const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | Element* FindRootElement(Node* node); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 151 | // Visitor whose default implementation visits the children nodes of any node. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 152 | class Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | public: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 154 | virtual ~Visitor() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 156 | virtual void Visit(Element* el) { |
| 157 | VisitChildren(el); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 158 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 160 | virtual void Visit(Text* text) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 161 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 163 | protected: |
| 164 | Visitor() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 166 | void VisitChildren(Element* el) { |
| 167 | for (auto& child : el->children) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | child->Accept(this); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 171 | |
| 172 | virtual void BeforeVisitElement(Element* el) { |
| 173 | } |
| 174 | virtual void AfterVisitElement(Element* el) { |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 179 | |
| 180 | friend class Element; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 183 | // An XML DOM visitor that will record the package name for a namespace prefix. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | class PackageAwareVisitor : public Visitor, public IPackageDeclStack { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | using Visitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame^] | 188 | Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 189 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 190 | protected: |
| 191 | PackageAwareVisitor() = default; |
| 192 | |
| 193 | void BeforeVisitElement(Element* el) override; |
| 194 | void AfterVisitElement(Element* el) override; |
| 195 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | private: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 197 | DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor); |
| 198 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | struct PackageDecl { |
| 200 | std::string prefix; |
| 201 | ExtractedPackage package; |
| 202 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 204 | std::vector<std::vector<PackageDecl>> package_decls_; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 207 | namespace internal { |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 208 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 209 | // Base class that overrides the default behaviour and does not descend into child nodes. |
| 210 | class NodeCastBase : public Visitor { |
| 211 | public: |
| 212 | void Visit(Element* el) override { |
| 213 | } |
| 214 | void Visit(Text* el) override { |
| 215 | } |
| 216 | |
| 217 | protected: |
| 218 | NodeCastBase() = default; |
| 219 | |
| 220 | void BeforeVisitElement(Element* el) override { |
| 221 | } |
| 222 | void AfterVisitElement(Element* el) override { |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | DISALLOW_COPY_AND_ASSIGN(NodeCastBase); |
| 227 | }; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 228 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | template <typename T> |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 230 | class NodeCastImpl : public NodeCastBase { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | public: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 232 | using NodeCastBase::Visit; |
| 233 | |
| 234 | NodeCastImpl() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 236 | T* value = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 238 | void Visit(T* v) override { |
| 239 | value = v; |
| 240 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 241 | |
| 242 | private: |
| 243 | DISALLOW_COPY_AND_ASSIGN(NodeCastImpl); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 246 | } // namespace internal |
| 247 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 248 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | T* NodeCast(Node* node) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 250 | internal::NodeCastImpl<T> visitor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | node->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | return visitor.value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 255 | } // namespace xml |
| 256 | } // namespace aapt |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 257 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | #endif // AAPT_XML_DOM_H |