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" |
| 25 | |
| 26 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 27 | |
| 28 | #include <istream> |
Elliott Hughes | 51348d2 | 2015-07-21 11:39:21 -0700 | [diff] [blame] | 29 | #include <expat.h> |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace aapt { |
| 35 | namespace xml { |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 37 | struct RawVisitor; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * The type of node. Can be used to downcast to the concrete XML node |
| 41 | * class. |
| 42 | */ |
| 43 | enum class NodeType { |
| 44 | kNamespace, |
| 45 | kElement, |
| 46 | kText, |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * Base class for all XML nodes. |
| 51 | */ |
| 52 | struct Node { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 53 | Node* parent = nullptr; |
| 54 | size_t lineNumber = 0; |
| 55 | size_t columnNumber = 0; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 56 | std::u16string comment; |
| 57 | std::vector<std::unique_ptr<Node>> children; |
| 58 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 59 | void addChild(std::unique_ptr<Node> child); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 60 | virtual void accept(RawVisitor* visitor) = 0; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 61 | virtual ~Node() {} |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Base class that implements the visitor methods for a |
| 66 | * subclass of Node. |
| 67 | */ |
| 68 | template <typename Derived> |
| 69 | struct BaseNode : public Node { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -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 | */ |
| 76 | struct Namespace : public BaseNode<Namespace> { |
| 77 | std::u16string namespacePrefix; |
| 78 | std::u16string namespaceUri; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 79 | }; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 81 | struct AaptAttribute { |
| 82 | ResourceId id; |
| 83 | aapt::Attribute attribute; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * An XML attribute. |
| 88 | */ |
| 89 | struct Attribute { |
| 90 | std::u16string namespaceUri; |
| 91 | std::u16string name; |
| 92 | std::u16string value; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 93 | |
| 94 | Maybe<AaptAttribute> compiledAttribute; |
| 95 | std::unique_ptr<Item> compiledValue; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * An Element XML node. |
| 100 | */ |
| 101 | struct Element : public BaseNode<Element> { |
| 102 | std::u16string namespaceUri; |
| 103 | std::u16string name; |
| 104 | std::vector<Attribute> attributes; |
| 105 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 106 | Attribute* findAttribute(const StringPiece16& ns, const StringPiece16& name); |
| 107 | xml::Element* findChild(const StringPiece16& ns, const StringPiece16& name); |
| 108 | xml::Element* findChildWithAttribute(const StringPiece16& ns, const StringPiece16& name, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 109 | const StringPiece16& attrNs, |
| 110 | const StringPiece16& attrName, |
| 111 | const StringPiece16& attrValue); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 112 | std::vector<xml::Element*> getChildElements(); |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * A Text (CDATA) XML node. Can not have any children. |
| 117 | */ |
| 118 | struct Text : public BaseNode<Text> { |
| 119 | std::u16string text; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /** |
| 123 | * Inflates an XML DOM from a text stream, logging errors to the logger. |
| 124 | * Returns the root node on success, or nullptr on failure. |
| 125 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 126 | 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] | 127 | |
| 128 | /** |
| 129 | * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger. |
| 130 | * Returns the root node on success, or nullptr on failure. |
| 131 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 132 | std::unique_ptr<XmlResource> inflate(const void* data, size_t dataLen, IDiagnostics* diag, |
| 133 | const Source& source); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 136 | * A visitor interface for the different XML Node subtypes. This will not traverse into |
| 137 | * children. Use Visitor for that. |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 138 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 139 | struct RawVisitor { |
| 140 | virtual ~RawVisitor() = default; |
| 141 | |
| 142 | virtual void visit(Namespace* node) {} |
| 143 | virtual void visit(Element* node) {} |
| 144 | virtual void visit(Text* text) {} |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * Visitor whose default implementation visits the children nodes of any node. |
| 149 | */ |
| 150 | struct Visitor : public RawVisitor { |
| 151 | using RawVisitor::visit; |
| 152 | |
| 153 | void visit(Namespace* node) override { |
| 154 | visitChildren(node); |
| 155 | } |
| 156 | |
| 157 | void visit(Element* node) override { |
| 158 | visitChildren(node); |
| 159 | } |
| 160 | |
| 161 | void visit(Text* text) override { |
| 162 | visitChildren(text); |
| 163 | } |
| 164 | |
| 165 | void visitChildren(Node* node) { |
| 166 | for (auto& child : node->children) { |
| 167 | child->accept(this); |
| 168 | } |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | /** |
| 173 | * An XML DOM visitor that will record the package name for a namespace prefix. |
| 174 | */ |
| 175 | class PackageAwareVisitor : public Visitor, public IPackageDeclStack { |
| 176 | private: |
| 177 | struct PackageDecl { |
| 178 | std::u16string prefix; |
| 179 | std::u16string package; |
| 180 | }; |
| 181 | |
| 182 | std::vector<PackageDecl> mPackageDecls; |
| 183 | |
| 184 | public: |
| 185 | using Visitor::visit; |
| 186 | |
| 187 | void visit(Namespace* ns) override { |
| 188 | bool added = false; |
| 189 | { |
| 190 | Maybe<std::u16string> package = util::extractPackageFromNamespace(ns->namespaceUri); |
| 191 | if (package) { |
| 192 | mPackageDecls.push_back(PackageDecl{ ns->namespacePrefix, package.value() }); |
| 193 | added = true; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Visitor::visit(ns); |
| 198 | |
| 199 | if (added) { |
| 200 | mPackageDecls.pop_back(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | Maybe<ResourceName> transformPackage(const ResourceName& name, |
| 205 | const StringPiece16& localPackage) const override { |
| 206 | if (name.package.empty()) { |
| 207 | return ResourceName{ localPackage.toString(), name.type, name.entry }; |
| 208 | } |
| 209 | |
| 210 | const auto rend = mPackageDecls.rend(); |
| 211 | for (auto iter = mPackageDecls.rbegin(); iter != rend; ++iter) { |
| 212 | if (name.package == iter->prefix) { |
| 213 | if (iter->package.empty()) { |
| 214 | return ResourceName{ localPackage.toString(), name.type, name.entry }; |
| 215 | } else { |
| 216 | return ResourceName{ iter->package, name.type, name.entry }; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | return {}; |
| 221 | } |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | // Implementations |
| 225 | |
| 226 | template <typename Derived> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 227 | void BaseNode<Derived>::accept(RawVisitor* visitor) { |
| 228 | visitor->visit(static_cast<Derived*>(this)); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 231 | template <typename T> |
| 232 | struct NodeCastImpl : public RawVisitor { |
| 233 | using RawVisitor::visit; |
| 234 | |
| 235 | T* value = nullptr; |
| 236 | |
| 237 | void visit(T* v) override { |
| 238 | value = v; |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | template <typename T> |
| 243 | T* nodeCast(Node* node) { |
| 244 | NodeCastImpl<T> visitor; |
| 245 | node->accept(&visitor); |
| 246 | return visitor.value; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | } // namespace xml |
| 250 | } // namespace aapt |
| 251 | |
| 252 | #endif // AAPT_XML_DOM_H |