Move StringPool to libandroidfw

Test: verified affected tests pass
Bug: 232940948
Change-Id: I22089893d7e5013f759c39ce190bec07fa6435db
diff --git a/tools/aapt2/xml/XmlActionExecutor.cpp b/tools/aapt2/xml/XmlActionExecutor.cpp
index ea42d26..9bdbd22 100644
--- a/tools/aapt2/xml/XmlActionExecutor.cpp
+++ b/tools/aapt2/xml/XmlActionExecutor.cpp
@@ -22,17 +22,19 @@
 namespace xml {
 
 static bool wrapper_one(const XmlNodeAction::ActionFunc& f, Element* el,
-                        const XmlActionExecutorPolicy& policy, SourcePathDiagnostics*) {
+                        const XmlActionExecutorPolicy& policy, android::SourcePathDiagnostics*) {
   return f(el);
 }
 
 static bool wrapper_two(const XmlNodeAction::ActionFuncWithDiag& f, Element* el,
-                        const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
+                        const XmlActionExecutorPolicy& policy,
+                        android::SourcePathDiagnostics* diag) {
   return f(el, diag);
 }
 
 static bool wrapper_three(const XmlNodeAction::ActionFuncWithPolicyAndDiag& f, Element* el,
-                          const XmlActionExecutorPolicy& policy, SourcePathDiagnostics* diag) {
+                          const XmlActionExecutorPolicy& policy,
+                          android::SourcePathDiagnostics* diag) {
   return f(el, policy, diag);
 }
 
@@ -51,7 +53,7 @@
                                   std::placeholders::_2, std::placeholders::_3));
 }
 
