AAPT2: Cleanup proto classes/methods and add XML serialization

Test: make aapt2_tests
Change-Id: I01ac2285af6771a683533c033a59ae6cfe875d93
diff --git a/tools/aapt2/cmd/Dump.cpp b/tools/aapt2/cmd/Dump.cpp
index 0965910..0dedc91 100644
--- a/tools/aapt2/cmd/Dump.cpp
+++ b/tools/aapt2/cmd/Dump.cpp
@@ -23,7 +23,7 @@
 #include "Flags.h"
 #include "io/ZipArchive.h"
 #include "process/IResourceTableConsumer.h"
-#include "proto/ProtoSerialize.h"
+#include "proto/ProtoDeserialize.h"
 #include "unflatten/BinaryResourceParser.h"
 #include "util/Files.h"
 
@@ -33,29 +33,28 @@
 
 bool DumpCompiledFile(const pb::internal::CompiledFile& pb_file, const void* data, size_t len,
                       const Source& source, IAaptContext* context) {
-  std::unique_ptr<ResourceFile> file =
-      DeserializeCompiledFileFromPb(pb_file, source, context->GetDiagnostics());
-  if (!file) {
-    context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
+  ResourceFile file;
+  std::string error;
+  if (!DeserializeCompiledFileFromPb(pb_file, &file, &error)) {
+    context->GetDiagnostics()->Warn(DiagMessage(source)
+                                    << "failed to read compiled file: " << error);
     return false;
   }
 
-  std::cout << "Resource: " << file->name << "\n"
-            << "Config:   " << file->config << "\n"
-            << "Source:   " << file->source << "\n";
+  std::cout << "Resource: " << file.name << "\n"
+            << "Config:   " << file.config << "\n"
+            << "Source:   " << file.source << "\n";
   return true;
 }
 
 bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
-  std::unique_ptr<ResourceTable> table;
-
   std::string err;
   std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err);
   if (zip) {
-    io::IFile* file = zip->FindFile("resources.arsc.flat");
-    if (file) {
+    ResourceTable table;
+    if (io::IFile* file = zip->FindFile("resources.arsc.flat")) {
       std::unique_ptr<io::IData> data = file->OpenAsData();
-      if (!data) {
+      if (data == nullptr) {
         context->GetDiagnostics()->Error(DiagMessage(file_path)
                                          << "failed to open resources.arsc.flat");
         return false;
@@ -67,83 +66,78 @@
         return false;
       }
 
-      table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
-      if (!table) {
+      ResourceTable table;
+      if (!DeserializeTableFromPb(pb_table, &table, &err)) {
+        context->GetDiagnostics()->Error(DiagMessage(file_path)
+                                         << "failed to parse table: " << err);
+        return false;
+      }
+    } else if (io::IFile* file = zip->FindFile("resources.arsc")) {
+      std::unique_ptr<io::IData> data = file->OpenAsData();
+      if (!data) {
+        context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc");
+        return false;
+      }
+
+      BinaryResourceParser parser(context, &table, Source(file_path), data->data(), data->size());
+      if (!parser.Parse()) {
         return false;
       }
     }
 
-    if (!table) {
-      file = zip->FindFile("resources.arsc");
-      if (file) {
-        std::unique_ptr<io::IData> data = file->OpenAsData();
-        if (!data) {
-          context->GetDiagnostics()->Error(DiagMessage(file_path)
-                                           << "failed to open resources.arsc");
-          return false;
-        }
+    DebugPrintTableOptions options;
+    options.show_sources = true;
+    Debug::PrintTable(&table, options);
+    return true;
+  }
 
-        table = util::make_unique<ResourceTable>();
-        BinaryResourceParser parser(context, table.get(), Source(file_path), data->data(),
-                                    data->size());
-        if (!parser.Parse()) {
-          return false;
-        }
-      }
+  err.clear();
+
+  Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
+  if (!file) {
+    context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
+    return false;
+  }
+
+  android::FileMap* file_map = &file.value();
+
+  // Check to see if this is a loose ResourceTable.
+  pb::ResourceTable pb_table;
+  if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) {
+    ResourceTable table;
+    if (DeserializeTableFromPb(pb_table, &table, &err)) {
+      DebugPrintTableOptions options;
+      options.show_sources = true;
+      Debug::PrintTable(&table, options);
+      return true;
     }
   }
 
-  if (!table) {
-    Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
-    if (!file) {
-      context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
+  // Try as a compiled file.
+  CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength());
+  uint32_t num_files = 0;
+  if (!input.ReadLittleEndian32(&num_files)) {
+    return false;
+  }
+
+  for (uint32_t i = 0; i < num_files; i++) {
+    pb::internal::CompiledFile compiled_file;
+    if (!input.ReadCompiledFile(&compiled_file)) {
+      context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
       return false;
     }
 
-    android::FileMap* file_map = &file.value();
-
-    // Try as a compiled table.
-    pb::ResourceTable pb_table;
-    if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) {
-      table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
+    uint64_t offset, len;
+    if (!input.ReadDataMetaData(&offset, &len)) {
+      context->GetDiagnostics()->Warn(DiagMessage() << "failed to read meta data");
+      return false;
     }
 
-    if (!table) {
-      // Try as a compiled file.
-      CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength());
-
-      uint32_t num_files = 0;
-      if (!input.ReadLittleEndian32(&num_files)) {
-        return false;
-      }
-
-      for (uint32_t i = 0; i < num_files; i++) {
-        pb::internal::CompiledFile compiled_file;
-        if (!input.ReadCompiledFile(&compiled_file)) {
-          context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
-          return false;
-        }
-
-        uint64_t offset, len;
-        if (!input.ReadDataMetaData(&offset, &len)) {
-          context->GetDiagnostics()->Warn(DiagMessage() << "failed to read meta data");
-          return false;
-        }
-
-        const void* data = static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
-        if (!DumpCompiledFile(compiled_file, data, len, Source(file_path), context)) {
-          return false;
-        }
-      }
+    const void* data = static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
+    if (!DumpCompiledFile(compiled_file, data, len, Source(file_path), context)) {
+      return false;
     }
   }
-
-  if (table) {
-    DebugPrintTableOptions options;
-    options.show_sources = true;
-    Debug::PrintTable(table.get(), options);
-  }
-
   return true;
 }