Merge "AAPT2: Strip dedicated tools namespace from XML"

This commit is contained in:
Alexandria Cornwall
2016-08-03 20:03:12 +00:00
committed by Android (Google) Code Review
3 changed files with 39 additions and 3 deletions

View File

@@ -88,9 +88,14 @@ struct XmlFlattenerVisitor : public xml::Visitor {
}
void visit(xml::Namespace* node) override {
writeNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
xml::Visitor::visit(node);
writeNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
if (node->namespaceUri == xml::kSchemaTools) {
// Skip dedicated tools namespace.
xml::Visitor::visit(node);
} else {
writeNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
xml::Visitor::visit(node);
writeNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
}
}
void visit(xml::Text* node) override {
@@ -183,6 +188,9 @@ struct XmlFlattenerVisitor : public xml::Visitor {
continue;
}
}
if (attr.namespaceUri == xml::kSchemaTools) {
continue;
}
mFilteredAttrs.push_back(&attr);
}

View File

@@ -167,6 +167,33 @@ TEST_F(XmlFlattenerTest, FlattenCompiledXmlAndStripSdk21) {
EXPECT_EQ(uint32_t(0x010103b3), tree.getAttributeNameResID(0));
}
TEST_F(XmlFlattenerTest, FlattenCompiledXmlAndStripOnlyTools) {
std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
<View xmlns:tools="http://schemas.android.com/tools"
xmlns:foo="http://schemas.android.com/foo"
foo:bar="Foo"
tools:ignore="MissingTranslation"/>)EOF");
android::ResXMLTree tree;
ASSERT_TRUE(flatten(doc.get(), &tree));
ASSERT_EQ(tree.next(), android::ResXMLTree::START_NAMESPACE);
size_t len;
const char16_t* namespacePrefix = tree.getNamespacePrefix(&len);
EXPECT_EQ(StringPiece16(namespacePrefix, len), u"foo");
const char16_t* namespaceUri = tree.getNamespaceUri(&len);
ASSERT_EQ(StringPiece16(namespaceUri, len), u"http://schemas.android.com/foo");
ASSERT_EQ(tree.next(), android::ResXMLTree::START_TAG);
EXPECT_EQ(
tree.indexOfAttribute("http://schemas.android.com/tools", "ignore"),
android::NAME_NOT_FOUND);
EXPECT_GE(tree.indexOfAttribute("http://schemas.android.com/foo", "bar"), 0);
}
TEST_F(XmlFlattenerTest, AssignSpecialAttributeIndices) {
std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
<View xmlns:android="http://schemas.android.com/apk/res/android"

View File

@@ -29,6 +29,7 @@ constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/";
constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/";
constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android";
constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
/**
* Result of extracting a package name from a namespace URI declaration.