aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/xml
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-04-19 22:07:47 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-04-20 11:17:11 +0000
commit66cf13874a3c8f866aeace2d59231e30ca4a5032 (patch)
treee926e87be0c3f8ff8322cd2be20e5a534085010f /src/tools/android/java/com/google/devtools/build/android/xml
parent9b35d8a81b0d5cb92a22e7d9c7bf30a834711d7f (diff)
4 of 5: Serialization of UnwrittenMergedAndroidData.
Adding AndroidDataSerializer, the serialize_format proto, and KeyValueConsumers (utility class for keeping consumers straight). The serializtion is a bit more manual as previous experience has proven to me that simply writing all the resources into a proto map and pulling them out is not performant in the least. So, the serializer stores each message independent, the keys and then the values allowing for potential lazy loading and other optimizations in the future. Also adds tests for parsing and writing style resources. -- MOS_MIGRATED_REVID=120274904
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/xml')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/ArrayXmlResourceValue.java31
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java140
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/IdXmlResourceValue.java29
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/PluralXmlResourceValue.java35
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java36
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/StyleXmlResourceValue.java33
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/xml/StyleableXmlResourceValue.java35
7 files changed, 291 insertions, 48 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/ArrayXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/ArrayXmlResourceValue.java
index 2e1a10330f..edb30267f7 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/ArrayXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/ArrayXmlResourceValue.java
@@ -22,7 +22,10 @@ import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
@@ -42,12 +45,12 @@ import javax.xml.stream.events.XMLEvent;
*
* There are two flavors of Android Resource arrays:
* <ul>
- * <li>Typed arrays (http://developer.android.com/guide/topics/resources/more-resources
- * .html#TypedArray) which which are indicated by a &lt;array&gt; tag.</li>
- * <li>Integer array (http://developer.android.com/guide/topics/resources/more-resources
- * .html#IntegerArray) which are indicated by &lt;integer-array&gt; tag.</li>
- * <li>String array (http://developer.android.com/guide/topics/resources/string-resource
- * .html#StringArray) which are indicated by &lt;string-array&gt; tag.</li>
+ * <li>Typed arrays (http://developer.android.com/guide/topics/resources/more-resources
+ * .html#TypedArray) which which are indicated by a &lt;array&gt; tag.</li>
+ * <li>Integer array (http://developer.android.com/guide/topics/resources/more-resources
+ * .html#IntegerArray) which are indicated by &lt;integer-array&gt; tag.</li>
+ * <li>String array (http://developer.android.com/guide/topics/resources/string-resource
+ * .html#StringArray) which are indicated by &lt;string-array&gt; tag.</li>
* </ul>
*
* Both of these are accessed by R.array.&lt;name&gt; in java.
@@ -117,6 +120,10 @@ public class ArrayXmlResourceValue implements XmlResourceValue {
return new ArrayXmlResourceValue(arrayType, ImmutableList.copyOf(values));
}
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
+ return of(ArrayType.valueOf(proto.getValueType()), proto.getListValueList());
+ }
+
@Override
public void write(
FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) {
@@ -129,6 +136,18 @@ public class ArrayXmlResourceValue implements XmlResourceValue {
}
@Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ return XmlResourceValues.serializeProtoDataValue(
+ output,
+ XmlResourceValues.newProtoDataBuilder(source)
+ .setXmlValue(
+ SerializeFormat.DataValueXml.newBuilder()
+ .addAllListValue(values)
+ .setType(SerializeFormat.DataValueXml.XmlType.ARRAY)
+ .setValueType(arrayType.toString())));
+ }
+
+ @Override
public int hashCode() {
return Objects.hash(arrayType, values);
}
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java
index d245294cb4..f8273b37e6 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java
@@ -27,7 +27,11 @@ import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import com.google.protobuf.InvalidProtocolBufferException;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
@@ -49,22 +53,24 @@ import javax.xml.stream.events.XMLEvent;
/**
* Represents an Android Resource custom attribute.
*
- * <p>Attribute are the most complicated Android resource, and therefore the least documented. Most
- * of the information about them is found by reading the android compatibility library source. An
+ * <p>
+ * Attribute are the most complicated Android resource, and therefore the least documented. Most of
+ * the information about them is found by reading the android compatibility library source. An
* Attribute defines a parameter that can be passed into a view class -- as such you can think of
* attributes as creating slots for other resources to fit into. Each slot will have at least one
- * format, and can have multiples. Simple attributes (color, boolean, reference, dimension,
- * float, integer, string, and fraction) are defined as &lt;attr name="<em>name</em>"
- * format="<em>format</em>" /&gt; while the complex ones, flag and enum, have sub tags:
- * &lt;attr name="<em>name</em>" &gt&lt;flag name="<em>name</em>" value="<em>value</em>"&gt;
- * &lt;/attr&gt;.
+ * format, and can have multiples. Simple attributes (color, boolean, reference, dimension, float,
+ * integer, string, and fraction) are defined as &lt;attr name="<em>name</em>" format=
+ * "<em>format</em>" /&gt; while the complex ones, flag and enum, have sub tags: &lt;attr name=
+ * "<em>name</em>" &gt&lt;flag name="<em>name</em>" value="<em>value</em>"&gt; &lt;/attr&gt;.
*
- * <p>Attributes also have a double duty as defining validation logic for layout resources --
- * each layout attribute *must* have a corresponding attribute which will be used to validate the
+ * <p>
+ * Attributes also have a double duty as defining validation logic for layout resources -- each
+ * layout attribute *must* have a corresponding attribute which will be used to validate the
* value/resource reference defined in it.
*
- * <p>AttrXmlValue, due to the multiple types of attributes is actually a composite class that
- * contains multiple {@link XmlResourceValue} instances for each resource.
+ * <p>
+ * AttrXmlValue, due to the multiple types of attributes is actually a composite class that contains
+ * multiple {@link XmlResourceValue} instances for each resource.
*/
@Immutable
public class AttrXmlResourceValue implements XmlResourceValue {
@@ -143,6 +149,51 @@ public class AttrXmlResourceValue implements XmlResourceValue {
return of(ImmutableMap.copyOf(Arrays.asList(entries)));
}
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto)
+ throws InvalidProtocolBufferException {
+ Builder<String, ResourceXmlAttrValue> formats =
+ ImmutableMap.<String, AttrXmlResourceValue.ResourceXmlAttrValue>builder();
+ for (Entry<String, SerializeFormat.DataValueXml> entry : proto.getMappedXmlValue().entrySet()) {
+ switch (entry.getKey()) {
+ case FLAG:
+ formats.put(
+ entry.getKey(), FlagResourceXmlAttrValue.of(entry.getValue().getMappedStringValue()));
+ break;
+ case ENUM:
+ formats.put(
+ entry.getKey(), EnumResourceXmlAttrValue.of(entry.getValue().getMappedStringValue()));
+ break;
+ case REFERENCE:
+ formats.put(entry.getKey(), ReferenceResourceXmlAttrValue.of());
+ break;
+ case COLOR:
+ formats.put(entry.getKey(), ColorResourceXmlAttrValue.of());
+ break;
+ case BOOLEAN:
+ formats.put(entry.getKey(), BooleanResourceXmlAttrValue.of());
+ break;
+ case DIMENSION:
+ formats.put(entry.getKey(), DimensionResourceXmlAttrValue.of());
+ break;
+ case FLOAT:
+ formats.put(entry.getKey(), FloatResourceXmlAttrValue.of());
+ break;
+ case INTEGER:
+ formats.put(entry.getKey(), IntegerResourceXmlAttrValue.of());
+ break;
+ case STRING:
+ formats.put(entry.getKey(), StringResourceXmlAttrValue.of());
+ break;
+ case FRACTION:
+ formats.put(entry.getKey(), FractionResourceXmlAttrValue.of());
+ break;
+ default:
+ throw new InvalidProtocolBufferException("Unexpected format: " + entry.getKey());
+ }
+ }
+ return of(formats.build());
+ }
+
public static XmlResourceValue from(
StartElement attr, @Nullable String format, XMLEventReader eventReader)
throws XMLStreamException {
@@ -242,9 +293,26 @@ public class AttrXmlResourceValue implements XmlResourceValue {
mergedDataWriter.writeToValuesXml(key, iterable.append("</attr>"));
}
+ @Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ SerializeFormat.DataValue.Builder builder = XmlResourceValues.newProtoDataBuilder(source);
+ SerializeFormat.DataValueXml.Builder xmlValueBuilder =
+ SerializeFormat.DataValueXml.newBuilder();
+ xmlValueBuilder.setType(SerializeFormat.DataValueXml.XmlType.ATTR);
+ for (Entry<String, ResourceXmlAttrValue> entry : formats.entrySet()) {
+ xmlValueBuilder
+ .getMutableMappedXmlValue()
+ .put(entry.getKey(), entry.getValue().appendTo(builder.getXmlValueBuilder()));
+ }
+ builder.setXmlValue(xmlValueBuilder);
+ return XmlResourceValues.serializeProtoDataValue(output, builder);
+ }
+
@CheckReturnValue
interface ResourceXmlAttrValue {
FluentIterable<String> appendTo(FluentIterable<String> iterable);
+
+ SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder);
}
// TODO(corysmith): The ResourceXmlAttrValue implementors, other than enum and flag, share a
@@ -305,6 +373,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable.append(FluentIterable.from(values.entrySet()).transform(MAP_TO_ENUM));
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.putAllMappedStringValue(values).build();
+ }
}
/** Represents an Android Flag Attribute resource. */
@@ -366,6 +439,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
}
}));
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.putAllMappedStringValue(values).build();
+ }
}
/** Represents an Android Reference Attribute resource. */
@@ -387,6 +465,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Color Attribute resource. */
@@ -407,6 +490,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Boolean Attribute resource. */
@@ -427,6 +515,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Float Attribute resource. */
@@ -447,6 +540,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Dimension Attribute resource. */
@@ -468,6 +566,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Integer Attribute resource. */
@@ -488,6 +591,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android String Attribute resource. */
@@ -508,6 +616,11 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
/** Represents an Android Fraction Attribute resource. */
@@ -528,5 +641,10 @@ public class AttrXmlResourceValue implements XmlResourceValue {
public FluentIterable<String> appendTo(FluentIterable<String> iterable) {
return iterable;
}
+
+ @Override
+ public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) {
+ return builder.build();
+ }
}
}
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/IdXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/IdXmlResourceValue.java
index 2938256af4..07e3ad5940 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/IdXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/IdXmlResourceValue.java
@@ -18,7 +18,12 @@ import com.google.common.collect.ImmutableList;
import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
+import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import com.google.protobuf.CodedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import javax.annotation.concurrent.Immutable;
@@ -26,11 +31,12 @@ import javax.annotation.concurrent.Immutable;
/**
* Represents an Android Resource id.
*
- * <p>Ids (http://developer.android.com/guide/topics/resources/more-resources.html#Id) are
- * special -- unlike other resources they cannot be overridden. This is due to the
- * nature of their usage. Each id corresponds to context sensitive resource of component, meaning
- * that they have no intrinsic defined value. They exist to reference parts of other resources.
- * Ids can also be declared on the fly in components with the syntax @[+][package:]id/resource_name.
+ * <p>
+ * Ids (http://developer.android.com/guide/topics/resources/more-resources.html#Id) are special --
+ * unlike other resources they cannot be overridden. This is due to the nature of their usage. Each
+ * id corresponds to context sensitive resource of component, meaning that they have no intrinsic
+ * defined value. They exist to reference parts of other resources. Ids can also be declared on the
+ * fly in components with the syntax @[+][package:]id/resource_name.
*/
@Immutable
public class IdXmlResourceValue implements XmlResourceValue {
@@ -52,6 +58,19 @@ public class IdXmlResourceValue implements XmlResourceValue {
}
@Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ SerializeFormat.DataValue value =
+ XmlResourceValues.newProtoDataBuilder(source)
+ .setXmlValue(
+ SerializeFormat.DataValueXml.newBuilder()
+ .setType(SerializeFormat.DataValueXml.XmlType.ID))
+ .build();
+ value.writeDelimitedTo(output);
+ return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
+ + value.getSerializedSize();
+ }
+
+ @Override
public String toString() {
return MoreObjects.toStringHelper(getClass()).toString();
}
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/PluralXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/PluralXmlResourceValue.java
index e47b5611c0..64397ef35f 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/PluralXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/PluralXmlResourceValue.java
@@ -21,7 +21,14 @@ import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
+import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import com.google.devtools.build.android.proto.SerializeFormat.DataValue.Builder;
+import com.google.devtools.build.android.proto.SerializeFormat.DataValueXml.XmlType;
+import com.google.protobuf.CodedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Objects;
@@ -32,10 +39,10 @@ import javax.annotation.concurrent.Immutable;
/**
* Represents an Android Plural Resource.
*
- * <p>Plurals are a localization construct (http://developer.android.com/guide/topics/resources/
- * string-resource.html#Plurals) that are basically a map of key to
- * value. They are defined in xml as:
- * <code>
+ * <p>
+ * Plurals are a localization construct (http://developer.android.com/guide/topics/resources/
+ * string-resource.html#Plurals) that are basically a map of key to value. They are defined in xml
+ * as: <code>
* &lt;plurals name="plural_name"&gt;
* &lt;item quantity=["zero" | "one" | "two" | "few" | "many" | "other"]&gt;
* text_string
@@ -95,4 +102,24 @@ public class PluralXmlResourceValue implements XmlResourceValue {
public String toString() {
return MoreObjects.toStringHelper(getClass()).add("values", values).toString();
}
+
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
+ return of(ImmutableMap.copyOf(proto.getMappedStringValue()));
+ }
+
+ @Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ Builder builder = XmlResourceValues.newProtoDataBuilder(source);
+ SerializeFormat.DataValue value =
+ builder
+ .setXmlValue(
+ builder
+ .getXmlValueBuilder()
+ .setType(XmlType.PLURAL)
+ .putAllMappedStringValue(values))
+ .build();
+ value.writeDelimitedTo(output);
+ return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
+ + value.getSerializedSize();
+ }
}
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java
index dadba36c9f..ff8c6ad742 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java
@@ -18,9 +18,13 @@ import com.google.common.collect.ImmutableList;
import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
+import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
import com.android.resources.ResourceType;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
@@ -31,14 +35,14 @@ import javax.xml.namespace.QName;
/**
* Represents a simple Android resource xml value.
*
- * <p>There is a class of resources that are simple name/value pairs:
- * string (http://developer.android.com/guide/topics/resources/string-resource.html),
- * bool (http://developer.android.com/guide/topics/resources/more-resources.html#Bool),
- * color (http://developer.android.com/guide/topics/resources/more-resources.html#Color), and
- * dimen (http://developer.android.com/guide/topics/resources/more-resources.html#Dimension).
- * These are defined in xml as &lt;<em>resource type</em> name="<em>name</em>"
- * value="<em>value</em>"&gt;. In the interest of keeping the parsing svelte, these are
- * represented by a single class.
+ * <p>
+ * There is a class of resources that are simple name/value pairs: string
+ * (http://developer.android.com/guide/topics/resources/string-resource.html), bool
+ * (http://developer.android.com/guide/topics/resources/more-resources.html#Bool), color
+ * (http://developer.android.com/guide/topics/resources/more-resources.html#Color), and dimen
+ * (http://developer.android.com/guide/topics/resources/more-resources.html#Dimension). These are
+ * defined in xml as &lt;<em>resource type</em> name="<em>name</em>" value="<em>value</em>"&gt;. In
+ * the interest of keeping the parsing svelte, these are represented by a single class.
*/
@Immutable
public class SimpleXmlResourceValue implements XmlResourceValue {
@@ -125,6 +129,22 @@ public class SimpleXmlResourceValue implements XmlResourceValue {
valueType.tagName.getLocalPart())));
}
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
+ return of(Type.valueOf(proto.getValueType()), proto.getValue());
+ }
+
+ @Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ SerializeFormat.DataValue.Builder builder = XmlResourceValues.newProtoDataBuilder(source);
+ builder.setXmlValue(
+ builder
+ .getXmlValueBuilder()
+ .setType(SerializeFormat.DataValueXml.XmlType.SIMPLE)
+ .setValue(value)
+ .setValueType(valueType.name()));
+ return XmlResourceValues.serializeProtoDataValue(output, builder);
+ }
+
@Override
public int hashCode() {
return Objects.hash(valueType, value);
diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/StyleXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/StyleXmlResourceValue.java
index ce11bbbda1..cb73d07cd1 100644
--- a/src/tools/android/java/com/google/devtools/build/android/xml/StyleXmlResourceValue.java
+++ b/src/tools/android/java/com/google/devtools/build/android/xml/StyleXmlResourceValue.java
@@ -21,7 +21,11 @@ import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
+import com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Map;
import java.util.Map.Entry;
@@ -33,12 +37,12 @@ import javax.annotation.concurrent.Immutable;
/**
* Represents an Android Style Resource.
*
- * <p>Styles (http://developer.android.com/guide/topics/resources/style-resource.html) define a
- * look and feel for a layout or other ui construct. They are effectively a s set of values that
+ * <p>
+ * Styles (http://developer.android.com/guide/topics/resources/style-resource.html) define a look
+ * and feel for a layout or other ui construct. They are effectively a s set of values that
* correspond to &lt;attr&gt; resources defined either in the base android framework or in other
* resources. They also allow inheritance on other styles. For a style to valid in a given resource
- * pass, they must only contain definer attributes with acceptable values.
- * <code>
+ * pass, they must only contain definer attributes with acceptable values. <code>
* &lt;resources&gt;
* &lt;style name="CustomText" parent="@style/Text"&gt;
* &lt;item name="android:textSize"&gt;20sp&lt;/item&gt;
@@ -64,7 +68,11 @@ public class StyleXmlResourceValue implements XmlResourceValue {
return new StyleXmlResourceValue(parent, ImmutableMap.copyOf(values));
}
- private StyleXmlResourceValue(String parent, ImmutableMap<String, String> values) {
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
+ return of(proto.hasValue() ? proto.getValue() : null, proto.getMappedStringValue());
+ }
+
+ private StyleXmlResourceValue(@Nullable String parent, ImmutableMap<String, String> values) {
this.parent = parent;
this.values = values;
}
@@ -79,12 +87,25 @@ public class StyleXmlResourceValue implements XmlResourceValue {
String.format("<!-- %s -->", source),
parent == null || parent.isEmpty()
? String.format("<style name='%s'>", key.name())
- : String.format("<style name='%s' parent='%s'>", key.name(), parent)))
+ : String.format("<style name='%s' parent='@%s'>", key.name(), parent)))
.append(FluentIterable.from(values.entrySet()).transform(ENTRY_TO_ITEM))
.append("</style>"));
}
@Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ SerializeFormat.DataValueXml.Builder xmlValueBuilder =
+ SerializeFormat.DataValueXml.newBuilder()
+ .setType(SerializeFormat.DataValueXml.XmlType.STYLE)
+ .putAllMappedStringValue(values);
+ if (parent != null && !parent.isEmpty()) {
+ xmlValueBuilder.setValue(parent);
+ }
+ return XmlResourceValues.serializeProtoDataValue(
+ output, XmlResourceValues.newProtoDataBuilder(source).setXmlValue(xmlValueBuilder));
+ }
+
+ @Override
public int hashCode() {
return Objects.hash(parent, values);
}
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 4f8fadc081..e46eaf3065 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
@@ -22,7 +22,11 @@ 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 com.google.devtools.build.android.XmlResourceValues;
+import com.google.devtools.build.android.proto.SerializeFormat;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
@@ -34,25 +38,25 @@ import javax.annotation.concurrent.Immutable;
/**
* Represent an Android styleable resource.
*
- * <p>Styleable resources are groups of attributes that can be applied to views. They are, for the
- * most part, vaguely documented (http://developer.android.com/training/custom-views/create-view
- * .html#customattr). It's worth noting that attributes declared inside a
- * &lt;declare-styleable&gt; tags, for example;
- * <code>
+ * <p>
+ * Styleable resources are groups of attributes that can be applied to views. They are, for the most
+ * part, vaguely documented (http://developer.android.com/training/custom-views/create-view
+ * .html#customattr). It's worth noting that attributes declared inside a &lt;declare-styleable&gt;
+ * tags, for example; <code>
* <declare-styleable name="PieChart">
* <attr name="showText" format="boolean" />
* </declare-styleable>
* </code>
*
- * Can also be seen as:
- * <code>
+ * Can also be seen as: <code>
* <attr name="showText" format="boolean" />
* <declare-styleable name="PieChart">
* <attr name="showText"/>
* </declare-styleable>
* </code>
*
- * <p>The StyleableXmlValue only contains names of the attributes it holds, not definitions.
+ * <p>
+ * The StyleableXmlValue only contains names of the attributes it holds, not definitions.
*/
@Immutable
public class StyleableXmlResourceValue implements XmlResourceValue {
@@ -94,6 +98,21 @@ public class StyleableXmlResourceValue implements XmlResourceValue {
}
@Override
+ public int serializeTo(Path source, OutputStream output) throws IOException {
+ return XmlResourceValues.serializeProtoDataValue(
+ output,
+ XmlResourceValues.newProtoDataBuilder(source)
+ .setXmlValue(
+ SerializeFormat.DataValueXml.newBuilder()
+ .setType(SerializeFormat.DataValueXml.XmlType.STYLEABLE)
+ .addAllListValue(attrs)));
+ }
+
+ public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
+ return of(proto.getListValueList());
+ }
+
+ @Override
public int hashCode() {
return attrs.hashCode();
}