aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-07-06 13:51:24 +0000
committerGravatar Klaus Aehlig <aehlig@google.com>2016-07-07 08:38:54 +0000
commit8a0fbb88c958eb86f75dddd37f2221d0873c7ac2 (patch)
tree10e4049c62d8c628343cf89cb5f6a5ec19defce5 /src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
parentbc16f72f22a6ba1e1d95ec91f973974826ffe0b4 (diff)
Remove .9 from nine-patch FullyQualifiedNames
In the end, foo.9.png should result in an R.java field R.drawable.foo (not R.drawable.foo_9). Also, given foo.9.png and foo.png, aapt will give an error: res/drawable/foo.png:0: error: Resource entry foo is already defined. res/drawable/foo.9.png:0: Originally defined here. So it seems like we should generate a merge conflict warning as well. Adjust FQN accordingly. -- MOS_MIGRATED_REVID=126693820
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java b/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
index eeebbbcfab..db697d0bdc 100644
--- a/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
+++ b/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
@@ -29,6 +29,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@@ -246,6 +247,44 @@ public class FullyQualifiedName implements DataKey, Comparable<FullyQualifiedNam
return FullyQualifiedName.of(
parsedPackage == null ? pkg : parsedPackage, qualifiers, resourceType, resourceName);
}
+
+ /**
+ * Generates a FullyQualifiedName for a file-based resource given the source Path.
+ *
+ * @param sourcePath the path of the file-based resource.
+ * @throws IllegalArgumentException if the file-based resource has an invalid filename
+ */
+ public FullyQualifiedName parse(Path sourcePath) {
+ return parse(deriveRawFullyQualifiedName(sourcePath));
+ }
+
+ private static String deriveRawFullyQualifiedName(Path source) {
+ if (source.getNameCount() < 2) {
+ throw new IllegalArgumentException(
+ String.format(
+ "The resource path %s is too short. "
+ + "The path is expected to be <resource type>/<file name>.",
+ source));
+ }
+ String pathWithExtension =
+ source.subpath(source.getNameCount() - 2, source.getNameCount()).toString();
+ int extensionStart = pathWithExtension.indexOf('.');
+ if (extensionStart > 0) {
+ return pathWithExtension.substring(0, extensionStart);
+ }
+ return pathWithExtension;
+ }
+
+ // Grabs the extension portion of the path removed by deriveRawFullyQualifiedName.
+ private static String getSourceExtension(Path source) {
+ // TODO(corysmith): Find out if there is a filename parser utility.
+ String fileName = source.getFileName().toString();
+ int extensionStart = fileName.indexOf('.');
+ if (extensionStart > 0) {
+ return fileName.substring(extensionStart);
+ }
+ return "";
+ }
}
public static boolean isOverwritable(FullyQualifiedName name) {
@@ -306,11 +345,11 @@ public class FullyQualifiedName implements DataKey, Comparable<FullyQualifiedNam
* Non-values Android Resource have a well defined file layout: From the resource directory, they
* reside in &lt;resource type&gt;[-&lt;qualifier&gt;]/&lt;resource name&gt;[.extension]
*
- * @param sourceExtension The extension of the resource represented by the FullyQualifiedName
+ * @param source The original source of the file-based resource's FullyQualifiedName
* @return A string representation of the FullyQualifiedName with the provided extension.
*/
- public String toPathString(String sourceExtension) {
- // TODO(corysmith): Does the extension belong in the FullyQualifiedName?
+ public String toPathString(Path source) {
+ String sourceExtension = FullyQualifiedName.Factory.getSourceExtension(source);
return Paths.get(
DASH_JOINER.join(
ImmutableList.<String>builder()