2015-06-03 14:54:23 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AAPT_XML_DOM_H
|
|
|
|
#define AAPT_XML_DOM_H
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
#include <istream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
#include "androidfw/StringPiece.h"
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
#include "Diagnostics.h"
|
|
|
|
#include "Resource.h"
|
|
|
|
#include "ResourceValues.h"
|
|
|
|
#include "util/Util.h"
|
2015-11-16 17:35:44 -08:00
|
|
|
#include "xml/XmlUtil.h"
|
2015-06-03 14:54:23 -07:00
|
|
|
|
|
|
|
namespace aapt {
|
|
|
|
namespace xml {
|
|
|
|
|
2016-08-25 12:26:56 -07:00
|
|
|
class RawVisitor;
|
2015-06-03 14:54:23 -07:00
|
|
|
|
2017-05-17 19:28:38 -07:00
|
|
|
class Element;
|
|
|
|
|
2015-06-03 14:54:23 -07:00
|
|
|
/**
|
|
|
|
* Base class for all XML nodes.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class Node {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
Node* parent = nullptr;
|
2016-10-21 17:56:45 -07:00
|
|
|
size_t line_number = 0;
|
|
|
|
size_t column_number = 0;
|
2016-10-19 12:18:14 -07:00
|
|
|
std::string comment;
|
|
|
|
std::vector<std::unique_ptr<Node>> children;
|
|
|
|
|
|
|
|
virtual ~Node() = default;
|
|
|
|
|
2016-10-27 16:31:58 -07:00
|
|
|
void AppendChild(std::unique_ptr<Node> child);
|
|
|
|
void InsertChild(size_t index, std::unique_ptr<Node> child);
|
2016-10-21 17:56:45 -07:00
|
|
|
virtual void Accept(RawVisitor* visitor) = 0;
|
2017-05-17 19:28:38 -07:00
|
|
|
|
|
|
|
using ElementCloneFunc = std::function<void(const Element&, Element*)>;
|
|
|
|
|
|
|
|
// Clones the Node subtree, using the given function to decide how to clone an Element.
|
|
|
|
virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) = 0;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class that implements the visitor methods for a
|
|
|
|
* subclass of Node.
|
|
|
|
*/
|
|
|
|
template <typename Derived>
|
2016-08-25 12:26:56 -07:00
|
|
|
class BaseNode : public Node {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
virtual void Accept(RawVisitor* visitor) override;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Namespace XML node. Can only have one child.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class Namespace : public BaseNode<Namespace> {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string namespace_prefix;
|
|
|
|
std::string namespace_uri;
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-05-17 19:28:38 -07:00
|
|
|
std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
2015-06-03 14:54:23 -07:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
struct AaptAttribute {
|
2017-05-17 19:28:38 -07:00
|
|
|
explicit AaptAttribute(const ::aapt::Attribute& attr, const Maybe<ResourceId>& resid = {})
|
|
|
|
: attribute(attr), id(resid) {
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
aapt::Attribute attribute;
|
2017-05-17 19:28:38 -07:00
|
|
|
Maybe<ResourceId> id;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An XML attribute.
|
|
|
|
*/
|
|
|
|
struct Attribute {
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string namespace_uri;
|
2016-10-19 12:18:14 -07:00
|
|
|
std::string name;
|
|
|
|
std::string value;
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Maybe<AaptAttribute> compiled_attribute;
|
|
|
|
std::unique_ptr<Item> compiled_value;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An Element XML node.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class Element : public BaseNode<Element> {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string namespace_uri;
|
2016-10-19 12:18:14 -07:00
|
|
|
std::string name;
|
|
|
|
std::vector<Attribute> attributes;
|
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
|
2017-05-17 19:28:38 -07:00
|
|
|
const Attribute* FindAttribute(const android::StringPiece& ns,
|
|
|
|
const android::StringPiece& name) const;
|
2017-01-16 15:07:21 -08:00
|
|
|
xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
|
|
|
|
xml::Element* FindChildWithAttribute(const android::StringPiece& ns,
|
|
|
|
const android::StringPiece& name,
|
|
|
|
const android::StringPiece& attr_ns,
|
|
|
|
const android::StringPiece& attr_name,
|
|
|
|
const android::StringPiece& attr_value);
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<xml::Element*> GetChildElements();
|
2017-05-17 19:28:38 -07:00
|
|
|
std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Text (CDATA) XML node. Can not have any children.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class Text : public BaseNode<Text> {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
std::string text;
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-05-17 19:28:38 -07:00
|
|
|
std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
2015-11-16 17:35:44 -08:00
|
|
|
/**
|
|
|
|
* An XML resource with a source, name, and XML tree.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class XmlResource {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
ResourceFile file;
|
2017-04-13 12:55:19 -07:00
|
|
|
|
|
|
|
// StringPool must come before the xml::Node. Destructors are called in reverse order, and
|
|
|
|
// the xml::Node may have StringPool references that need to be destroyed before the StringPool
|
|
|
|
// is destroyed.
|
2017-04-03 18:12:45 -07:00
|
|
|
StringPool string_pool;
|
2017-04-13 12:55:19 -07:00
|
|
|
|
|
|
|
std::unique_ptr<xml::Node> root;
|
2015-11-16 17:35:44 -08:00
|
|
|
};
|
|
|
|
|
2015-06-03 14:54:23 -07:00
|
|
|
/**
|
|
|
|
* Inflates an XML DOM from a text stream, logging errors to the logger.
|
|
|
|
* Returns the root node on success, or nullptr on failure.
|
|
|
|
*/
|
2017-04-03 18:12:45 -07:00
|
|
|
std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source);
|
2015-06-03 14:54:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
|
|
|
|
* Returns the root node on success, or nullptr on failure.
|
|
|
|
*/
|
2017-04-03 18:12:45 -07:00
|
|
|
std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
|
|
|
|
const Source& source);
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Element* FindRootElement(XmlResource* doc);
|
|
|
|
Element* FindRootElement(Node* node);
|
2015-10-21 14:42:43 -07:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* A visitor interface for the different XML Node subtypes. This will not
|
|
|
|
* traverse into
|
2015-08-14 14:26:04 -07:00
|
|
|
* children. Use Visitor for that.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class RawVisitor {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
virtual ~RawVisitor() = default;
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
virtual void Visit(Namespace* node) {}
|
|
|
|
virtual void Visit(Element* node) {}
|
|
|
|
virtual void Visit(Text* text) {}
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visitor whose default implementation visits the children nodes of any node.
|
|
|
|
*/
|
2016-08-25 12:26:56 -07:00
|
|
|
class Visitor : public RawVisitor {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
using RawVisitor::Visit;
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Visit(Namespace* node) override { VisitChildren(node); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Visit(Element* node) override { VisitChildren(node); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Visit(Text* text) override { VisitChildren(text); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void VisitChildren(Node* node) {
|
2016-10-19 12:18:14 -07:00
|
|
|
for (auto& child : node->children) {
|
2016-10-21 17:56:45 -07:00
|
|
|
child->Accept(this);
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
2015-06-03 14:54:23 -07:00
|
|
|
|
|
|
|
/**
|
2015-08-14 14:26:04 -07:00
|
|
|
* An XML DOM visitor that will record the package name for a namespace prefix.
|
2015-06-03 14:54:23 -07:00
|
|
|
*/
|
2015-08-14 14:26:04 -07:00
|
|
|
class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
using Visitor::Visit;
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Visit(Namespace* ns) override;
|
|
|
|
Maybe<ExtractedPackage> TransformPackageAlias(
|
2017-01-16 15:07:21 -08:00
|
|
|
const android::StringPiece& alias, const android::StringPiece& local_package) const override;
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
private:
|
|
|
|
struct PackageDecl {
|
|
|
|
std::string prefix;
|
|
|
|
ExtractedPackage package;
|
|
|
|
};
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<PackageDecl> package_decls_;
|
2015-06-03 14:54:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Implementations
|
|
|
|
|
|
|
|
template <typename Derived>
|
2016-10-21 17:56:45 -07:00
|
|
|
void BaseNode<Derived>::Accept(RawVisitor* visitor) {
|
|
|
|
visitor->Visit(static_cast<Derived*>(this));
|
2015-06-03 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
template <typename T>
|
2016-08-25 12:26:56 -07:00
|
|
|
class NodeCastImpl : public RawVisitor {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2016-10-21 17:56:45 -07:00
|
|
|
using RawVisitor::Visit;
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
T* value = nullptr;
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Visit(T* v) override { value = v; }
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-10-21 17:56:45 -07:00
|
|
|
T* NodeCast(Node* node) {
|
2016-10-19 12:18:14 -07:00
|
|
|
NodeCastImpl<T> visitor;
|
2016-10-21 17:56:45 -07:00
|
|
|
node->Accept(&visitor);
|
2016-10-19 12:18:14 -07:00
|
|
|
return visitor.value;
|
2015-06-03 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
} // namespace xml
|
|
|
|
} // namespace aapt
|
2015-06-03 14:54:23 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
#endif // AAPT_XML_DOM_H
|