AAPT2: Read config from disk
Implement the todo left from last change to read the contents of the
configuration file from disk. Since this is an operation that may fail
the API was changed to take return a Maybe to indicate errors reading
the file.
Test: unit test for error condition
Test: ran aapt2 optimize with the new code path wired in
Change-Id: I93d532b4a57af9520231225eee4fc5f2b1a046b9
diff --git a/tools/aapt2/configuration/ConfigurationParser.cpp b/tools/aapt2/configuration/ConfigurationParser.cpp
index 89618d3..303a809 100644
--- a/tools/aapt2/configuration/ConfigurationParser.cpp
+++ b/tools/aapt2/configuration/ConfigurationParser.cpp
@@ -21,10 +21,13 @@
#include <memory>
#include <utility>
+#include <android-base/file.h>
#include <android-base/logging.h>
#include "ConfigDescription.h"
#include "Diagnostics.h"
+#include "io/File.h"
+#include "io/FileSystem.h"
#include "util/Util.h"
#include "xml/XmlActionExecutor.h"
#include "xml/XmlDom.h"
@@ -42,6 +45,8 @@
using ::aapt::configuration::GlTexture;
using ::aapt::configuration::Group;
using ::aapt::configuration::Locale;
+using ::aapt::io::IFile;
+using ::aapt::io::RegularFile;
using ::aapt::util::TrimWhitespace;
using ::aapt::xml::Element;
using ::aapt::xml::FindRootElement;
@@ -49,6 +54,7 @@
using ::aapt::xml::XmlActionExecutor;
using ::aapt::xml::XmlActionExecutorPolicy;
using ::aapt::xml::XmlNodeAction;
+using ::android::base::ReadFileToString;
const std::unordered_map<std::string, Abi> kAbiMap = {
{"armeabi", Abi::kArmeV6},
@@ -96,6 +102,17 @@
} // namespace
+
+
+/** Returns a ConfigurationParser for the file located at the provided path. */
+Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) {
+ std::string contents;
+ if (!ReadFileToString(path, &contents, true)) {
+ return {};
+ }
+ return ConfigurationParser(contents);
+}
+
ConfigurationParser::ConfigurationParser(std::string contents)
: contents_(std::move(contents)),
diag_(&noop_) {
diff --git a/tools/aapt2/configuration/ConfigurationParser.h b/tools/aapt2/configuration/ConfigurationParser.h
index 0fb2f71..8b9c085 100644
--- a/tools/aapt2/configuration/ConfigurationParser.h
+++ b/tools/aapt2/configuration/ConfigurationParser.h
@@ -142,18 +142,16 @@
*/
class ConfigurationParser {
public:
+
+ /** Returns a ConfigurationParser for the file located at the provided path. */
+ static Maybe<ConfigurationParser> ForPath(const std::string& path);
+
/** Returns a ConfigurationParser for the configuration in the provided file contents. */
static ConfigurationParser ForContents(const std::string& contents) {
ConfigurationParser parser{contents};
return parser;
}
- /** Returns a ConfigurationParser for the file located at the provided path. */
- static ConfigurationParser ForPath(const std::string& path) {
- // TODO: Read XML file into memory.
- return ForContents(path);
- }
-
/** Sets the diagnostics context to use when parsing. */
ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
diag_ = diagnostics;
diff --git a/tools/aapt2/configuration/ConfigurationParser_test.cpp b/tools/aapt2/configuration/ConfigurationParser_test.cpp
index 72a97b2..8421ee3 100644
--- a/tools/aapt2/configuration/ConfigurationParser_test.cpp
+++ b/tools/aapt2/configuration/ConfigurationParser_test.cpp
@@ -130,6 +130,11 @@
StdErrDiagnostics diag_;
};
+TEST_F(ConfigurationParserTest, ForPath_NoFile) {
+ auto result = ConfigurationParser::ForPath("./does_not_exist.xml");
+ EXPECT_FALSE(result);
+}
+
TEST_F(ConfigurationParserTest, ValidateFile) {
auto parser = ConfigurationParser::ForContents(kValidConfig).WithDiagnostics(&diag_);
auto result = parser.Parse();
diff --git a/tools/aapt2/configuration/example/config.xml b/tools/aapt2/configuration/example/config.xml
index a8360f8..ce31e61 100644
--- a/tools/aapt2/configuration/example/config.xml
+++ b/tools/aapt2/configuration/example/config.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
-<post-process xmlns="http://schemas.android.com/tools/aapt2">
+<post-process xmlns="http://schemas.android.com/tools/aapt">
<groups>
<abi-group label="arm">
<abi>armeabi-v7a</abi>