AAPT2: Change XmlDom to exclude Namespace as a node

In preparation for exporting an XML proto format for UAM to consume,
this change brings the XML DOM API more in line with other APIs that
do not make the Namespace a separate node.

Treating Namespace declarations as just properties of an Element
node makes the implementation of algorithms much simpler, as
the constraints that Namespace nodes have only one child
are now built in and traversing to find Element nodes
is much simpler.

Also made a bunch of quality of life improvements, like formatting and
comment style.

Test: make aapt2_tests
Change-Id: Ib97ff1c4252b7907e2cc1f13a448dc4ca3b809a4
diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp
index b872ebb..49ed778 100644
--- a/tools/aapt2/Debug.cpp
+++ b/tools/aapt2/Debug.cpp
@@ -33,6 +33,8 @@
 
 namespace aapt {
 
+namespace {
+
 class PrintVisitor : public ValueVisitor {
  public:
   using ValueVisitor::Visit;
@@ -88,9 +90,13 @@
     }
   }
 
-  void Visit(Array* array) override { array->Print(&std::cout); }
+  void Visit(Array* array) override {
+    array->Print(&std::cout);
+  }
 
-  void Visit(Plural* plural) override { plural->Print(&std::cout); }
+  void Visit(Plural* plural) override {
+    plural->Print(&std::cout);
+  }
 
   void Visit(Styleable* styleable) override {
     std::cout << "(styleable)";
@@ -110,11 +116,14 @@
     }
   }
 
-  void VisitItem(Item* item) override { item->Print(&std::cout); }
+  void VisitItem(Item* item) override {
+    item->Print(&std::cout);
+  }
 };
 
-void Debug::PrintTable(ResourceTable* table,
-                       const DebugPrintTableOptions& options) {
+}  // namespace
+
+void Debug::PrintTable(ResourceTable* table, const DebugPrintTableOptions& options) {
   PrintVisitor visitor;
 
   for (auto& package : table->packages) {
@@ -148,10 +157,9 @@
       }
 
       for (const ResourceEntry* entry : sorted_entries) {
-        ResourceId id(package->id ? package->id.value() : uint8_t(0),
-                      type->id ? type->id.value() : uint8_t(0),
-                      entry->id ? entry->id.value() : uint16_t(0));
-        ResourceName name(package->name, type->type, entry->name);
+        const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0),
+                            entry->id.value_or_default(0));
+        const ResourceName name(package->name, type->type, entry->name);
 
         std::cout << "    spec resource " << id << " " << name;
         switch (entry->symbol_status.state) {
@@ -180,16 +188,14 @@
   }
 }
 
-static size_t GetNodeIndex(const std::vector<ResourceName>& names,
-                           const ResourceName& name) {
+static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
   auto iter = std::lower_bound(names.begin(), names.end(), name);
   CHECK(iter != names.end());
   CHECK(*iter == name);
   return std::distance(names.begin(), iter);
 }
 
-void Debug::PrintStyleGraph(ResourceTable* table,
-                            const ResourceName& target_style) {
+void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) {
   std::map<ResourceName, std::set<ResourceName>> graph;
 
   std::queue<ResourceName> styles_to_visit;
@@ -223,8 +229,7 @@
 
   std::cout << "digraph styles {\n";
   for (const auto& name : names) {
-    std::cout << "  node_" << GetNodeIndex(names, name) << " [label=\"" << name
-              << "\"];\n";
+    std::cout << "  node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n";
   }
 
   for (const auto& entry : graph) {
@@ -243,8 +248,7 @@
 void Debug::DumpHex(const void* data, size_t len) {
   const uint8_t* d = (const uint8_t*)data;
   for (size_t i = 0; i < len; i++) {
-    std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i]
-              << " ";
+    std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " ";
     if (i % 8 == 7) {
       std::cerr << "\n";
     }
@@ -262,8 +266,15 @@
   using xml::Visitor::Visit;
 
   void Visit(xml::Element* el) override {
-    std::cerr << prefix_;
-    std::cerr << "E: ";
+    const size_t previous_size = prefix_.size();
+
+    for (const xml::NamespaceDecl& decl : el->namespace_decls) {
+      std::cerr << prefix_ << "N: " << decl.prefix << "=" << decl.uri
+                << " (line=" << decl.line_number << ")\n";
+      prefix_ += "  ";
+    }
+
+    std::cerr << prefix_ << "E: ";
     if (!el->namespace_uri.empty()) {
       std::cerr << el->namespace_uri << ":";
     }
@@ -283,26 +294,13 @@
       std::cerr << "=" << attr.value << "\n";
     }
 
-    const size_t previous_size = prefix_.size();
     prefix_ += "  ";
     xml::Visitor::Visit(el);
     prefix_.resize(previous_size);
   }
 
-  void Visit(xml::Namespace* ns) override {
-    std::cerr << prefix_;
-    std::cerr << "N: " << ns->namespace_prefix << "=" << ns->namespace_uri
-              << " (line=" << ns->line_number << ")\n";
-
-    const size_t previous_size = prefix_.size();
-    prefix_ += "  ";
-    xml::Visitor::Visit(ns);
-    prefix_.resize(previous_size);
-  }
-
   void Visit(xml::Text* text) override {
-    std::cerr << prefix_;
-    std::cerr << "T: '" << text->text << "'\n";
+    std::cerr << prefix_ << "T: '" << text->text << "'\n";
   }
 
  private: