summaryrefslogtreecommitdiff
path: root/tools/aapt2/configuration/ConfigurationParser.cpp
diff options
context:
space:
mode:
authorShane Farmer <safarmer@google.com>2017-06-22 12:26:44 -0700
committerShane Farmer <safarmer@google.com>2017-07-18 20:32:54 +0000
commit9f0e7f1dbade4b35b85e23954fa2b7c31d5f2009 (patch)
tree96b29ed091ce96131997ea3814e46838c63efb77 /tools/aapt2/configuration/ConfigurationParser.cpp
parent91936a1d06641c26158bf074a135363082fd4213 (diff)
AAPT2: Parse artifact names from template.
Add a helper method to convert a templated artifact name to file name based on the values present in an Artifact struct. The Artifact validates that all required template paramters are present. Test: Unit tests Change-Id: Id97ff606bb41c72a31c2d769104966be9cbca1a0
Diffstat (limited to 'tools/aapt2/configuration/ConfigurationParser.cpp')
-rw-r--r--tools/aapt2/configuration/ConfigurationParser.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/tools/aapt2/configuration/ConfigurationParser.cpp b/tools/aapt2/configuration/ConfigurationParser.cpp
index 0b6743c4c3b6..d051120b9445 100644
--- a/tools/aapt2/configuration/ConfigurationParser.cpp
+++ b/tools/aapt2/configuration/ConfigurationParser.cpp
@@ -29,6 +29,7 @@
#include "Diagnostics.h"
#include "io/File.h"
#include "io/FileSystem.h"
+#include "util/Maybe.h"
#include "util/Util.h"
#include "xml/XmlActionExecutor.h"
#include "xml/XmlDom.h"
@@ -109,6 +110,61 @@ const std::string& AbiToString(Abi abi) {
return kAbiToStringMap.find(abi)->second;
}
+/**
+ * Attempts to replace the placeholder in the name string with the provided value. Returns true on
+ * success, or false if the either the placeholder is not found in the name, or the value is not
+ * present and the placeholder was.
+ */
+static bool ReplacePlaceholder(const std::string& placeholder, const Maybe<std::string>& value,
+ std::string* name, IDiagnostics* diag) {
+ size_t offset = name->find(placeholder);
+ if (value) {
+ if (offset == std::string::npos) {
+ diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder);
+ return false;
+ }
+ name->replace(offset, placeholder.length(), value.value());
+ return true;
+ }
+
+ // Make sure the placeholder was not present if the desired value was not present.
+ bool result = (offset == std::string::npos);
+ if (!result) {
+ diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder);
+ }
+ return result;
+}
+
+Maybe<std::string> Artifact::ToArtifactName(const std::string& format, IDiagnostics* diag) const {
+ std::string result = format;
+
+ if (!ReplacePlaceholder("{abi}", abi_group, &result, diag)) {
+ return {};
+ }
+
+ if (!ReplacePlaceholder("{density}", screen_density_group, &result, diag)) {
+ return {};
+ }
+
+ if (!ReplacePlaceholder("{locale}", locale_group, &result, diag)) {
+ return {};
+ }
+
+ if (!ReplacePlaceholder("{sdk}", android_sdk_group, &result, diag)) {
+ return {};
+ }
+
+ if (!ReplacePlaceholder("{feature}", device_feature_group, &result, diag)) {
+ return {};
+ }
+
+ if (!ReplacePlaceholder("{gl}", gl_texture_group, &result, diag)) {
+ return {};
+ }
+
+ return result;
+}
+
} // namespace configuration
/** Returns a ConfigurationParser for the file located at the provided path. */