diff options
author | 2016-06-29 16:38:09 +0000 | |
---|---|---|
committer | 2016-06-30 11:40:56 +0000 | |
commit | cee8f09ec9c0c6beaef3a1937dd07ad638b4ffc9 (patch) | |
tree | 365dbaa93982e03ba6f62aeab1ca52c72df9cfa5 /src/tools/android | |
parent | 46e5e41fd52239baba882152f7423c89c58d8242 (diff) |
Switches XmlResourceValue writing to the "define" api.
--
MOS_MIGRATED_REVID=126200568
Diffstat (limited to 'src/tools/android')
8 files changed, 202 insertions, 159 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java b/src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java index 09b1780f7f..4321b9ad96 100644 --- a/src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java +++ b/src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java @@ -82,7 +82,7 @@ public class XmlResourceValues { private static final XMLEventFactory XML_EVENT_FACTORY = XMLEventFactory.newInstance(); static final String XLIFF_NAMESPACE = "urn:oasis:names:tc:xliff:document:1.2"; - private static final String XLIFF_PREFIX = "xliff"; + static final String XLIFF_PREFIX = "xliff"; static XmlResourceValue parsePlurals(XMLEventReader eventReader) throws XMLStreamException { ImmutableMap.Builder<String, String> values = ImmutableMap.builder(); 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 42d9d76150..ce00d9577b 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 @@ -14,12 +14,11 @@ package com.google.devtools.build.android.xml; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; import com.google.common.base.MoreObjects; -import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.ValuesResourceDefinition; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -31,10 +30,8 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map.Entry; import java.util.Objects; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; @@ -62,17 +59,8 @@ 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"); - private static final Function<String, String> ITEM_TO_XML = - new Function<String, String>() { - @Nullable - @Override - public String apply(@Nullable String item) { - return String.format("<item>%s</item>", item); - } - }; - /** - * Enumerates the different types of array tags. + * Enumerates the different types of array parentTags. */ public enum ArrayType { INTEGER_ARRAY(TAG_INTEGER_ARRAY), @@ -95,25 +83,6 @@ public class ArrayXmlResourceValue implements XmlResourceValue { throw new IllegalArgumentException( String.format("%s not found in %s", tagQName, Arrays.toString(values()))); } - - String openTag(FullyQualifiedName key, ImmutableMap<String, String> attributes) { - StringBuilder xmlString = new StringBuilder("<"); - xmlString.append(tagName.getLocalPart()); - xmlString.append(" name='").append(key.name()).append("'"); - for (Entry<String, String> entry : attributes.entrySet()) { - xmlString - .append(" ") - .append(entry.getKey()) - .append("=\"") - .append(entry.getValue()) - .append("\""); - } - return xmlString.append(">").toString(); - } - - String closeTag() { - return String.format("</%s>", tagName.getLocalPart()); - } } private final ImmutableList<String> values; @@ -152,13 +121,15 @@ public class ArrayXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - mergedDataWriter.writeToValuesXml( - key, - FluentIterable.from( - ImmutableList.of( - String.format("<!-- %s -->", source), arrayType.openTag(key, attributes))) - .append(FluentIterable.from(values).transform(ITEM_TO_XML)) - .append(arrayType.closeTag())); + ValuesResourceDefinition definition = mergedDataWriter.define(key).derivedFrom(source) + .startTag(arrayType.tagName) + .named(key) + .addAttributesFrom(attributes.entrySet()) + .closeTag(); + for (String value : values) { + definition = definition.startItemTag().closeTag().addCharactersOf(value).endTag(); + } + definition.endTag().save(); } @Override 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 ce9641b7ac..a4608ce6ed 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 @@ -13,6 +13,9 @@ // limitations under the License. package com.google.devtools.build.android.xml; +import static com.google.common.base.Predicates.equalTo; +import static com.google.common.base.Predicates.not; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Joiner; @@ -24,6 +27,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Ordering; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.ValuesResourceDefinition; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -53,24 +57,21 @@ 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 <attr name="<em>name</em>" format= - * "<em>format</em>" /> while the complex ones, flag and enum, have sub tags: <attr name= - * "<em>name</em>" ><flag name="<em>name</em>" value="<em>value</em>"> </attr>. + * "<em>format</em>" /> while the complex ones, flag and enum, have sub parentTags: <attr + * name= "<em>name</em>" ><flag name="<em>name</em>" value="<em>value</em>"> </attr>. * - * <p> - * Attributes also have a double duty as defining validation logic for layout resources -- each + * <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 { @@ -113,7 +114,7 @@ public class AttrXmlResourceValue implements XmlResourceValue { private static void endAttrElement(XMLEventReader reader) throws XMLStreamException { XMLEvent endTag = reader.nextTag(); if (!endTag.isEndElement() || !QName.valueOf("attr").equals(endTag.asEndElement().getName())) { - throw new XMLStreamException("Unexpected Tag:" + endTag, endTag.getLocation()); + throw new XMLStreamException("Unexpected ParentTag:" + endTag, endTag.getLocation()); } } @@ -149,6 +150,7 @@ public class AttrXmlResourceValue implements XmlResourceValue { return of(ImmutableMap.copyOf(Arrays.asList(entries))); } + @SuppressWarnings("deprecation") public static XmlResourceValue from(SerializeFormat.DataValueXml proto) throws InvalidProtocolBufferException { Builder<String, ResourceXmlAttrValue> formats = @@ -290,34 +292,33 @@ public class AttrXmlResourceValue implements XmlResourceValue { public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - ImmutableList<String> formatKeys = Ordering.natural().immutableSortedCopy(formats.keySet()); - if (formatKeys.isEmpty()) { - mergedDataWriter.writeToValuesXml( - key, - ImmutableList.of( - String.format("<!-- %s -->", source), - String.format("<attr name='%s'/>", key.name()))); - } else if (formats.containsKey(FLAGS) || formats.containsKey(ENUM)) { - FluentIterable<String> iterable = - FluentIterable.from( - ImmutableList.of( - String.format("<!-- %s -->", source), - formatKeys.isEmpty() - ? String.format("<attr name='%s'>", key.name()) - : String.format( - "<attr format='%s' name='%s' >", - Joiner.on('|').join(formatKeys), key.name()))); - for (String formatKey : formatKeys) { - iterable = formats.get(formatKey).appendTo(iterable); - } - mergedDataWriter.writeToValuesXml(key, iterable.append("</attr>")); + if (formats.isEmpty()) { + mergedDataWriter + .define(key) + .derivedFrom(source) + .startTag("attr") + .named(key) + .closeTag() + .endTag() + .save(); } else { - mergedDataWriter.writeToValuesXml( - key, - ImmutableList.of( - String.format("<!-- %s -->", source), - String.format( - "<attr format='%s' name='%s'/>", Joiner.on('|').join(formatKeys), key.name()))); + ImmutableList<String> formatKeys = + FluentIterable.from(formats.keySet()) + .filter(not(equalTo(FLAGS))) + .filter(not(equalTo(ENUM))) + .toSortedList(Ordering.natural()); + ValuesResourceDefinition definition = + mergedDataWriter + .define(key) + .derivedFrom(source) + .startTag("attr") + .named(key) + .optional().attribute("format").setTo(Joiner.on("|").join(formatKeys)) + .closeTag(); + for (ResourceXmlAttrValue value : formats.values()) { + definition = value.writeTo(definition); + } + definition.endTag().save(); } } @@ -346,6 +347,8 @@ public class AttrXmlResourceValue implements XmlResourceValue { interface ResourceXmlAttrValue { FluentIterable<String> appendTo(FluentIterable<String> iterable); + ValuesResourceDefinition writeTo(ValuesResourceDefinition writer); + SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder); } @@ -412,6 +415,21 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.putAllMappedStringValue(values).build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + for (Entry<String, String> entry : values.entrySet()) { + writer = + writer + .startTag("enum") + .attribute("name") + .setTo(entry.getKey()) + .attribute("value") + .setTo(entry.getValue()) + .closeUnaryTag(); + } + return writer; + } } /** Represents an Android Flag Attribute resource. */ @@ -478,6 +496,21 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.putAllMappedStringValue(values).build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + for (Entry<String, String> entry : values.entrySet()) { + writer = + writer + .startTag("flag") + .attribute("name") + .setTo(entry.getKey()) + .attribute("value") + .setTo(entry.getValue()) + .closeUnaryTag(); + } + return writer; + } } /** Represents an Android Reference Attribute resource. */ @@ -504,6 +537,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Color Attribute resource. */ @@ -529,6 +567,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Boolean Attribute resource. */ @@ -554,6 +597,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Float Attribute resource. */ @@ -579,6 +627,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Dimension Attribute resource. */ @@ -605,6 +658,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Integer Attribute resource. */ @@ -630,6 +688,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android String Attribute resource. */ @@ -655,6 +718,11 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } /** Represents an Android Fraction Attribute resource. */ @@ -680,5 +748,10 @@ public class AttrXmlResourceValue implements XmlResourceValue { public SerializeFormat.DataValueXml appendTo(SerializeFormat.DataValueXml.Builder builder) { return builder.build(); } + + @Override + public ValuesResourceDefinition writeTo(ValuesResourceDefinition writer) { + return writer; + } } } 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 0b0b476fcc..b300e43851 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 @@ -14,8 +14,8 @@ package com.google.devtools.build.android.xml; import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableList; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.StartTag; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -66,17 +66,18 @@ public class IdXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - String sourceString = String.format("<!-- %s -->", source); + StartTag startTag = + mergedDataWriter + .define(key) + .derivedFrom(source) + .startItemTag() + .named(key) + .attribute("type") + .setTo("id"); if (value == null) { - mergedDataWriter.writeToValuesXml( - key, - ImmutableList.of(sourceString, String.format("<item type='id' name='%s'/>", key.name()))); + startTag.closeUnaryTag().save(); } else { - mergedDataWriter.writeToValuesXml( - key, - ImmutableList.of( - sourceString, - String.format("<item type='id' name='%s'>%s</item>", key.name(), value))); + startTag.closeTag().addCharactersOf(value).endTag().save(); } } 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 1c8eee88fb..791e993c15 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 @@ -15,10 +15,9 @@ package com.google.devtools.build.android.xml; import com.google.common.base.Function; import com.google.common.base.MoreObjects; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.ValuesResourceDefinition; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -34,6 +33,7 @@ import java.util.Objects; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; +import javax.xml.namespace.QName; /** * Represents an Android Plural Resource. @@ -52,6 +52,7 @@ import javax.annotation.concurrent.Immutable; @Immutable public class PluralXmlResourceValue implements XmlResourceValue { + private static final QName PLURALS = QName.valueOf("plurals"); public static final Function<Entry<String, String>, String> ENTRY_TO_PLURAL = new Function<Entry<String, String>, String>() { @Nullable @@ -73,14 +74,19 @@ public class PluralXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - mergedDataWriter.writeToValuesXml( - key, - FluentIterable.from( - ImmutableList.of( - String.format("<!-- %s -->", source), - String.format("<plurals name='%s'>", key.name()))) - .append(FluentIterable.from(values.entrySet()).transform(ENTRY_TO_PLURAL)) - .append("</plurals>")); + ValuesResourceDefinition definition = + mergedDataWriter.define(key).derivedFrom(source).startTag(PLURALS).named(key).closeTag(); + for (Entry<String, String> plural : values.entrySet()) { + definition = + definition + .startItemTag() + .attribute("quantity") + .setTo(plural.getKey()) + .closeTag() + .addCharactersOf(plural.getValue()) + .endTag(); + } + definition.endTag().save(); } @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 218dd3cb86..7feb695fc4 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 @@ -14,9 +14,9 @@ package com.google.devtools.build.android.xml; import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.StartTag; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -29,7 +29,6 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.file.Path; import java.util.Arrays; -import java.util.Map.Entry; import java.util.Objects; import javax.annotation.Nullable; @@ -190,32 +189,20 @@ public class SimpleXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - StringBuilder xmlString = - new StringBuilder("<") - .append(valueType.tagName.getLocalPart()) - .append(" name=\"") - .append(key.name()) - .append("\""); - for (Entry<String, String> entry : attributes.entrySet()) { - xmlString - .append(" ") - .append(entry.getKey()) - .append("=\"") - .append(entry.getValue()) - .append("\""); - } + + StartTag startTag = + mergedDataWriter + .define(key) + .derivedFrom(source) + .startTag(valueType.tagName) + .named(key) + .addAttributesFrom(attributes.entrySet()); + if (value != null) { - xmlString - .append(">") - .append(value) - .append("</") - .append(valueType.tagName.getLocalPart()) - .append(">"); + startTag.closeTag().addCharactersOf(value).endTag().save(); } else { - xmlString.append("/>"); + startTag.closeUnaryTag().save(); } - mergedDataWriter.writeToValuesXml( - key, ImmutableList.of(String.format("<!-- %s -->", source), xmlString.toString())); } @SuppressWarnings("deprecation") 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 e2f62f0b95..d0c833201a 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 @@ -15,10 +15,9 @@ package com.google.devtools.build.android.xml; import com.google.common.base.Function; import com.google.common.base.MoreObjects; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.ValuesResourceDefinition; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -69,6 +68,7 @@ public class StyleXmlResourceValue implements XmlResourceValue { return new StyleXmlResourceValue(parent, ImmutableMap.copyOf(values)); } + @SuppressWarnings("deprecation") public static XmlResourceValue from(SerializeFormat.DataValueXml proto) { return of(proto.hasValue() ? proto.getValue() : null, proto.getMappedStringValue()); } @@ -81,24 +81,27 @@ public class StyleXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - mergedDataWriter.writeToValuesXml( - key, - FluentIterable.from( - ImmutableList.of( - String.format("<!-- %s -->", source), - String.format("<style name='%s' %s>", key.name(), parentAsXmlAttribute()))) - .append(FluentIterable.from(values.entrySet()).transform(ENTRY_TO_ITEM)) - .append("</style>")); - } - private String parentAsXmlAttribute() { - if (parent == null) { - return ""; - } - if (parent.isEmpty()) { - return "parent=''"; + ValuesResourceDefinition definition = + mergedDataWriter + .define(key) + .derivedFrom(source) + .startTag("style") + .named(key) + .optional() + .attribute("parent") + .setTo(parent) + .closeTag(); + for (Entry<String, String> entry : values.entrySet()) { + definition = + definition + .startItemTag() + .named(entry.getKey()) + .closeTag() + .addCharactersOf(entry.getValue()) + .endTag(); } - return "parent=\"" + parent + "\""; + definition.endTag().save(); } @Override 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 0ed66afaf7..38d90fd8e3 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 @@ -16,12 +16,11 @@ package com.google.devtools.build.android.xml; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.MoreObjects; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Iterables; import com.google.devtools.build.android.AndroidDataWritingVisitor; +import com.google.devtools.build.android.AndroidDataWritingVisitor.ValuesResourceDefinition; import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.XmlResourceValue; import com.google.devtools.build.android.XmlResourceValues; @@ -61,13 +60,6 @@ import javax.annotation.concurrent.Immutable; */ @Immutable public class StyleableXmlResourceValue implements XmlResourceValue { - public static final Function<FullyQualifiedName, String> ITEM_TO_ATTR = - new Function<FullyQualifiedName, String>() { - @Override - public String apply(FullyQualifiedName input) { - return String.format("<attr name='%s'/>", input.name()); - } - }; static final Function<Entry<FullyQualifiedName, Boolean>, SerializeFormat.DataKey> FULLY_QUALIFIED_NAME_TO_DATA_KEY = @@ -120,14 +112,24 @@ public class StyleableXmlResourceValue implements XmlResourceValue { @Override public void write( FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) { - mergedDataWriter.writeToValuesXml( - key, - FluentIterable.from( - ImmutableList.of( - String.format("<!-- %s -->", source), - String.format("<declare-styleable name='%s'>", key.name()))) - .append(FluentIterable.from(attrs.keySet()).transform(ITEM_TO_ATTR)) - .append("</declare-styleable>")); + ValuesResourceDefinition definition = + mergedDataWriter + .define(key) + .derivedFrom(source) + .startTag("declare-styleable") + .named(key) + .closeTag(); + for (Entry<FullyQualifiedName, Boolean> entry : attrs.entrySet()) { + if (entry.getValue().booleanValue()) { + // Move the attr definition to this styleable. + definition = definition.adopt(entry.getKey()); + } else { + // Make a reference to the attr. + definition = + definition.startTag("attr").attribute("name").setTo(entry.getKey()).closeUnaryTag(); + } + } + definition.endTag().save(); } @Override |