diff options
author | corysmith <corysmith@google.com> | 2018-02-16 13:14:29 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-16 13:18:21 -0800 |
commit | f672a31b8b19baab95373e4f2f6d110aa8b8f0fb (patch) | |
tree | 58cef0309a67e62e3fe0ef024916d9d5c53bae8c /src/tools/android/java/com/google/devtools/build/android/xml | |
parent | 950bcf79c47484832bb4c84fbb23f6b56800e0b3 (diff) |
Normalized the serialization proto to save space and allow greater versatility in storage.
RELNOTES: None
PiperOrigin-RevId: 186036607
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/xml')
10 files changed, 152 insertions, 288 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 fb74fd2208..f977c7b315 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,6 +29,8 @@ 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; @@ -45,14 +47,15 @@ import javax.xml.stream.events.XMLEvent; /** * Represents an Android resource array. * - * There are two flavors of Android Resource arrays: + * <p>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> - * <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> + * <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. * </ul> * * Both of these are accessed by R.array.<name> in java. @@ -62,9 +65,19 @@ 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"); - /** - * Enumerates the different types of array parentTags. - */ + + @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. */ public enum ArrayType { INTEGER_ARRAY(TAG_INTEGER_ARRAY), ARRAY(TAG_ARRAY), @@ -105,7 +118,7 @@ public class ArrayXmlResourceValue implements XmlResourceValue { } public static XmlResourceValue of(ArrayType arrayType, List<String> values) { - return of(arrayType, values, ImmutableMap.<String, String>of()); + return of(arrayType, values, ImmutableMap.of()); } public static XmlResourceValue of( @@ -138,20 +151,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 @@ -165,21 +178,6 @@ 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 f2645ac0a3..96ac31c947 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,23 +404,16 @@ public class AttrXmlResourceValue implements XmlResourceValue { } } - @SuppressWarnings("deprecation") @Override - public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) - throws IOException { - SerializeFormat.DataValue.Builder builder = - XmlResourceValues.newSerializableDataValueBuilder(sourceId); + public void writeTo(OutputStream out) throws IOException { SerializeFormat.DataValueXml.Builder xmlValueBuilder = SerializeFormat.DataValueXml.newBuilder(); - xmlValueBuilder - .setType(SerializeFormat.DataValueXml.XmlType.ATTR) - .putAllNamespace(namespaces.asMap()); + xmlValueBuilder.setType(SerializeFormat.DataValueXml.XmlType.ATTR); for (Entry<String, ResourceXmlAttrValue> entry : formats.entrySet()) { xmlValueBuilder.putMappedXmlValue( - entry.getKey(), entry.getValue().appendTo(builder.getXmlValueBuilder())); + entry.getKey(), entry.getValue().appendTo(SerializeFormat.DataValueXml.newBuilder())); } - builder.setXmlValue(xmlValueBuilder); - return XmlResourceValues.serializeProtoDataValue(output, builder); + xmlValueBuilder.build().writeDelimitedTo(out); } @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 c3fb2faefa..e3fe65aa02 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,11 +20,8 @@ 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; @@ -94,20 +91,8 @@ public class IdXmlResourceValue implements XmlResourceValue { } @Override - 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(); + public void writeTo(OutputStream out) throws IOException { + SerializeFormat.DataValueXml.newBuilder().setType(XmlType.ID).build().writeDelimitedTo(out); } @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 82e8810139..10da5ff225 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,8 +16,13 @@ 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; @@ -39,11 +44,23 @@ 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>> { +public class Namespaces implements Iterable<Entry<String, String>>, Writeable { 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 b912cc141e..7c1b6532e3 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,10 +24,8 @@ 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; @@ -157,23 +155,13 @@ public class PluralXmlResourceValue implements XmlResourceValue { } @Override - 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(); + public void writeTo(OutputStream out) throws IOException { + SerializeFormat.DataValueXml.newBuilder() + .setType(XmlType.PLURAL) + .putAllAttribute(attributes) + .putAllMappedStringValue(values) + .build() + .writeDelimitedTo(out); } @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 af4184d88a..c1e3eb40a0 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,13 +19,11 @@ 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; @@ -33,6 +31,7 @@ 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. @@ -139,23 +138,19 @@ public class PublicXmlResourceValue implements XmlResourceValue { } @Override - 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); + 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); } @Override @@ -178,7 +173,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 a54667ecdb..75a6c0207e 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,9 +21,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.Builder; import com.google.errorprone.annotations.Immutable; import java.io.IOException; import java.io.OutputStream; @@ -40,13 +38,15 @@ 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,9 +92,7 @@ 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; @@ -114,19 +112,13 @@ public class ResourcesAttribute implements XmlResourceValue { } @Override - 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); + 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); } @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 32147cd49c..55bef85bc1 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,9 +27,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; 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; @@ -41,8 +42,7 @@ 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,106 +66,27 @@ 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 and simple value validation. */ + /** Provides an enumeration resource type. */ public enum Type { - 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; - } - }; + 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); 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())) { @@ -177,8 +98,7 @@ 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()))); } } @@ -200,8 +120,7 @@ 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); } @@ -265,11 +184,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"; @@ -282,10 +201,7 @@ 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 @@ -294,23 +210,20 @@ public class SimpleXmlResourceValue implements XmlResourceValue { } @Override - 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) + public void writeTo(OutputStream out) throws IOException { + final Builder builder = + DataValueXml.newBuilder() + .setType(XmlType.SIMPLE) + .setValueType(valueType.name()) // 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) { - xmlValueBuilder.setValue(value); + builder.setValue(value); } - builder.setXmlValue(xmlValueBuilder.setValueType(valueType.name())); - return XmlResourceValues.serializeProtoDataValue(output, builder); + + builder.build().writeDelimitedTo(out); } @Override @@ -342,7 +255,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 5cd0f6e0a5..6929c9d588 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,7 +24,6 @@ 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; @@ -39,8 +38,7 @@ 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 @@ -154,19 +152,15 @@ public class StyleXmlResourceValue implements XmlResourceValue { } @Override - public int serializeTo(int sourceId, Namespaces namespaces, OutputStream output) - throws IOException { + public void writeTo(OutputStream out) throws IOException { SerializeFormat.DataValueXml.Builder xmlValueBuilder = SerializeFormat.DataValueXml.newBuilder() .setType(XmlType.STYLE) - .putAllNamespace(namespaces.asMap()) .putAllMappedStringValue(values); if (parent != null) { xmlValueBuilder.setValue(parent); } - return XmlResourceValues.serializeProtoDataValue( - output, - XmlResourceValues.newSerializableDataValueBuilder(sourceId).setXmlValue(xmlValueBuilder)); + xmlValueBuilder.build().writeDelimitedTo(out); } @Override @@ -195,7 +189,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 c9661690cf..5006600a48 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,7 +27,6 @@ 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,6 +37,7 @@ 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,22 +65,11 @@ public class StyleableXmlResourceValue implements XmlResourceValue { static final Function<Entry<FullyQualifiedName, Boolean>, SerializeFormat.DataKey> FULLY_QUALIFIED_NAME_TO_DATA_KEY = - new Function<Entry<FullyQualifiedName, Boolean>, SerializeFormat.DataKey>() { - @Override - public SerializeFormat.DataKey apply(Entry<FullyQualifiedName, Boolean> input) { - return input.getKey().toSerializedBuilder().setReference(input.getValue()).build(); - } - }; + input -> input.getKey().toSerializedBuilder().setReference(input.getValue()).build(); static final Function<SerializeFormat.DataKey, Entry<FullyQualifiedName, Boolean>> DATA_KEY_TO_FULLY_QUALIFIED_NAME = - 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()); - } - }; + input -> new SimpleEntry<>(FullyQualifiedName.fromProto(input), input.getReference()); private final ImmutableMap<FullyQualifiedName, Boolean> attrs; @@ -145,17 +134,17 @@ public class StyleableXmlResourceValue 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() - .setType(XmlType.STYLEABLE) - .putAllNamespace(namespaces.asMap()) - .addAllReferences( - Iterables.transform(attrs.entrySet(), FULLY_QUALIFIED_NAME_TO_DATA_KEY)))); + 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 static XmlResourceValue from(SerializeFormat.DataValueXml proto) { @@ -202,12 +191,12 @@ public class StyleableXmlResourceValue implements XmlResourceValue { /** * Combines this instance with another {@link StyleableXmlResourceValue}. * - * 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. + * <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. * - * @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}. |