1 package org.robolectric.res; 2 3 import javax.xml.stream.XMLStreamException; 4 import javax.xml.stream.XMLStreamReader; 5 6 public class StaxValueLoader extends StaxLoader { 7 private final StringBuilder buf = new StringBuilder(); 8 protected String name; 9 StaxValueLoader(PackageResourceTable resourceTable, String attrType, ResType resType)10 public StaxValueLoader(PackageResourceTable resourceTable, String attrType, ResType resType) { 11 super(resourceTable, attrType, resType); 12 13 if (resType == ResType.CHAR_SEQUENCE) { 14 addHandler("*", new TextCollectingNodeHandler(buf)); 15 } 16 } 17 18 @Override onStart(XMLStreamReader xml, XmlContext xmlContext)19 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 20 name = xml.getAttributeValue(null, "name"); 21 buf.setLength(0); 22 } 23 24 @Override onCharacters(XMLStreamReader xml, XmlContext xmlContext)25 public void onCharacters(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 26 buf.append(xml.getText()); 27 } 28 29 @Override onEnd(XMLStreamReader xml, XmlContext xmlContext)30 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 31 String s = buf.toString(); 32 if (resType == ResType.CHAR_SEQUENCE) { 33 s = StringResources.processStringResources(s); 34 } 35 resourceTable.addResource(attrType, name, new TypedResource<>(s, resType, xmlContext)); 36 } 37 } 38