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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "Diagnostics.h" |
| 21 | #include "Resource.h" |
| 22 | #include "ResourceValues.h" |
| 23 | #include "util/StringPiece.h" |
| 24 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | #include "xml/XmlUtil.h" |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 26 | |
| 27 | #include <istream> |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 28 | #include <memory> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace aapt { |
| 33 | namespace xml { |
| 34 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 35 | class RawVisitor; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 38 | * Base class for all XML nodes. |
| 39 | */ |
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: |
| 42 | Node* parent = nullptr; |
| 43 | size_t lineNumber = 0; |
| 44 | size_t columnNumber = 0; |
| 45 | std::string comment; |
| 46 | std::vector<std::unique_ptr<Node>> children; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | virtual ~Node() = default; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | void addChild(std::unique_ptr<Node> child); |
| 51 | virtual void accept(RawVisitor* visitor) = 0; |
| 52 | virtual std::unique_ptr<Node> clone() = 0; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Base class that implements the visitor methods for a |
| 57 | * subclass of Node. |
| 58 | */ |
| 59 | template <typename Derived> |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 60 | class BaseNode : public Node { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | public: |
| 62 | virtual void accept(RawVisitor* visitor) override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * A Namespace XML node. Can only have one child. |
| 67 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 68 | class Namespace : public BaseNode<Namespace> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | public: |
| 70 | std::string namespacePrefix; |
| 71 | std::string namespaceUri; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | std::unique_ptr<Node> clone() override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | }; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | struct AaptAttribute { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | Maybe<ResourceId> id; |
| 78 | aapt::Attribute attribute; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * An XML attribute. |
| 83 | */ |
| 84 | struct Attribute { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | std::string namespaceUri; |
| 86 | std::string name; |
| 87 | std::string value; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | Maybe<AaptAttribute> compiledAttribute; |
| 90 | std::unique_ptr<Item> compiledValue; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * An Element XML node. |
| 95 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 96 | class Element : public BaseNode<Element> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | public: |
| 98 | std::string namespaceUri; |
| 99 | std::string name; |
| 100 | std::vector<Attribute> attributes; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | Attribute* findAttribute(const StringPiece& ns, const StringPiece& name); |
| 103 | xml::Element* findChild(const StringPiece& ns, const StringPiece& name); |
| 104 | xml::Element* findChildWithAttribute(const StringPiece& ns, |
| 105 | const StringPiece& name, |
| 106 | const StringPiece& attrNs, |
| 107 | const StringPiece& attrName, |
| 108 | const StringPiece& attrValue); |
| 109 | std::vector<xml::Element*> getChildElements(); |
| 110 | std::unique_ptr<Node> clone() override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * A Text (CDATA) XML node. Can not have any children. |
| 115 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 116 | class Text : public BaseNode<Text> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | public: |
| 118 | std::string text; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | std::unique_ptr<Node> clone() override; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /** |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 124 | * An XML resource with a source, name, and XML tree. |
| 125 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 126 | class XmlResource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | public: |
| 128 | ResourceFile file; |
| 129 | std::unique_ptr<xml::Node> root; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /** |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 133 | * Inflates an XML DOM from a text stream, logging errors to the logger. |
| 134 | * Returns the root node on success, or nullptr on failure. |
| 135 | */ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 136 | std::unique_ptr<XmlResource> inflate(std::istream* in, IDiagnostics* diag, |
| 137 | const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger. |
| 141 | * Returns the root node on success, or nullptr on failure. |
| 142 | */ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | std::unique_ptr<XmlResource> inflate(const void* data, size_t dataLen, |
| 144 | IDiagnostics* diag, const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 146 | Element* findRootElement(XmlResource* doc); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 147 | Element* findRootElement(Node* node); |
| 148 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 149 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | * A visitor interface for the different XML Node subtypes. This will not |
| 151 | * traverse into |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | * children. Use Visitor for that. |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 153 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 154 | class RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | public: |
| 156 | virtual ~RawVisitor() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | virtual void visit(Namespace* node) {} |
| 159 | virtual void visit(Element* node) {} |
| 160 | virtual void visit(Text* text) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /** |
| 164 | * Visitor whose default implementation visits the children nodes of any node. |
| 165 | */ |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 166 | class Visitor : public RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | public: |
| 168 | using RawVisitor::visit; |
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 | void visit(Namespace* node) override { visitChildren(node); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 172 | void visit(Element* node) override { visitChildren(node); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | void visit(Text* text) override { visitChildren(text); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 175 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | void visitChildren(Node* node) { |
| 177 | for (auto& child : node->children) { |
| 178 | child->accept(this); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 179 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * An XML DOM visitor that will record the package name for a namespace prefix. |
| 185 | */ |
| 186 | class PackageAwareVisitor : public Visitor, public IPackageDeclStack { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | public: |
| 188 | using Visitor::visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 189 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | void visit(Namespace* ns) override; |
| 191 | Maybe<ExtractedPackage> transformPackageAlias( |
| 192 | const StringPiece& alias, const StringPiece& localPackage) const override; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | private: |
| 195 | struct PackageDecl { |
| 196 | std::string prefix; |
| 197 | ExtractedPackage package; |
| 198 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 199 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | std::vector<PackageDecl> mPackageDecls; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | // Implementations |
| 204 | |
| 205 | template <typename Derived> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 206 | void BaseNode<Derived>::accept(RawVisitor* visitor) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | visitor->visit(static_cast<Derived*>(this)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 210 | template <typename T> |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 211 | class NodeCastImpl : public RawVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | public: |
| 213 | using RawVisitor::visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | T* value = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | void visit(T* v) override { value = v; } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | template <typename T> |
| 221 | T* nodeCast(Node* node) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | NodeCastImpl<T> visitor; |
| 223 | node->accept(&visitor); |
| 224 | return visitor.value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | } // namespace xml |
| 228 | } // namespace aapt |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 229 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | #endif // AAPT_XML_DOM_H |