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/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index 9005c5b..db83d00 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -482,7 +482,7 @@
 
   if (options_.no_version_vectors || options_.no_version_transitions) {
     // Skip this if it is a vector or animated-vector.
-    xml::Element* el = xml::FindRootElement(doc);
+    xml::Element* el = doc->root.get();
     if (el && el->namespace_uri.empty()) {
       if ((options_.no_version_vectors && IsVectorElement(el->name)) ||
           (options_.no_version_transitions && IsTransitionElement(el->name))) {
diff --git a/tools/aapt2/cmd/Util.cpp b/tools/aapt2/cmd/Util.cpp
index e1c45d6..d17858d 100644
--- a/tools/aapt2/cmd/Util.cpp
+++ b/tools/aapt2/cmd/Util.cpp
@@ -28,7 +28,7 @@
 #include "util/Maybe.h"
 #include "util/Util.h"
 
-using android::StringPiece;
+using ::android::StringPiece;
 
 namespace aapt {
 
@@ -134,19 +134,21 @@
   return xml::AaptAttribute(Attribute(), id);
 }
 
+static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
+  xml::NamespaceDecl decl;
+  decl.prefix = "android";
+  decl.uri = xml::kSchemaAndroid;
+  return decl;
+}
+
 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
                                                         const SplitConstraints& constraints) {
   const ResourceId kVersionCode(0x0101021b);
   const ResourceId kRevisionCode(0x010104d5);
   const ResourceId kHasCode(0x0101000c);
 
-  std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
-
-  std::unique_ptr<xml::Namespace> namespace_android = util::make_unique<xml::Namespace>();
-  namespace_android->namespace_uri = xml::kSchemaAndroid;
-  namespace_android->namespace_prefix = "android";
-
   std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
+  manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
   manifest_el->name = "manifest";
   manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
 
@@ -179,8 +181,8 @@
         xml::Attribute{"", "configForSplit", app_info.split_name.value()});
   }
 
-  // Splits may contain more configurations than originally desired (fallback densities, etc.).
-  // This makes programmatic discovery of split targetting difficult. Encode the original
+  // Splits may contain more configurations than originally desired (fall-back densities, etc.).
+  // This makes programmatic discovery of split targeting difficult. Encode the original
   // split constraints intended for this split.
   std::stringstream target_config_str;
   target_config_str << util::Joiner(constraints.configs, ",");
@@ -193,8 +195,9 @@
                      util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
 
   manifest_el->AppendChild(std::move(application_el));
-  namespace_android->AppendChild(std::move(manifest_el));
-  doc->root = std::move(namespace_android);
+
+  std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
+  doc->root = std::move(manifest_el);
   return doc;
 }
 
@@ -284,7 +287,7 @@
 
 Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
   // Make sure the first element is <manifest> with package attribute.
-  xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
+  xml::Element* manifest_el = xml_res->root.get();
   if (manifest_el == nullptr) {
     return {};
   }