aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-04-19 22:07:47 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-04-20 11:17:11 +0000
commit66cf13874a3c8f866aeace2d59231e30ca4a5032 (patch)
treee926e87be0c3f8ff8322cd2be20e5a534085010f /src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java
parent9b35d8a81b0d5cb92a22e7d9c7bf30a834711d7f (diff)
4 of 5: Serialization of UnwrittenMergedAndroidData.
Adding AndroidDataSerializer, the serialize_format proto, and KeyValueConsumers (utility class for keeping consumers straight). The serializtion is a bit more manual as previous experience has proven to me that simply writing all the resources into a proto map and pulling them out is not performant in the least. So, the serializer stores each message independent, the keys and then the values allowing for potential lazy loading and other optimizations in the future. Also adds tests for parsing and writing style resources. -- MOS_MIGRATED_REVID=120274904
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/XmlResourceValues.java63
1 files changed, 59 insertions, 4 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 f2525712ea..1ed13c1057 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
@@ -13,17 +13,22 @@
// limitations under the License.
package com.google.devtools.build.android;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.android.ParsedAndroidData.KeyValueConsumer;
+import com.google.devtools.build.android.proto.SerializeFormat;
import com.google.devtools.build.android.xml.AttrXmlResourceValue;
import com.google.devtools.build.android.xml.IdXmlResourceValue;
import com.google.devtools.build.android.xml.PluralXmlResourceValue;
import com.google.devtools.build.android.xml.SimpleXmlResourceValue;
import com.google.devtools.build.android.xml.StyleXmlResourceValue;
import com.google.devtools.build.android.xml.StyleableXmlResourceValue;
+import com.google.protobuf.CodedOutputStream;
import com.android.resources.ResourceType;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
@@ -40,8 +45,9 @@ import javax.xml.stream.events.XMLEvent;
/**
* {@link XmlResourceValues} provides methods for getting {@link XmlResourceValue} derived classes.
*
- * <p>Acts a static factory class containing the general xml parsing logic for resources
- * that are declared inside the &lt;resources&gt; tag.
+ * <p>
+ * Acts a static factory class containing the general xml parsing logic for resources that are
+ * declared inside the &lt;resources&gt; tag.
*/
public class XmlResourceValues {
@@ -120,10 +126,46 @@ public class XmlResourceValues {
return IdXmlResourceValue.of();
}
- static XmlResourceValue parseSimple(XMLEventReader eventReader, ResourceType resourceType)
+ static XmlResourceValue parseSimple(
+ XMLEventReader eventReader, ResourceType resourceType, QName startTag)
throws XMLStreamException {
+ StringBuilder contents = new StringBuilder();
+ while (!isEndTag(eventReader.peek(), startTag)) {
+ XMLEvent xmlEvent = eventReader.nextEvent();
+ if (xmlEvent.isCharacters()) {
+ contents.append(xmlEvent.asCharacters().getData());
+ } else if (xmlEvent.isStartElement()) {
+ QName name = xmlEvent.asStartElement().getName();
+ contents.append("<");
+ if (!name.getNamespaceURI().isEmpty()) {
+ contents
+ .append(name.getPrefix())
+ .append(':')
+ .append(name.getLocalPart())
+ .append(' ')
+ .append("xmlns:")
+ .append(name.getPrefix())
+ .append("='")
+ .append(name.getNamespaceURI())
+ .append("'");
+ } else {
+ contents.append(name.getLocalPart());
+ }
+ contents.append(">");
+ } else if (xmlEvent.isEndElement()) {
+ QName name = xmlEvent.asEndElement().getName();
+ contents.append("</");
+ if (!name.getNamespaceURI().isEmpty()) {
+ contents.append(name.getPrefix()).append(':').append(name.getLocalPart());
+ } else {
+ contents.append(name.getLocalPart());
+ }
+ contents.append(">");
+ }
+ }
+ Preconditions.checkArgument(eventReader.nextEvent().asEndElement().getName().equals(startTag));
return SimpleXmlResourceValue.of(
- SimpleXmlResourceValue.Type.from(resourceType), eventReader.getElementText());
+ SimpleXmlResourceValue.Type.from(resourceType), contents.toString());
}
/* XML helper methods follow. */
@@ -209,4 +251,17 @@ public class XmlResourceValues {
}
return false;
}
+
+ public static SerializeFormat.DataValue.Builder newProtoDataBuilder(Path source) {
+ SerializeFormat.DataValue.Builder builder = SerializeFormat.DataValue.newBuilder();
+ return builder.setSource(builder.getSourceBuilder().setFilename(source.toString()));
+ }
+
+ public static int serializeProtoDataValue(
+ OutputStream output, SerializeFormat.DataValue.Builder builder) throws IOException {
+ SerializeFormat.DataValue value = builder.build();
+ value.writeDelimitedTo(output);
+ return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
+ + value.getSerializedSize();
+ }
}