diff options
Diffstat (limited to 'src/tools/android/java/com/google/devtools')
3 files changed, 44 insertions, 33 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/DataValueFile.java b/src/tools/android/java/com/google/devtools/build/android/DataValueFile.java index c5e9bf6e36..2f41931d32 100644 --- a/src/tools/android/java/com/google/devtools/build/android/DataValueFile.java +++ b/src/tools/android/java/com/google/devtools/build/android/DataValueFile.java @@ -83,7 +83,7 @@ public class DataValueFile implements DataResource, DataAsset { @Override public void writeResource(FullyQualifiedName key, AndroidDataWritingVisitor mergedDataWriter) throws IOException, MergingException { - mergedDataWriter.copyResource(source, key.toPathString(getSourceExtension())); + mergedDataWriter.copyResource(source, key.toPathString(source)); } @Override @@ -96,16 +96,6 @@ public class DataValueFile implements DataResource, DataAsset { + value.getSerializedSize(); } - private String getSourceExtension() { - // TODO(corysmith): Find out if there is a filename parser utility. - String fileName = source.getFileName().toString(); - int extensionStart = fileName.lastIndexOf('.'); - if (extensionStart > 0) { - return fileName.substring(extensionStart); - } - return ""; - } - @Override public DataResource combineWith(DataResource resource) { throw new IllegalArgumentException(getClass() + " does not combine."); 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 <resource type>[-<qualifier>]/<resource name>[.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() diff --git a/src/tools/android/java/com/google/devtools/build/android/ParsedAndroidData.java b/src/tools/android/java/com/google/devtools/build/android/ParsedAndroidData.java index abe1dfd395..876e4afa30 100644 --- a/src/tools/android/java/com/google/devtools/build/android/ParsedAndroidData.java +++ b/src/tools/android/java/com/google/devtools/build/android/ParsedAndroidData.java @@ -282,23 +282,6 @@ public class ParsedAndroidData { this.parseIds = parseIds; } - private static String deriveRawFullyQualifiedName(Path path) { - if (path.getNameCount() < 2) { - throw new IllegalArgumentException( - String.format( - "The resource path %s is too short. " - + "The path is expected to be <resource type>/<file name>.", - path)); - } - String pathWithExtension = - path.subpath(path.getNameCount() - 2, path.getNameCount()).toString(); - int extensionStart = pathWithExtension.lastIndexOf('.'); - if (extensionStart > 0) { - return pathWithExtension.substring(0, extensionStart); - } - return pathWithExtension; - } - @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { @@ -325,8 +308,7 @@ public class ParsedAndroidData { DataResourceXml.parse( xmlInputFactory, path, fqnFactory, overwritingConsumer, combiningResources); } else if (folderType != null) { - String rawFqn = deriveRawFullyQualifiedName(path); - FullyQualifiedName key = fqnFactory.parse(rawFqn); + FullyQualifiedName key = fqnFactory.parse(path); if (parseIds && ID_PROVIDING_RESOURCE_TYPES.contains(folderType) && path.getFileName().toString().endsWith(SdkConstants.DOT_XML)) { |