diff options
author | 2017-11-15 11:55:15 -0800 | |
---|---|---|
committer | 2017-11-15 11:57:13 -0800 | |
commit | 7925d5b265249466bff385602e94509a05de6870 (patch) | |
tree | 9543038c23171dd93261a792a6f0571591e3cb09 /src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java | |
parent | 1e0b7cb49b5d22f72d9e32018d15972a9f28878c (diff) |
Create merge action and data deserializer for aapt2 compiled resources.
RELNOTES: None
PiperOrigin-RevId: 175858467
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java')
-rw-r--r-- | src/tools/android/java/com/google/devtools/build/android/xml/SimpleXmlResourceValue.java | 61 |
1 files changed, 60 insertions, 1 deletions
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 923c163ad3..4006d4d7a4 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 @@ -13,6 +13,10 @@ // limitations under the License. package com.google.devtools.build.android.xml; +import com.android.aapt.Resources.Item; +import com.android.aapt.Resources.StyledString; +import com.android.aapt.Resources.StyledString.Span; +import com.android.aapt.Resources.Value; import com.android.resources.ResourceType; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableMap; @@ -54,6 +58,8 @@ public class SimpleXmlResourceValue implements XmlResourceValue { static final QName TAG_FRACTION = QName.valueOf("fraction"); static final QName TAG_INTEGER = QName.valueOf("integer"); static final QName TAG_ITEM = QName.valueOf("item"); + static final QName TAG_LAYOUT = QName.valueOf("layout"); + static final QName TAG_MIPMAP = QName.valueOf("mipmap"); static final QName TAG_PUBLIC = QName.valueOf("public"); static final QName TAG_STRING = QName.valueOf("string"); @@ -108,6 +114,20 @@ public class SimpleXmlResourceValue implements XmlResourceValue { return true; } }, + LAYOUT(TAG_LAYOUT) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the item type. + return true; + } + }, + MIPMAP(TAG_MIPMAP) { + @Override + public boolean validate(String value) { + // TODO(corysmith): Validate the item type. + return true; + } + }, PUBLIC(TAG_PUBLIC) { @Override public boolean validate(String value) { @@ -121,7 +141,7 @@ public class SimpleXmlResourceValue implements XmlResourceValue { return true; } }; - private QName tagName; + private final QName tagName; Type(QName tagName) { this.tagName = tagName; @@ -211,6 +231,45 @@ public class SimpleXmlResourceValue implements XmlResourceValue { proto.hasValue() ? proto.getValue() : null); } + public static XmlResourceValue from(Value proto, ResourceType resourceType) { + Item item = proto.getItem(); + String stringValue = null; + + if (item.hasStr()) { + stringValue = item.getStr().toString(); + } else if (item.hasRef()) { + stringValue = "@" + item.getRef().getName(); + } else if (item.hasStyledStr()) { + StyledString styledString = item.getStyledStr(); + StringBuilder stringBuilder = new StringBuilder(styledString.getValue()); + + for (Span span : styledString.getSpanList()) { + stringBuilder.append( + String.format(";%s,%d,%d", span.getTag(), span.getFirstChar(), span.getLastChar())); + } + stringValue = stringBuilder.toString(); + } 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()){ + stringValue = Integer.toString(item.getPrim().getData()); + } else if (resourceType == ResourceType.BOOL && item.hasPrim()) { + stringValue = item.getPrim().getData() == 0 ? "false" : "true"; + } else if (resourceType == ResourceType.FRACTION + || resourceType == ResourceType.DIMEN) { + stringValue = Integer.toString(item.getPrim().getData()); + } else { + throw new IllegalArgumentException( + String.format("'%s' is not a valid resource type.", resourceType)); + } + + return of( + Type.valueOf(resourceType.toString().toUpperCase()), + ImmutableMap.of(), + stringValue); + } + @Override public void writeResourceToClass(FullyQualifiedName key, AndroidResourceSymbolSink sink) { sink.acceptSimpleResource(key.type(), key.name()); |