diff options
author | corysmith <corysmith@google.com> | 2018-02-16 15:48:49 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-16 15:50:13 -0800 |
commit | d18d3e2f83f9d582858a3edab7a450c60044028c (patch) | |
tree | 16ff3c0dff1e87a60ddcafaea2917623d48ef096 /src/tools/android/java/com/google/devtools/build/android/xml | |
parent | d7a56179ab35bf21ee6d77d87bb0096bef042175 (diff) |
Automated rollback of commit f672a31b8b19baab95373e4f2f6d110aa8b8f0fb.
*** Reason for rollback ***
Unclassified general breakages in tests. Rolling back for further investigation.
*** Original change description ***
Normalized the serialization proto to save space and allow greater versatility in storage.
RELNOTES: None
PiperOrigin-RevId: 186057879
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/xml')
10 files changed, 288 insertions, 152 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 f977c7b315..fb74fd2208 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 @@ -29,8 +29,6 @@ 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.DataValueXml; -import com.google.devtools.build.android.proto.SerializeFormat.DataValueXml.XmlType; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; @@ -47,15 +45,14 @@ import javax.xml.stream.events.XMLEvent; /** * Represents an Android resource array. * - * <p>There are two flavors of Android Resource arrays: - * + * 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 <array> tag. - * <li>Integer array (http://developer.android.com/guide/topics/resources/more-resources - * .html#IntegerArray) which are indicated by <integer-array> tag. - * <li>String array (http://developer.android.com/guide/topics/resources/string-resource - * .html#StringArray) which are indicated by <string-array> tag. + * <li>Typed arrays (http://developer.android.com/guide/topics/resources/more-resources + * .html#TypedArray) which which are indicated by a <array> tag.</li> + * <li>Integer array (http://developer.android.com/guide/topics/resources/more-resources + * .html#IntegerArray) which are indicated by <integer-array> tag.</li> + * <li>String array (http://developer.android.com/guide/topics/resources/string-resource + * .html#StringArray) which are indicated by <string-array> tag.</li> * </ul> * * Both of these are accessed by R.array.<name> in java. @@ -65,19 +62,9 @@ public class ArrayXmlResourceValue implements XmlResourceValue { private static final QName TAG_INTEGER_ARRAY = QName.valueOf("integer-array"); private static final QName TAG_ARRAY = QName.valueOf("array"); private static final QName TAG_STRING_ARRAY = QName.valueOf("string-array"); - - @Override - public void writeTo(OutputStream out) throws IOException { - DataValueXml.newBuilder() - .addAllListValue(values) - .setType(XmlType.ARRAY) - .putAllAttribute(attributes) - .setValueType(arrayType.toString()) - .build() - .writeDelimitedTo(out); - } - - /** Enumerates the different types of array parentTags. */ + /** + * Enumerates the different types of array parentTags. + */ public enum ArrayType { INTEGER_ARRAY(TAG_INTEGER_ARRAY), ARRAY(TAG_ARRAY), @@ -118,7 +105,7 @@ public class ArrayXmlResourceValue implements XmlResourceValue { } public static XmlResourceValue of(ArrayType arrayType, List<String> values) { - return of(arrayType, values, ImmutableMap.of()); + return of(arrayType, values, ImmutableMap.<String, String>of()); } public static XmlResourceValue of( @@ -151,20 +138,20 @@ public class ArrayXmlResourceValue implements XmlResourceValue { } } - return of(ArrayType.ARRAY, items, ImmutableMap.of()); + return of( + ArrayType.ARRAY, + items, + ImmutableMap.of()); } @Override public void write( FullyQualifiedName key, DataSource source, AndroidDataWritingVisitor mergedDataWriter) { - ValuesResourceDefinition definition = - mergedDataWriter - .define(key) - .derivedFrom(source) - .startTag(arrayType.tagName) - .named(key) - .addAttributesFrom(attributes.entrySet()) - .closeTag(); + ValuesResourceDefinition definition = mergedDataWriter.define(key).derivedFrom(source) + .startTag(arrayType.tagName) + .named(key) + .addAttributesFrom(attributes.entrySet()) + .closeTag(); for (String value : values) { definition = definition @@ -178,6 +165,21 @@ public class ArrayXmlResourceValue implements XmlResourceValue { } @Override + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + return XmlResourceValues.serializeProtoDataValue( + output, + XmlResourceValues.newSerializableDataValueBuilder(sourceId) + .setXmlValue( + SerializeFormat.DataValueXml.newBuilder() + .addAllListValue(values) + .setType(SerializeFormat.DataValueXml.XmlType.ARRAY) + .putAllNamespace(namespaces.asMap()) + .putAllAttribute(attributes) + .setValueType(arrayType.toString()))); + } + + @Override public int hashCode() { return Objects.hash(arrayType, values, attributes); } 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 96ac31c947..f2645ac0a3 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 @@ -404,16 +404,23 @@ public class AttrXmlResourceValue implements XmlResourceValue { } } + @SuppressWarnings("deprecation") @Override - public void writeTo(OutputStream out) throws IOException { + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + SerializeFormat.DataValue.Builder builder = + XmlResourceValues.newSerializableDataValueBuilder(sourceId); SerializeFormat.DataValueXml.Builder xmlValueBuilder = SerializeFormat.DataValueXml.newBuilder(); - xmlValueBuilder.setType(SerializeFormat.DataValueXml.XmlType.ATTR); + xmlValueBuilder + .setType(SerializeFormat.DataValueXml.XmlType.ATTR) + .putAllNamespace(namespaces.asMap()); for (Entry<String, ResourceXmlAttrValue> entry : formats.entrySet()) { xmlValueBuilder.putMappedXmlValue( - entry.getKey(), entry.getValue().appendTo(SerializeFormat.DataValueXml.newBuilder())); + entry.getKey(), entry.getValue().appendTo(builder.getXmlValueBuilder())); } - xmlValueBuilder.build().writeDelimitedTo(out); + builder.setXmlValue(xmlValueBuilder); + return XmlResourceValues.serializeProtoDataValue(output, builder); } @Override 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 e3fe65aa02..c3fb2faefa 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 @@ -20,8 +20,11 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml.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.util.Objects; @@ -91,8 +94,20 @@ public class IdXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.DataValueXml.newBuilder().setType(XmlType.ID).build().writeDelimitedTo(out); + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + Builder xmlValue = + SerializeFormat.DataValueXml.newBuilder() + .setType(XmlType.ID) + .putAllNamespace(namespaces.asMap()); + if (value != null) { + xmlValue.setValue(value); + } + SerializeFormat.DataValue dataValue = + XmlResourceValues.newSerializableDataValueBuilder(sourceId).setXmlValue(xmlValue).build(); + dataValue.writeDelimitedTo(output); + return CodedOutputStream.computeUInt32SizeNoTag(dataValue.getSerializedSize()) + + dataValue.getSerializedSize(); } @Override diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/Namespaces.java b/src/tools/android/java/com/google/devtools/build/android/xml/Namespaces.java index 10da5ff225..82e8810139 100644 --- a/src/tools/android/java/com/google/devtools/build/android/xml/Namespaces.java +++ b/src/tools/android/java/com/google/devtools/build/android/xml/Namespaces.java @@ -16,13 +16,8 @@ package com.google.devtools.build.android.xml; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.android.DataResourceXml; -import com.google.devtools.build.android.Writeable; 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.XmlNamespaces; -import java.io.IOException; -import java.io.OutputStream; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -44,23 +39,11 @@ import javax.xml.stream.events.StartElement; * resources tag to combining multiple {@link DataResourceXml}s, the Namespaces must be tracked and * kept with each value. */ -public class Namespaces implements Iterable<Entry<String, String>>, Writeable { +public class Namespaces implements Iterable<Entry<String, String>> { private static final Logger logger = Logger.getLogger(Namespaces.class.getCanonicalName()); private static final Namespaces EMPTY_INSTANCE = new Namespaces(ImmutableMap.<String, String>of()); - @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.XmlNamespaces.newBuilder() - .putAllNamespace(prefixToUri) - .build() - .writeDelimitedTo(out); - } - - public static Namespaces fromProto(XmlNamespaces xmlNamespaces) { - return from(xmlNamespaces.getNamespaceMap()); - } - /** Collects prefix and uri pairs from elements. */ public static class Collector { private Map<String, String> prefixToUri = new HashMap<>(); 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 7c1b6532e3..b912cc141e 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 @@ -24,8 +24,10 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml.XmlType; +import com.google.protobuf.CodedOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; @@ -155,13 +157,23 @@ public class PluralXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.DataValueXml.newBuilder() - .setType(XmlType.PLURAL) - .putAllAttribute(attributes) - .putAllMappedStringValue(values) - .build() - .writeDelimitedTo(out); + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + SerializeFormat.DataValue.Builder builder = + XmlResourceValues.newSerializableDataValueBuilder(sourceId); + SerializeFormat.DataValue value = + builder + .setXmlValue( + builder + .getXmlValueBuilder() + .setType(XmlType.PLURAL) + .putAllNamespace(namespaces.asMap()) + .putAllAttribute(attributes) + .putAllMappedStringValue(values)) + .build(); + value.writeDelimitedTo(output); + return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize()) + + value.getSerializedSize(); } @Override diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/PublicXmlResourceValue.java b/src/tools/android/java/com/google/devtools/build/android/xml/PublicXmlResourceValue.java index c1e3eb40a0..af4184d88a 100644 --- a/src/tools/android/java/com/google/devtools/build/android/xml/PublicXmlResourceValue.java +++ b/src/tools/android/java/com/google/devtools/build/android/xml/PublicXmlResourceValue.java @@ -19,11 +19,13 @@ import com.google.common.base.MoreObjects; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; import com.google.devtools.build.android.AndroidDataWritingVisitor; import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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; @@ -31,7 +33,6 @@ import java.util.EnumMap; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; -import java.util.stream.Collectors; /** * Represents an Android resource <public> xml tag. @@ -138,19 +139,23 @@ public class PublicXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.DataValueXml.newBuilder() - .setType(SerializeFormat.DataValueXml.XmlType.PUBLIC) - .putAllMappedStringValue( - typeToId - .entrySet() - .stream() - .collect( - Collectors.toMap( - e -> e.getKey().toString(), - e -> e.getValue().transform(Object::toString).or(MISSING_ID_VALUE)))) - .build() - .writeDelimitedTo(out); + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + Map<String, String> assignments = Maps.newLinkedHashMapWithExpectedSize(typeToId.size()); + for (Entry<ResourceType, Optional<Integer>> entry : typeToId.entrySet()) { + Optional<Integer> value = entry.getValue(); + String stringValue = value.isPresent() ? value.get().toString() : MISSING_ID_VALUE; + assignments.put(entry.getKey().toString(), stringValue); + } + SerializeFormat.DataValue.Builder builder = + XmlResourceValues.newSerializableDataValueBuilder(sourceId); + builder.setXmlValue( + builder + .getXmlValueBuilder() + .setType(SerializeFormat.DataValueXml.XmlType.PUBLIC) + .putAllNamespace(namespaces.asMap()) + .putAllMappedStringValue(assignments)); + return XmlResourceValues.serializeProtoDataValue(output, builder); } @Override @@ -173,7 +178,7 @@ public class PublicXmlResourceValue implements XmlResourceValue { } return of(combined); } - + @Override public String asConflictStringWith(DataSource source) { return source.asConflictString(); diff --git a/src/tools/android/java/com/google/devtools/build/android/xml/ResourcesAttribute.java b/src/tools/android/java/com/google/devtools/build/android/xml/ResourcesAttribute.java index 75a6c0207e..a54667ecdb 100644 --- a/src/tools/android/java/com/google/devtools/build/android/xml/ResourcesAttribute.java +++ b/src/tools/android/java/com/google/devtools/build/android/xml/ResourcesAttribute.java @@ -21,7 +21,9 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml.Builder; import com.google.errorprone.annotations.Immutable; import java.io.IOException; import java.io.OutputStream; @@ -38,15 +40,13 @@ public class ResourcesAttribute implements XmlResourceValue { public String combine(String first, String second); } - private static final Combiner COMMA_SEPARATED_COMBINER = - new Combiner() { - private final Joiner joiner = Joiner.on(','); - - @Override - public String combine(String first, String second) { - return joiner.join(first, second); - } - }; + private static final Combiner COMMA_SEPARATED_COMBINER = new Combiner() { + private final Joiner joiner = Joiner.on(','); + @Override + public String combine(String first, String second) { + return joiner.join(first, second); + } + }; /** * Represents the semantic meaning of an xml attribute and how it is combined with other @@ -92,7 +92,9 @@ public class ResourcesAttribute implements XmlResourceValue { Map.Entry<String, String> attribute = Iterables.getOnlyElement(proto.getAttributeMap().entrySet()); return new ResourcesAttribute( - AttributeType.valueOf(proto.getValueType()), attribute.getKey(), attribute.getValue()); + AttributeType.valueOf(proto.getValueType()), + attribute.getKey(), + attribute.getValue()); } private final AttributeType type; @@ -112,13 +114,19 @@ public class ResourcesAttribute implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.DataValueXml.newBuilder() - .setType(SerializeFormat.DataValueXml.XmlType.RESOURCES_ATTRIBUTE) - .setValueType(type.name()) - .putAttribute(name, value) - .build() - .writeDelimitedTo(out); + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + SerializeFormat.DataValue.Builder builder = + XmlResourceValues.newSerializableDataValueBuilder(sourceId); + Builder xmlValueBuilder = + builder + .getXmlValueBuilder() + .putAllNamespace(namespaces.asMap()) + .setType(SerializeFormat.DataValueXml.XmlType.RESOURCES_ATTRIBUTE) + .setValueType(type.name()) + .putAttribute(name, value); + builder.setXmlValue(xmlValueBuilder); + return XmlResourceValues.serializeProtoDataValue(output, builder); } @Override 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 55bef85bc1..32147cd49c 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 @@ -27,10 +27,9 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml; import com.google.devtools.build.android.proto.SerializeFormat.DataValueXml.Builder; -import com.google.devtools.build.android.proto.SerializeFormat.DataValueXml.XmlType; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; @@ -42,7 +41,8 @@ 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 + * <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 @@ -66,27 +66,106 @@ public class SimpleXmlResourceValue implements XmlResourceValue { static final QName TAG_RAW = QName.valueOf("raw"); static final QName TAG_STRING = QName.valueOf("string"); - /** Provides an enumeration resource type. */ + /** Provides an enumeration resource type and simple value validation. */ public enum Type { - BOOL(TAG_BOOL), - COLOR(TAG_COLOR), - DIMEN(TAG_DIMEN), - DRAWABLE(TAG_DRAWABLE), - FRACTION(TAG_FRACTION), - INTEGER(TAG_INTEGER), - ITEM(TAG_ITEM), - LAYOUT(TAG_LAYOUT), - MENU(TAG_MENU), - MIPMAP(TAG_MIPMAP), - PUBLIC(TAG_PUBLIC), - RAW(TAG_RAW), - STRING(TAG_STRING); + BOOL(TAG_BOOL) { + @Override + public boolean validate(String value) { + final String cleanValue = value.toLowerCase().trim(); + return "true".equals(cleanValue) || "false".equals(cleanValue); + } + }, + COLOR(TAG_COLOR) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the hex color. + return true; + } + }, + DIMEN(TAG_DIMEN) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the dimension type. + return true; + } + }, + DRAWABLE(TAG_DRAWABLE) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the drawable type. + return true; + } + }, + FRACTION(TAG_FRACTION) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the fraction type. + return true; + } + }, + INTEGER(TAG_INTEGER) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the integer type. + return true; + } + }, + ITEM(TAG_ITEM) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the item type. + return true; + } + }, + LAYOUT(TAG_LAYOUT) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the layout type. + return true; + } + }, + MENU(TAG_MENU) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the menu type. + return true; + } + }, + MIPMAP(TAG_MIPMAP) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the mipmap type. + return true; + } + }, + PUBLIC(TAG_PUBLIC) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the public type. + return true; + } + }, + RAW(TAG_RAW) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the raw type. + return true; + } + }, + STRING(TAG_STRING) { + @Override + public boolean validate(String value) { + return true; + } + }; private final QName tagName; Type(QName tagName) { this.tagName = tagName; } + abstract boolean validate(String value); + public static Type from(ResourceType resourceType) { for (Type valueType : values()) { if (valueType.tagName.getLocalPart().equals(resourceType.getName())) { @@ -98,7 +177,8 @@ public class SimpleXmlResourceValue implements XmlResourceValue { throw new IllegalArgumentException( String.format( "%s resource type not found in available types: %s", - resourceType, Arrays.toString(values()))); + resourceType, + Arrays.toString(values()))); } } @@ -120,7 +200,8 @@ public class SimpleXmlResourceValue implements XmlResourceValue { return of(Type.ITEM, ImmutableMap.of("type", resourceType.getName(), "format", format), value); } - public static XmlResourceValue itemWithValue(ResourceType resourceType, String value) { + public static XmlResourceValue itemWithValue( + ResourceType resourceType, String value) { return of(Type.ITEM, ImmutableMap.of("type", resourceType.getName()), value); } @@ -184,11 +265,11 @@ public class SimpleXmlResourceValue implements XmlResourceValue { String.format(";%s,%d,%d", span.getTag(), span.getFirstChar(), span.getLastChar())); } stringValue = stringBuilder.toString(); - } else if ((resourceType == ResourceType.COLOR || resourceType == ResourceType.DRAWABLE) - && item.hasPrim()) { + } else if ((resourceType == ResourceType.COLOR + || resourceType == ResourceType.DRAWABLE) && item.hasPrim()) { stringValue = String.format("#%1$8s", Integer.toHexString(item.getPrim().getData())).replace(' ', '0'); - } else if (resourceType == ResourceType.INTEGER && item.hasPrim()) { + } else if (resourceType == ResourceType.INTEGER && item.hasPrim()){ stringValue = Integer.toString(item.getPrim().getData()); } else if (resourceType == ResourceType.BOOL && item.hasPrim()) { stringValue = item.getPrim().getData() == 0 ? "false" : "true"; @@ -201,7 +282,10 @@ public class SimpleXmlResourceValue implements XmlResourceValue { String.format("'%s' is not a valid resource type.", resourceType)); } - return of(Type.valueOf(resourceType.toString().toUpperCase()), ImmutableMap.of(), stringValue); + return of( + Type.valueOf(resourceType.toString().toUpperCase()), + ImmutableMap.of(), + stringValue); } @Override @@ -210,20 +294,23 @@ public class SimpleXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - final Builder builder = - DataValueXml.newBuilder() - .setType(XmlType.SIMPLE) - .setValueType(valueType.name()) + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + SerializeFormat.DataValue.Builder builder = + XmlResourceValues.newSerializableDataValueBuilder(sourceId); + Builder xmlValueBuilder = + builder + .getXmlValueBuilder() + .putAllNamespace(namespaces.asMap()) + .setType(SerializeFormat.DataValueXml.XmlType.SIMPLE) // TODO(corysmith): Find a way to avoid writing strings to the serialized format // it's inefficient use of space and costs more when deserializing. .putAllAttribute(attributes); - if (value != null) { - builder.setValue(value); + xmlValueBuilder.setValue(value); } - - builder.build().writeDelimitedTo(out); + builder.setXmlValue(xmlValueBuilder.setValueType(valueType.name())); + return XmlResourceValues.serializeProtoDataValue(output, builder); } @Override @@ -255,7 +342,7 @@ public class SimpleXmlResourceValue implements XmlResourceValue { public XmlResourceValue combineWith(XmlResourceValue value) { throw new IllegalArgumentException(this + " is not a combinable resource."); } - + @Override public String asConflictStringWith(DataSource source) { if (value != null) { 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 6929c9d588..5cd0f6e0a5 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 @@ -24,6 +24,7 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml.XmlType; import java.io.IOException; @@ -38,7 +39,8 @@ 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 + * <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 <attr> 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 @@ -152,15 +154,19 @@ public class StyleXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { SerializeFormat.DataValueXml.Builder xmlValueBuilder = SerializeFormat.DataValueXml.newBuilder() .setType(XmlType.STYLE) + .putAllNamespace(namespaces.asMap()) .putAllMappedStringValue(values); if (parent != null) { xmlValueBuilder.setValue(parent); } - xmlValueBuilder.build().writeDelimitedTo(out); + return XmlResourceValues.serializeProtoDataValue( + output, + XmlResourceValues.newSerializableDataValueBuilder(sourceId).setXmlValue(xmlValueBuilder)); } @Override @@ -189,7 +195,7 @@ public class StyleXmlResourceValue implements XmlResourceValue { public XmlResourceValue combineWith(XmlResourceValue value) { throw new IllegalArgumentException(this + " is not a combinable resource."); } - + @Override public String asConflictStringWith(DataSource source) { return source.asConflictString(); 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 5006600a48..c9661690cf 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 @@ -27,6 +27,7 @@ import com.google.devtools.build.android.AndroidResourceSymbolSink; import com.google.devtools.build.android.DataSource; 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.DataValueXml.XmlType; import java.io.IOException; @@ -37,7 +38,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; -import java.util.stream.Collectors; import javax.annotation.concurrent.Immutable; /** @@ -65,11 +65,22 @@ public class StyleableXmlResourceValue implements XmlResourceValue { static final Function<Entry<FullyQualifiedName, Boolean>, SerializeFormat.DataKey> FULLY_QUALIFIED_NAME_TO_DATA_KEY = - input -> input.getKey().toSerializedBuilder().setReference(input.getValue()).build(); + new Function<Entry<FullyQualifiedName, Boolean>, SerializeFormat.DataKey>() { + @Override + public SerializeFormat.DataKey apply(Entry<FullyQualifiedName, Boolean> input) { + return input.getKey().toSerializedBuilder().setReference(input.getValue()).build(); + } + }; static final Function<SerializeFormat.DataKey, Entry<FullyQualifiedName, Boolean>> DATA_KEY_TO_FULLY_QUALIFIED_NAME = - input -> new SimpleEntry<>(FullyQualifiedName.fromProto(input), input.getReference()); + new Function<SerializeFormat.DataKey, Entry<FullyQualifiedName, Boolean>>() { + @Override + public Entry<FullyQualifiedName, Boolean> apply(SerializeFormat.DataKey input) { + FullyQualifiedName key = FullyQualifiedName.fromProto(input); + return new SimpleEntry<FullyQualifiedName, Boolean>(key, input.getReference()); + } + }; private final ImmutableMap<FullyQualifiedName, Boolean> attrs; @@ -134,17 +145,17 @@ public class StyleableXmlResourceValue implements XmlResourceValue { } @Override - public void writeTo(OutputStream out) throws IOException { - SerializeFormat.DataValueXml.newBuilder() - .setType(XmlType.STYLEABLE) - .addAllReferences( - attrs - .entrySet() - .stream() - .map(FULLY_QUALIFIED_NAME_TO_DATA_KEY) - .collect(Collectors.toSet())) - .build() - .writeDelimitedTo(out); + public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) + throws IOException { + return XmlResourceValues.serializeProtoDataValue( + output, + XmlResourceValues.newSerializableDataValueBuilder(sourceId) + .setXmlValue( + SerializeFormat.DataValueXml.newBuilder() + .setType(XmlType.STYLEABLE) + .putAllNamespace(namespaces.asMap()) + .addAllReferences( + Iterables.transform(attrs.entrySet(), FULLY_QUALIFIED_NAME_TO_DATA_KEY)))); } public static XmlResourceValue from(SerializeFormat.DataValueXml proto) { @@ -191,12 +202,12 @@ public class StyleableXmlResourceValue implements XmlResourceValue { /** * Combines this instance with another {@link StyleableXmlResourceValue}. * - * <p>Defining two Styleables (undocumented in the official Android Docs) with the same {@link - * FullyQualifiedName} results in a single Styleable containing a union of all the attribute - * references. + * Defining two Styleables (undocumented in the official Android Docs) with the same + * {@link FullyQualifiedName} results in a single Styleable containing a union of all the + * attribute references. * - * @param value Another {@link StyleableXmlResourceValue} with the same {@link - * FullyQualifiedName}. + * @param value Another {@link StyleableXmlResourceValue} with the same + * {@link FullyQualifiedName}. * @return {@link StyleableXmlResourceValue} containing a sorted union of the attribute * references. * @throws IllegalArgumentException if value is not an {@link StyleableXmlResourceValue}. |