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/link/XmlNamespaceRemover.cpp b/tools/aapt2/link/XmlNamespaceRemover.cpp
index 24aa566..b5e2423 100644
--- a/tools/aapt2/link/XmlNamespaceRemover.cpp
+++ b/tools/aapt2/link/XmlNamespaceRemover.cpp
@@ -24,37 +24,19 @@
 
 namespace {
 
-/**
- * Visits each xml Node, removing URI references and nested namespaces.
- */
+// Visits each xml Node, removing URI references and nested namespaces.
 class XmlVisitor : public xml::Visitor {
  public:
   explicit XmlVisitor(bool keep_uris) : keep_uris_(keep_uris) {}
 
   void Visit(xml::Element* el) override {
-    // Strip namespaces
-    for (auto& child : el->children) {
-      while (child && xml::NodeCast<xml::Namespace>(child.get())) {
-        if (child->children.empty()) {
-          child = {};
-        } else {
-          child = std::move(child->children.front());
-          child->parent = el;
-        }
-      }
-    }
-    el->children.erase(
-        std::remove_if(el->children.begin(), el->children.end(),
-                       [](const std::unique_ptr<xml::Node>& child) -> bool {
-                         return child == nullptr;
-                       }),
-        el->children.end());
+    el->namespace_decls.clear();
 
     if (!keep_uris_) {
       for (xml::Attribute& attr : el->attributes) {
-        attr.namespace_uri = std::string();
+        attr.namespace_uri.clear();
       }
-      el->namespace_uri = std::string();
+      el->namespace_uri.clear();
     }
     xml::Visitor::Visit(el);
   }
@@ -67,19 +49,11 @@
 
 }  // namespace
 
-bool XmlNamespaceRemover::Consume(IAaptContext* context,
-                                  xml::XmlResource* resource) {
+bool XmlNamespaceRemover::Consume(IAaptContext* context, xml::XmlResource* resource) {
   if (!resource->root) {
     return false;
   }
-  // Replace any root namespaces until the root is a non-namespace node
-  while (xml::NodeCast<xml::Namespace>(resource->root.get())) {
-    if (resource->root->children.empty()) {
-      break;
-    }
-    resource->root = std::move(resource->root->children.front());
-    resource->root->parent = nullptr;
-  }
+
   XmlVisitor visitor(keep_uris_);
   resource->root->Accept(&visitor);
   return true;