AAPT2: Add Inline Complex XML support

See: https://developer.android.com/guide/topics/resources/complex-xml-resources.html

Change-Id: I8274c85e25cabf90423141c228697e873167d136
diff --git a/tools/aapt2/compile/Compile.cpp b/tools/aapt2/compile/Compile.cpp
index 39e4489..e0f37ec 100644
--- a/tools/aapt2/compile/Compile.cpp
+++ b/tools/aapt2/compile/Compile.cpp
@@ -20,6 +20,7 @@
 #include "ResourceParser.h"
 #include "ResourceTable.h"
 #include "compile/IdAssigner.h"
+#include "compile/InlineXmlFormatParser.h"
 #include "compile/Png.h"
 #include "compile/PseudolocaleGenerator.h"
 #include "compile/XmlIdCollector.h"
@@ -39,6 +40,9 @@
 #include <fstream>
 #include <string>
 
+using google::protobuf::io::CopyingOutputStreamAdaptor;
+using google::protobuf::io::ZeroCopyOutputStream;
+
 namespace aapt {
 
 struct ResourcePathData {
@@ -238,13 +242,14 @@
         return false;
     }
 
-    std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(&table);
-
-    // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
+    // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry().
     {
-        google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
+        // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream
+        // interface.
+        CopyingOutputStreamAdaptor copyingAdaptor(writer);
 
-        if (!pbTable->SerializeToZeroCopyStream(&adaptor)) {
+        std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(&table);
+        if (!pbTable->SerializeToZeroCopyStream(&copyingAdaptor)) {
             context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to write");
             return false;
         }
@@ -266,21 +271,23 @@
         return false;
     }
 
-    // Create the header.
-    std::unique_ptr<pb::CompiledFile> pbCompiledFile = serializeCompiledFileToPb(file);
-
+    // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry().
     {
-        // The stream must be destroyed before we finish the entry, or else
-        // some data won't be flushed.
         // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream
         // interface.
-        google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
-        CompiledFileOutputStream outputStream(&adaptor, pbCompiledFile.get());
-        for (const BigBuffer::Block& block : buffer) {
-            if (!outputStream.Write(block.buffer.get(), block.size)) {
-                diag->error(DiagMessage(outputPath) << "failed to write data");
-                return false;
-            }
+        CopyingOutputStreamAdaptor copyingAdaptor(writer);
+        CompiledFileOutputStream outputStream(&copyingAdaptor);
+
+        // Number of CompiledFiles.
+        outputStream.WriteLittleEndian32(1);
+
+        std::unique_ptr<pb::CompiledFile> compiledFile = serializeCompiledFileToPb(file);
+        outputStream.WriteCompiledFile(compiledFile.get());
+        outputStream.WriteData(&buffer);
+
+        if (outputStream.HadError()) {
+            diag->error(DiagMessage(outputPath) << "failed to write data");
+            return false;
         }
     }
 
@@ -300,17 +307,21 @@
         return false;
     }
 
-    // Create the header.
-    std::unique_ptr<pb::CompiledFile> pbCompiledFile = serializeCompiledFileToPb(file);
-
+    // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry().
     {
-        // The stream must be destroyed before we finish the entry, or else
-        // some data won't be flushed.
         // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream
         // interface.
-        google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
-        CompiledFileOutputStream outputStream(&adaptor, pbCompiledFile.get());
-        if (!outputStream.Write(map.getDataPtr(), map.getDataLength())) {
+        CopyingOutputStreamAdaptor copyingAdaptor(writer);
+        CompiledFileOutputStream outputStream(&copyingAdaptor);
+
+        // Number of CompiledFiles.
+        outputStream.WriteLittleEndian32(1);
+
+        std::unique_ptr<pb::CompiledFile> compiledFile = serializeCompiledFileToPb(file);
+        outputStream.WriteCompiledFile(compiledFile.get());
+        outputStream.WriteData(map.getDataPtr(), map.getDataLength());
+
+        if (outputStream.HadError()) {
             diag->error(DiagMessage(outputPath) << "failed to write data");
             return false;
         }
@@ -323,6 +334,28 @@
     return true;
 }
 
+static bool flattenXmlToOutStream(IAaptContext* context, const StringPiece& outputPath,
+                                  xml::XmlResource* xmlRes,
+                                  CompiledFileOutputStream* out) {
+    BigBuffer buffer(1024);
+    XmlFlattenerOptions xmlFlattenerOptions;
+    xmlFlattenerOptions.keepRawValues = true;
+    XmlFlattener flattener(&buffer, xmlFlattenerOptions);
+    if (!flattener.consume(context, xmlRes)) {
+        return false;
+    }
+
+    std::unique_ptr<pb::CompiledFile> pbCompiledFile = serializeCompiledFileToPb(xmlRes->file);
+    out->WriteCompiledFile(pbCompiledFile.get());
+    out->WriteData(&buffer);
+
+    if (out->HadError()) {
+        context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to write data");
+        return false;
+    }
+    return true;
+}
+
 static bool compileXml(IAaptContext* context, const CompileOptions& options,
                        const ResourcePathData& pathData, IArchiveWriter* writer,
                        const std::string& outputPath) {
@@ -344,26 +377,55 @@
         return false;
     }
 
+    xmlRes->file.name = ResourceName({}, *parseResourceType(pathData.resourceDir), pathData.name);
+    xmlRes->file.config = pathData.config;
+    xmlRes->file.source = pathData.source;
+
     // Collect IDs that are defined here.
     XmlIdCollector collector;
     if (!collector.consume(context, xmlRes.get())) {
         return false;
     }
 
-    xmlRes->file.name = ResourceName({}, *parseResourceType(pathData.resourceDir), pathData.name);
-    xmlRes->file.config = pathData.config;
-    xmlRes->file.source = pathData.source;
-
-    BigBuffer buffer(1024);
-    XmlFlattenerOptions xmlFlattenerOptions;
-    xmlFlattenerOptions.keepRawValues = true;
-    XmlFlattener flattener(&buffer, xmlFlattenerOptions);
-    if (!flattener.consume(context, xmlRes.get())) {
+    // Look for and process any <aapt:attr> tags and create sub-documents.
+    InlineXmlFormatParser inlineXmlFormatParser;
+    if (!inlineXmlFormatParser.consume(context, xmlRes.get())) {
         return false;
     }
 
-    if (!writeHeaderAndBufferToWriter(outputPath, xmlRes->file, buffer, writer,
-                                      context->getDiagnostics())) {
+    // Start the entry so we can write the header.
+    if (!writer->startEntry(outputPath, 0)) {
+        context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to open file");
+        return false;
+    }
+
+    // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry().
+    {
+        // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream
+        // interface.
+        CopyingOutputStreamAdaptor copyingAdaptor(writer);
+        CompiledFileOutputStream outputStream(&copyingAdaptor);
+
+        std::vector<std::unique_ptr<xml::XmlResource>>& inlineDocuments =
+                inlineXmlFormatParser.getExtractedInlineXmlDocuments();
+
+        // Number of CompiledFiles.
+        outputStream.WriteLittleEndian32(1 + inlineDocuments.size());
+
+        if (!flattenXmlToOutStream(context, outputPath, xmlRes.get(), &outputStream)) {
+            return false;
+        }
+
+        for (auto& inlineXmlDoc : inlineDocuments) {
+            if (!flattenXmlToOutStream(context, outputPath, inlineXmlDoc.get(), &outputStream)) {
+                return false;
+            }
+        }
+    }
+
+    if (!writer->finishEntry()) {
+        context->getDiagnostics()->error(DiagMessage(outputPath)
+                                         << "failed to finish writing data");
         return false;
     }
     return true;
diff --git a/tools/aapt2/compile/InlineXmlFormatParser.cpp b/tools/aapt2/compile/InlineXmlFormatParser.cpp
new file mode 100644
index 0000000..f965bff
--- /dev/null
+++ b/tools/aapt2/compile/InlineXmlFormatParser.cpp
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "Debug.h"
+#include "ResourceUtils.h"
+#include "compile/InlineXmlFormatParser.h"
+#include "util/Util.h"
+#include "xml/XmlDom.h"
+#include "xml/XmlUtil.h"
+
+#include <android-base/macros.h>
+#include <sstream>
+#include <string>
+
+namespace aapt {
+
+namespace {
+
+/**
+ * XML Visitor that will find all <aapt:attr> elements for extraction.
+ */
+class Visitor : public xml::PackageAwareVisitor {
+public:
+    using xml::PackageAwareVisitor::visit;
+
+    struct InlineDeclaration {
+        xml::Element* el;
+        std::string attrNamespaceUri;
+        std::string attrName;
+    };
+
+    explicit Visitor(IAaptContext* context, xml::XmlResource* xmlResource) :
+            mContext(context), mXmlResource(xmlResource) {
+    }
+
+    void visit(xml::Element* el) override {
+        if (el->namespaceUri != xml::kSchemaAapt || el->name != "attr") {
+            xml::PackageAwareVisitor::visit(el);
+            return;
+        }
+
+        const Source& src = mXmlResource->file.source.withLine(el->lineNumber);
+
+        xml::Attribute* attr = el->findAttribute({}, "name");
+        if (!attr) {
+            mContext->getDiagnostics()->error(DiagMessage(src) << "missing 'name' attribute");
+            mError = true;
+            return;
+        }
+
+        Maybe<Reference> ref = ResourceUtils::parseXmlAttributeName(attr->value);
+        if (!ref) {
+            mContext->getDiagnostics()->error(DiagMessage(src) << "invalid XML attribute '"
+                                              << attr->value << "'");
+            mError = true;
+            return;
+        }
+
+        const ResourceName& name = ref.value().name.value();
+
+        // Use an empty string for the compilation package because we don't want to default to
+        // the local package if the user specified name="style" or something. This should just
+        // be the default namespace.
+        Maybe<xml::ExtractedPackage> maybePkg = transformPackageAlias(name.package, {});
+        if (!maybePkg) {
+            mContext->getDiagnostics()->error(DiagMessage(src) << "invalid namespace prefix '"
+                                              << name.package << "'");
+            mError = true;
+            return;
+        }
+
+        const xml::ExtractedPackage& pkg = maybePkg.value();
+        const bool privateNamespace = pkg.privateNamespace || ref.value().privateReference;
+
+        InlineDeclaration decl;
+        decl.el = el;
+        decl.attrName = name.entry;
+        if (!pkg.package.empty()) {
+            decl.attrNamespaceUri = xml::buildPackageNamespace(pkg.package, privateNamespace);
+        }
+
+        mInlineDeclarations.push_back(std::move(decl));
+    }
+
+    const std::vector<InlineDeclaration>& getInlineDeclarations() const {
+        return mInlineDeclarations;
+    }
+
+    bool hasError() const {
+        return mError;
+    }
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(Visitor);
+
+    IAaptContext* mContext;
+    xml::XmlResource* mXmlResource;
+    std::vector<InlineDeclaration> mInlineDeclarations;
+    bool mError = false;
+};
+
+} // namespace
+
+bool InlineXmlFormatParser::consume(IAaptContext* context, xml::XmlResource* doc) {
+    Visitor visitor(context, doc);
+    doc->root->accept(&visitor);
+    if (visitor.hasError()) {
+        return false;
+    }
+
+    size_t nameSuffixCounter = 0;
+    for (const Visitor::InlineDeclaration& decl : visitor.getInlineDeclarations()) {
+        auto newDoc = util::make_unique<xml::XmlResource>();
+        newDoc->file.config = doc->file.config;
+        newDoc->file.source = doc->file.source.withLine(decl.el->lineNumber);
+        newDoc->file.name = doc->file.name;
+
+        // Modify the new entry name. We need to suffix the entry with a number to avoid
+        // local collisions, then mangle it with the empty package, such that it won't show up
+        // in R.java.
+
+        newDoc->file.name.entry = NameMangler::mangleEntry(
+                {}, newDoc->file.name.entry + "__" + std::to_string(nameSuffixCounter));
+
+        // Extracted elements must be the only child of <aapt:attr>.
+        // Make sure there is one root node in the children (ignore empty text).
+        for (auto& child : decl.el->children) {
+            const Source childSource = doc->file.source.withLine(child->lineNumber);
+            if (xml::Text* t = xml::nodeCast<xml::Text>(child.get())) {
+                if (!util::trimWhitespace(t->text).empty()) {
+                    context->getDiagnostics()->error(DiagMessage(childSource)
+                                                     << "can't extract text into its own resource");
+                    return false;
+                }
+            } else if (newDoc->root) {
+                context->getDiagnostics()->error(DiagMessage(childSource)
+                                                 << "inline XML resources must have a single root");
+                return false;
+            } else {
+                newDoc->root = std::move(child);
+                newDoc->root->parent = nullptr;
+            }
+        }
+
+        // Walk up and find the parent element.
+        xml::Node* node = decl.el;
+        xml::Element* parentEl = nullptr;
+        while (node->parent && (parentEl = xml::nodeCast<xml::Element>(node->parent)) == nullptr) {
+            node = node->parent;
+        }
+
+        if (!parentEl) {
+            context->getDiagnostics()->error(DiagMessage(newDoc->file.source)
+                                             << "no suitable parent for inheriting attribute");
+            return false;
+        }
+
+        // Add the inline attribute to the parent.
+        parentEl->attributes.push_back(xml::Attribute{
+                decl.attrNamespaceUri, decl.attrName, "@" + newDoc->file.name.toString() });
+
+        // Delete the subtree.
+        for (auto iter = parentEl->children.begin(); iter != parentEl->children.end(); ++iter) {
+            if (iter->get() == node) {
+                parentEl->children.erase(iter);
+                break;
+            }
+        }
+
+        mQueue.push_back(std::move(newDoc));
+
+        nameSuffixCounter++;
+    }
+    return true;
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/compile/InlineXmlFormatParser.h b/tools/aapt2/compile/InlineXmlFormatParser.h
new file mode 100644
index 0000000..69065fd
--- /dev/null
+++ b/tools/aapt2/compile/InlineXmlFormatParser.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2016 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_COMPILE_INLINEXMLFORMATPARSER_H
+#define AAPT_COMPILE_INLINEXMLFORMATPARSER_H
+
+#include "process/IResourceTableConsumer.h"
+
+#include <android-base/macros.h>
+#include <memory>
+#include <vector>
+
+namespace aapt {
+
+/**
+ * Extracts Inline XML definitions into their own xml::XmlResource objects.
+ *
+ * Inline XML looks like:
+ *
+ * <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
+ *                  xmlns:aapt="http://schemas.android.com/aapt" >
+ *   <aapt:attr name="android:drawable" >
+ *     <vector
+ *       android:height="64dp"
+ *       android:width="64dp"
+ *       android:viewportHeight="600"
+ *       android:viewportWidth="600"/>
+ *   </aapt:attr>
+ * </animated-vector>
+ *
+ * The <vector> will be extracted into its own XML file and <animated-vector> will
+ * gain an attribute 'android:drawable' set to a reference to the extracted <vector> resource.
+ */
+class InlineXmlFormatParser : public IXmlResourceConsumer {
+public:
+    explicit InlineXmlFormatParser() = default;
+
+    bool consume(IAaptContext* context, xml::XmlResource* doc) override;
+
+    std::vector<std::unique_ptr<xml::XmlResource>>& getExtractedInlineXmlDocuments() {
+        return mQueue;
+    }
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(InlineXmlFormatParser);
+
+    std::vector<std::unique_ptr<xml::XmlResource>> mQueue;
+};
+
+} // namespace aapt
+
+#endif /* AAPT_COMPILE_INLINEXMLFORMATPARSER_H */
diff --git a/tools/aapt2/compile/InlineXmlFormatParser_test.cpp b/tools/aapt2/compile/InlineXmlFormatParser_test.cpp
new file mode 100644
index 0000000..8d62210
--- /dev/null
+++ b/tools/aapt2/compile/InlineXmlFormatParser_test.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "compile/InlineXmlFormatParser.h"
+#include "test/Test.h"
+
+namespace aapt {
+
+TEST(InlineXmlFormatParserTest, PassThrough) {
+    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
+    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
+      <View xmlns:android="http://schemas.android.com/apk/res/android">
+        <View android:text="hey">
+          <View android:id="hi" />
+        </View>
+      </View>)EOF");
+
+    InlineXmlFormatParser parser;
+    ASSERT_TRUE(parser.consume(context.get(), doc.get()));
+    EXPECT_EQ(0u, parser.getExtractedInlineXmlDocuments().size());
+}
+
+TEST(InlineXmlFormatParserTest, ExtractOneXmlResource) {
+    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
+    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
+      <View1 xmlns:android="http://schemas.android.com/apk/res/android"
+            xmlns:aapt="http://schemas.android.com/aapt">
+        <aapt:attr name="android:text">
+          <View2 android:text="hey">
+            <View3 android:id="hi" />
+          </View2>
+        </aapt:attr>
+      </View1>)EOF");
+
+    doc->file.name = test::parseNameOrDie("layout/main");
+
+    InlineXmlFormatParser parser;
+    ASSERT_TRUE(parser.consume(context.get(), doc.get()));
+
+    // One XML resource should have been extracted.
+    EXPECT_EQ(1u, parser.getExtractedInlineXmlDocuments().size());
+
+    xml::Element* el = xml::findRootElement(doc.get());
+    ASSERT_NE(nullptr, el);
+
+    EXPECT_EQ("View1", el->name);
+
+    // The <aapt:attr> tag should be extracted.
+    EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr"));
+
+    // The 'android:text' attribute should be set with a reference.
+    xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "text");
+    ASSERT_NE(nullptr, attr);
+
+    ResourceNameRef nameRef;
+    ASSERT_TRUE(ResourceUtils::parseReference(attr->value, &nameRef));
+
+    xml::XmlResource* extractedDoc = parser.getExtractedInlineXmlDocuments()[0].get();
+    ASSERT_NE(nullptr, extractedDoc);
+
+    // Make sure the generated reference is correct.
+    EXPECT_EQ(nameRef.package, extractedDoc->file.name.package);
+    EXPECT_EQ(nameRef.type, extractedDoc->file.name.type);
+    EXPECT_EQ(nameRef.entry, extractedDoc->file.name.entry);
+
+    // Verify the structure of the extracted XML.
+    el = xml::findRootElement(extractedDoc);
+    ASSERT_NE(nullptr, el);
+    EXPECT_EQ("View2", el->name);
+    EXPECT_NE(nullptr, el->findChild({}, "View3"));
+}
+
+TEST(InlineXmlFormatParserTest, ExtractTwoXmlResources) {
+    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
+    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
+      <View1 xmlns:android="http://schemas.android.com/apk/res/android"
+            xmlns:aapt="http://schemas.android.com/aapt">
+        <aapt:attr name="android:text">
+          <View2 android:text="hey">
+            <View3 android:id="hi" />
+          </View2>
+        </aapt:attr>
+
+        <aapt:attr name="android:drawable">
+          <vector />
+        </aapt:attr>
+      </View1>)EOF");
+
+    doc->file.name = test::parseNameOrDie("layout/main");
+
+    InlineXmlFormatParser parser;
+    ASSERT_TRUE(parser.consume(context.get(), doc.get()));
+    ASSERT_EQ(2u, parser.getExtractedInlineXmlDocuments().size());
+
+    xml::Element* el = xml::findRootElement(doc.get());
+    ASSERT_NE(nullptr, el);
+
+    EXPECT_EQ("View1", el->name);
+
+    xml::Attribute* attrText = el->findAttribute(xml::kSchemaAndroid, "text");
+    ASSERT_NE(nullptr, attrText);
+
+    xml::Attribute* attrDrawable = el->findAttribute(xml::kSchemaAndroid, "drawable");
+    ASSERT_NE(nullptr, attrDrawable);
+
+    // The two extracted resources should have different names.
+    EXPECT_NE(attrText->value, attrDrawable->value);
+
+    // The child <aapt:attr> elements should be gone.
+    EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr"));
+
+    xml::XmlResource* extractedDocText = parser.getExtractedInlineXmlDocuments()[0].get();
+    ASSERT_NE(nullptr, extractedDocText);
+    el = xml::findRootElement(extractedDocText);
+    ASSERT_NE(nullptr, el);
+    EXPECT_EQ("View2", el->name);
+
+    xml::XmlResource* extractedDocDrawable = parser.getExtractedInlineXmlDocuments()[1].get();
+    ASSERT_NE(nullptr, extractedDocDrawable);
+    el = xml::findRootElement(extractedDocDrawable);
+    ASSERT_NE(nullptr, el);
+    EXPECT_EQ("vector", el->name);
+}
+
+} // namespace aapt