aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-04-05 17:52:42 +0000
committerGravatar Lukacs Berki <lberki@google.com>2016-04-07 11:40:01 +0000
commit0f86dc815c46f5bde00e42fc875ed0502a1fac44 (patch)
tree54661f592792a5c35f6e7b5ec0134f995dcaebf4 /src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java
parentc0b4e2c5cc15c749afba7a238d6337c7c4d66004 (diff)
4.25 of 5: Writing of UnwrittenMergedAndroidData
Introduces the AndroidDataWriter and AndroidDataWritingVisitor to abstract the io operations from the data classes. Necessary refactoring to the stubbed write method on DataValue in DataAsset.writeAsset and DataResource.writeResource. New interface for the AttributeValues to reflect the simplifications of writing Resource Attributes. Of special note is the fact all xml is written into a single file, values.xml. This is following the Gradle convention and aapt has demonstrated a preference of only reading a values.xml and ignoring all other xml files in the values directory. Unless profiling demonstrates an advantage to writing multiple files (which I doubt), this merger carries on this convention. -- MOS_MIGRATED_REVID=119066611
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java
index f1a0486307..76011237f7 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java
@@ -14,15 +14,23 @@
package com.google.devtools.build.android.xml;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Ordering;
+import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
-import java.io.Writer;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
+
/**
* Represent an Android styleable resource.
*
@@ -46,26 +54,42 @@ import java.util.Objects;
*
* <p>The StyleableXmlValue only contains names of the attributes it holds, not definitions.
*/
+@Immutable
public class StyleableXmlResourceValue implements XmlResourceValue {
- private final List<String> attrs;
+ public static final Function<String, String> ITEM_TO_ATTR =
+ new Function<String, String>() {
+ @Nullable
+ @Override
+ public String apply(@Nullable String input) {
+ return String.format("<attr name='%s'/>", input);
+ }
+ };
+ private final ImmutableList<String> attrs;
- private StyleableXmlResourceValue(List<String> attrs) {
+ private StyleableXmlResourceValue(ImmutableList<String> attrs) {
this.attrs = attrs;
}
public static XmlResourceValue of(List<String> attrs) {
- return new StyleableXmlResourceValue(attrs);
+ return new StyleableXmlResourceValue(ImmutableList.copyOf(attrs));
}
@VisibleForTesting
public static XmlResourceValue of(String... attrs) {
- return new StyleableXmlResourceValue(Arrays.asList(attrs));
+ return new StyleableXmlResourceValue(
+ Ordering.natural().immutableSortedCopy(Arrays.asList(attrs)));
}
@Override
- public void write(Writer buffer, FullyQualifiedName name) {
- // TODO(corysmith): Implement write.
- throw new UnsupportedOperationException();
+ public void write(
+ FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) {
+ mergedDataWriter.writeToValuesXml(
+ key,
+ FluentIterable.of(
+ String.format("<!-- %s -->", source),
+ String.format("<declare-styleable name='%s'>", key.name()))
+ .append(FluentIterable.from(attrs).transform(ITEM_TO_ATTR))
+ .append("</declare-styleable>"));
}
@Override