| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_XML_DOM_H |
| 18 |
#define AAPT_XML_DOM_H |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
#include "androidfw/StringPiece.h" |
| 25 |
|
| 26 |
#include "Diagnostics.h" |
| 27 |
#include "Resource.h" |
| 28 |
#include "ResourceValues.h" |
| 29 |
#include "io/Io.h" |
| 30 |
#include "util/Util.h" |
| 31 |
#include "xml/XmlUtil.h" |
| 32 |
|
| 33 |
namespace aapt { |
| 34 |
namespace xml { |
| 35 |
|
| 36 |
class Element; |
| 37 |
class Visitor; |
| 38 |
class ConstVisitor; |
| 39 |
|
| 40 |
|
| 41 |
class Node { |
| 42 |
public: |
| 43 |
virtual ~Node() = default; |
| 44 |
|
| 45 |
Element* parent = nullptr; |
| 46 |
size_t line_number = 0u; |
| 47 |
size_t column_number = 0u; |
| 48 |
std::string comment; |
| 49 |
|
| 50 |
virtual void Accept(Visitor* visitor) = 0; |
| 51 |
virtual void Accept(ConstVisitor* visitor) const = 0; |
| 52 |
|
| 53 |
using ElementCloneFunc = std::function<void(const Element&, Element*)>; |
| 54 |
|
| 55 |
|
| 56 |
virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const = 0; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
struct NamespaceDecl { |
| 61 |
std::string prefix; |
| 62 |
std::string uri; |
| 63 |
size_t line_number = 0u; |
| 64 |
size_t column_number = 0u; |
| 65 |
}; |
| 66 |
|
| 67 |
struct AaptAttribute { |
| 68 |
explicit AaptAttribute(const ::aapt::Attribute& attr, const std::optional<ResourceId>& resid = {}) |
| 69 |
: attribute(attr), id(resid) { |
| 70 |
} |
| 71 |
|
| 72 |
aapt::Attribute attribute; |
| 73 |
std::optional<ResourceId> id; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
struct Attribute { |
| 78 |
std::string namespace_uri; |
| 79 |
std::string name; |
| 80 |
std::string value; |
| 81 |
|
| 82 |
std::optional<AaptAttribute> compiled_attribute; |
| 83 |
std::unique_ptr<Item> compiled_value; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
class Element : public Node { |
| 88 |
public: |
| 89 |
|
| 90 |
std::vector<NamespaceDecl> namespace_decls; |
| 91 |
|
| 92 |
std::string namespace_uri; |
| 93 |
std::string name; |
| 94 |
std::vector<Attribute> attributes; |
| 95 |
std::vector<std::unique_ptr<Node>> children; |
| 96 |
|
| 97 |
void AppendChild(std::unique_ptr<Node> child); |
| 98 |
void InsertChild(size_t index, std::unique_ptr<Node> child); |
| 99 |
|
| 100 |
Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name); |
| 101 |
const Attribute* FindAttribute(const android::StringPiece& ns, |
| 102 |
const android::StringPiece& name) const; |
| 103 |
Attribute* FindOrCreateAttribute(const android::StringPiece& ns, |
| 104 |
const android::StringPiece& name); |
| 105 |
void RemoveAttribute(const android::StringPiece& ns, |
| 106 |
const android::StringPiece& name); |
| 107 |
|
| 108 |
Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name); |
| 109 |
const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const; |
| 110 |
|
| 111 |
Element* FindChildWithAttribute(const android::StringPiece& ns, const android::StringPiece& name, |
| 112 |
const android::StringPiece& attr_ns, |
| 113 |
const android::StringPiece& attr_name, |
| 114 |
const android::StringPiece& attr_value); |
| 115 |
|
| 116 |
const Element* FindChildWithAttribute(const android::StringPiece& ns, |
| 117 |
const android::StringPiece& name, |
| 118 |
const android::StringPiece& attr_ns, |
| 119 |
const android::StringPiece& attr_name, |
| 120 |
const android::StringPiece& attr_value) const; |
| 121 |
|
| 122 |
std::vector<Element*> GetChildElements(); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
std::unique_ptr<Element> CloneElement(const ElementCloneFunc& el_cloner) const; |
| 127 |
|
| 128 |
std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override; |
| 129 |
|
| 130 |
void Accept(Visitor* visitor) override; |
| 131 |
void Accept(ConstVisitor* visitor) const override; |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
class Text : public Node { |
| 136 |
public: |
| 137 |
std::string text; |
| 138 |
|
| 139 |
std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) const override; |
| 140 |
|
| 141 |
void Accept(Visitor* visitor) override; |
| 142 |
void Accept(ConstVisitor* visitor) const override; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
class XmlResource { |
| 147 |
public: |
| 148 |
ResourceFile file; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
StringPool string_pool; |
| 154 |
|
| 155 |
std::unique_ptr<xml::Element> root; |
| 156 |
|
| 157 |
std::unique_ptr<XmlResource> Clone() const; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source); |
| 162 |
|
| 163 |
|
| 164 |
std::unique_ptr<XmlResource> Inflate(const void* data, size_t len, |
| 165 |
std::string* out_error = nullptr); |
| 166 |
|
| 167 |
Element* FindRootElement(Node* node); |
| 168 |
|
| 169 |
|
| 170 |
class Visitor { |
| 171 |
public: |
| 172 |
virtual ~Visitor() = default; |
| 173 |
|
| 174 |
virtual void Visit(Element* el) { |
| 175 |
VisitChildren(el); |
| 176 |
} |
| 177 |
|
| 178 |
virtual void Visit(Text* text) { |
| 179 |
} |
| 180 |
|
| 181 |
protected: |
| 182 |
Visitor() = default; |
| 183 |
|
| 184 |
void VisitChildren(Element* el) { |
| 185 |
for (auto& child : el->children) { |
| 186 |
child->Accept(this); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
virtual void BeforeVisitElement(Element* el) { |
| 191 |
} |
| 192 |
virtual void AfterVisitElement(Element* el) { |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 197 |
|
| 198 |
friend class Element; |
| 199 |
}; |
| 200 |
|
| 201 |
class ConstVisitor { |
| 202 |
public: |
| 203 |
virtual ~ConstVisitor() = default; |
| 204 |
|
| 205 |
virtual void Visit(const Element* el) { |
| 206 |
VisitChildren(el); |
| 207 |
} |
| 208 |
|
| 209 |
virtual void Visit(const Text* text) { |
| 210 |
} |
| 211 |
|
| 212 |
protected: |
| 213 |
ConstVisitor() = default; |
| 214 |
|
| 215 |
void VisitChildren(const Element* el) { |
| 216 |
for (const auto& child : el->children) { |
| 217 |
child->Accept(this); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
virtual void BeforeVisitElement(const Element* el) { |
| 222 |
} |
| 223 |
|
| 224 |
virtual void AfterVisitElement(const Element* el) { |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
DISALLOW_COPY_AND_ASSIGN(ConstVisitor); |
| 229 |
|
| 230 |
friend class Element; |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
class PackageAwareVisitor : public Visitor, public IPackageDeclStack { |
| 235 |
public: |
| 236 |
using Visitor::Visit; |
| 237 |
|
| 238 |
std::optional<ExtractedPackage> TransformPackageAlias( |
| 239 |
const android::StringPiece& alias) const override; |
| 240 |
|
| 241 |
protected: |
| 242 |
PackageAwareVisitor() = default; |
| 243 |
|
| 244 |
void BeforeVisitElement(Element* el) override; |
| 245 |
void AfterVisitElement(Element* el) override; |
| 246 |
|
| 247 |
private: |
| 248 |
DISALLOW_COPY_AND_ASSIGN(PackageAwareVisitor); |
| 249 |
|
| 250 |
struct PackageDecl { |
| 251 |
std::string prefix; |
| 252 |
ExtractedPackage package; |
| 253 |
}; |
| 254 |
|
| 255 |
std::vector<std::vector<PackageDecl>> package_decls_; |
| 256 |
}; |
| 257 |
|
| 258 |
namespace internal { |
| 259 |
|
| 260 |
|
| 261 |
class NodeCastBase : public ConstVisitor { |
| 262 |
public: |
| 263 |
void Visit(const Element* el) override { |
| 264 |
} |
| 265 |
void Visit(const Text* el) override { |
| 266 |
} |
| 267 |
|
| 268 |
protected: |
| 269 |
NodeCastBase() = default; |
| 270 |
|
| 271 |
void BeforeVisitElement(const Element* el) override { |
| 272 |
} |
| 273 |
void AfterVisitElement(const Element* el) override { |
| 274 |
} |
| 275 |
|
| 276 |
private: |
| 277 |
DISALLOW_COPY_AND_ASSIGN(NodeCastBase); |
| 278 |
}; |
| 279 |
|
| 280 |
template <typename T> |
| 281 |
class NodeCastImpl : public NodeCastBase { |
| 282 |
public: |
| 283 |
using NodeCastBase::Visit; |
| 284 |
|
| 285 |
NodeCastImpl() = default; |
| 286 |
|
| 287 |
const T* value = nullptr; |
| 288 |
|
| 289 |
void Visit(const T* v) override { |
| 290 |
value = v; |
| 291 |
} |
| 292 |
|
| 293 |
private: |
| 294 |
DISALLOW_COPY_AND_ASSIGN(NodeCastImpl); |
| 295 |
}; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
template <typename T> |
| 300 |
const T* NodeCast(const Node* node) { |
| 301 |
internal::NodeCastImpl<T> visitor; |
| 302 |
node->Accept(&visitor); |
| 303 |
return visitor.value; |
| 304 |
} |
| 305 |
|
| 306 |
template <typename T> |
| 307 |
T* NodeCast(Node* node) { |
| 308 |
return const_cast<T*>(NodeCast<T>(static_cast<const T*>(node))); |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
#endif |