-static void PrintElementToDiagMessage(const Element* el, DiagMessage* msg) {
+static void PrintElementToDiagMessage(const Element* el, android::DiagMessage* msg) {
   *msg << "<";
   if (!el->namespace_uri.empty()) {
     *msg << el->namespace_uri << ":";
@@ -60,7 +62,7 @@
 }
 
 bool XmlNodeAction::Execute(XmlActionExecutorPolicy policy, std::vector<StringPiece>* bread_crumb,
-                            SourcePathDiagnostics* diag, Element* el) const {
+                            android::SourcePathDiagnostics* diag, Element* el) const {
   bool error = false;
   for (const ActionFuncWithPolicyAndDiag& action : actions_) {
     error |= !action(el, policy, diag);
@@ -78,7 +80,7 @@
       }
 
       if (policy != XmlActionExecutorPolicy::kNone) {
-        DiagMessage error_msg(child_el->line_number);
+        android::DiagMessage error_msg(child_el->line_number);
         error_msg << "unexpected element ";
         PrintElementToDiagMessage(child_el, &error_msg);
         error_msg << " found in ";
@@ -99,14 +101,14 @@
   return !error;
 }
 
-bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
+bool XmlActionExecutor::Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag,
                                 XmlResource* doc) const {
-  SourcePathDiagnostics source_diag(doc->file.source, diag);
+  android::SourcePathDiagnostics source_diag(doc->file.source, diag);
 
   Element* el = doc->root.get();
   if (!el) {
     if (policy == XmlActionExecutorPolicy::kAllowList) {
-      source_diag.Error(DiagMessage() << "no root XML tag found");
+      source_diag.Error(android::DiagMessage() << "no root XML tag found");
       return false;
     }
     return true;
@@ -121,7 +123,7 @@
     }
 
     if (policy == XmlActionExecutorPolicy::kAllowList) {
-      DiagMessage error_msg(el->line_number);
+      android::DiagMessage error_msg(el->line_number);
       error_msg << "unexpected root element ";
       PrintElementToDiagMessage(el, &error_msg);
       source_diag.Error(error_msg);
diff --git a/tools/aapt2/xml/XmlActionExecutor.h b/tools/aapt2/xml/XmlActionExecutor.h
index 78c4334..8cc4573 100644
--- a/tools/aapt2/xml/XmlActionExecutor.h
+++ b/tools/aapt2/xml/XmlActionExecutor.h
@@ -23,8 +23,7 @@
 #include <vector>
 
 #include "android-base/macros.h"
-
-#include "Diagnostics.h"
+#include "androidfw/IDiagnostics.h"
 #include "xml/XmlDom.h"
 
 namespace aapt {
@@ -50,8 +49,8 @@
 class XmlNodeAction {
  public:
   using ActionFuncWithPolicyAndDiag =
-      std::function<bool(Element*, XmlActionExecutorPolicy, SourcePathDiagnostics*)>;
-  using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>;
+      std::function<bool(Element*, XmlActionExecutorPolicy, android::SourcePathDiagnostics*)>;
+  using ActionFuncWithDiag = std::function<bool(Element*, android::SourcePathDiagnostics*)>;
   using ActionFunc = std::function<bool(Element*)>;
 
   // Find or create a child XmlNodeAction that will be performed for the child element with the
@@ -69,7 +68,7 @@
   friend class XmlActionExecutor;
 
   bool Execute(XmlActionExecutorPolicy policy, std::vector<::android::StringPiece>* bread_crumb,
-               SourcePathDiagnostics* diag, Element* el) const;
+               android::SourcePathDiagnostics* diag, Element* el) const;
 
   std::map<std::string, XmlNodeAction> map_;
   std::vector<ActionFuncWithPolicyAndDiag> actions_;
@@ -88,7 +87,7 @@
 
   // Execute the defined actions for this XmlResource.
   // Returns true if all actions return true, otherwise returns false.
-  bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const;
+  bool Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag, XmlResource* doc) const;
 
  private:
   std::map<std::string, XmlNodeAction> map_;
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index 8b7eadf9..f51e8a4 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -183,7 +183,8 @@
   stack->pending_comment += comment;
 }
 
-std::unique_ptr<XmlResource> Inflate(InputStream* in, IDiagnostics* diag, const Source& source) {
+std::unique_ptr<XmlResource> Inflate(InputStream* in, android::IDiagnostics* diag,
+                                     const android::Source& source) {
   Stack stack;
 
   std::unique_ptr<std::remove_pointer<XML_Parser>::type, decltype(XML_ParserFree)*> parser = {
@@ -199,28 +200,29 @@
   size_t buffer_size = 0;
   while (in->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
     if (XML_Parse(parser.get(), buffer, buffer_size, false) == XML_STATUS_ERROR) {
-      diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
+      diag->Error(android::DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
                   << XML_ErrorString(XML_GetErrorCode(parser.get())));
       return {};
     }
   }
 
   if (in->HadError()) {
-    diag->Error(DiagMessage(source) << in->GetError());
+    diag->Error(android::DiagMessage(source) << in->GetError());
     return {};
   } else {
     // Finish off the parsing.
     if (XML_Parse(parser.get(), nullptr, 0u, true) == XML_STATUS_ERROR) {
-      diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
+      diag->Error(android::DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
                   << XML_ErrorString(XML_GetErrorCode(parser.get())));
       return {};
     }
   }
   return util::make_unique<XmlResource>(ResourceFile{{}, {}, ResourceFile::Type::kUnknown, source},
-                                        StringPool{}, std::move(stack.root));
+                                        android::StringPool{}, std::move(stack.root));
 }
 
-static void CopyAttributes(Element* el, android::ResXMLParser* parser, StringPool* out_pool) {
+static void CopyAttributes(Element* el, android::ResXMLParser* parser,
+                           android::StringPool* out_pool) {
   const size_t attr_count = parser->getAttributeCount();
   if (attr_count > 0) {
     el->attributes.reserve(attr_count);
@@ -229,12 +231,12 @@
       size_t len;
       const char16_t* str16 = parser->getAttributeNamespace(i, &len);
       if (str16) {
-        attr.namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
+        attr.namespace_uri = android::util::Utf16ToUtf8(StringPiece16(str16, len));
       }
 
       str16 = parser->getAttributeName(i, &len);
       if (str16) {
-        attr.name = util::Utf16ToUtf8(StringPiece16(str16, len));
+        attr.name = android::util::Utf16ToUtf8(StringPiece16(str16, len));
       }
 
       uint32_t res_id = parser->getAttributeNameResID(i);
@@ -244,7 +246,7 @@
 
       str16 = parser->getAttributeStringValue(i, &len);
       if (str16) {
-        attr.value = util::Utf16ToUtf8(StringPiece16(str16, len));
+        attr.value = android::util::Utf16ToUtf8(StringPiece16(str16, len));
       }
 
       android::Res_value res_value;
@@ -294,12 +296,12 @@
         size_t len;
         const char16_t* str16 = tree.getNamespacePrefix(&len);
         if (str16) {
-          decl.prefix = util::Utf16ToUtf8(StringPiece16(str16, len));
+          decl.prefix = android::util::Utf16ToUtf8(StringPiece16(str16, len));
         }
 
         str16 = tree.getNamespaceUri(&len);
         if (str16) {
-          decl.uri = util::Utf16ToUtf8(StringPiece16(str16, len));
+          decl.uri = android::util::Utf16ToUtf8(StringPiece16(str16, len));
         }
 
         if (pending_element == nullptr) {
@@ -323,12 +325,12 @@
         size_t len;
         const char16_t* str16 = tree.getElementNamespace(&len);
         if (str16) {
-          el->namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
+          el->namespace_uri = android::util::Utf16ToUtf8(StringPiece16(str16, len));
         }
 
         str16 = tree.getElementName(&len);
         if (str16) {
-          el->name = util::Utf16ToUtf8(StringPiece16(str16, len));
+          el->name = android::util::Utf16ToUtf8(StringPiece16(str16, len));
         }
 
         Element* this_el = el.get();
@@ -349,7 +351,7 @@
         size_t len;
         const char16_t* str16 = tree.getText(&len);
         if (str16) {
-          text->text = util::Utf16ToUtf8(StringPiece16(str16, len));
+          text->text = android::util::Utf16ToUtf8(StringPiece16(str16, len));
         }
         CHECK(!node_stack.empty());
         node_stack.top()->AppendChild(std::move(text));
diff --git a/tools/aapt2/xml/XmlDom.h b/tools/aapt2/xml/XmlDom.h
index 5d31804..5bc55b6 100644
--- a/tools/aapt2/xml/XmlDom.h
+++ b/tools/aapt2/xml/XmlDom.h
@@ -21,11 +21,10 @@
 #include <string>
 #include <vector>
 
-#include "androidfw/StringPiece.h"
-
-#include "Diagnostics.h"
 #include "Resource.h"
 #include "ResourceValues.h"
+#include "androidfw/IDiagnostics.h"
+#include "androidfw/StringPiece.h"
 #include "io/Io.h"
 #include "util/Util.h"
 #include "xml/XmlUtil.h"
@@ -150,7 +149,7 @@
   // StringPool must come before the xml::Node. Destructors are called in reverse order, and
   // the xml::Node may have StringPool references that need to be destroyed before the StringPool
   // is destroyed.
-  StringPool string_pool;
+  android::StringPool string_pool;
 
   std::unique_ptr<xml::Element> root;
 
@@ -158,7 +157,8 @@
 };
 
 // Inflates an XML DOM from an InputStream, logging errors to the logger.
-std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
+std::unique_ptr<XmlResource> Inflate(io::InputStream* in, android::IDiagnostics* diag,
+                                     const android::Source& source);
 
 // Inflates an XML DOM from a binary ResXMLTree.
 std::unique_ptr<XmlResource> Inflate(const void* data, size_t len,
diff --git a/tools/aapt2/xml/XmlDom_test.cpp b/tools/aapt2/xml/XmlDom_test.cpp
index 6c717dc..c503338 100644
--- a/tools/aapt2/xml/XmlDom_test.cpp
+++ b/tools/aapt2/xml/XmlDom_test.cpp
@@ -45,7 +45,7 @@
 
   StdErrDiagnostics diag;
   StringInputStream in(input);
-  std::unique_ptr<XmlResource> doc = Inflate(&in, &diag, Source("test.xml"));
+  std::unique_ptr<XmlResource> doc = Inflate(&in, &diag, android::Source("test.xml"));
   ASSERT_THAT(doc, NotNull());
 
   Element* el = doc->root.get();
@@ -77,13 +77,13 @@
   decl.line_number = 2u;
   doc->root->namespace_decls.push_back(decl);
 
-  BigBuffer buffer(4096);
+  android::BigBuffer buffer(4096);
   XmlFlattenerOptions options;
   options.keep_raw_values = true;
   XmlFlattener flattener(&buffer, options);
   ASSERT_TRUE(flattener.Consume(context.get(), doc.get()));
 
-  auto block = util::Copy(buffer);
+  auto block = android::util::Copy(buffer);
   std::unique_ptr<XmlResource> new_doc = Inflate(block.get(), buffer.size(), nullptr);
   ASSERT_THAT(new_doc, NotNull());