1 package org.robolectric.res; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import javax.xml.stream.XMLStreamException; 6 import javax.xml.stream.XMLStreamReader; 7 8 public class StaxStyleLoader extends StaxLoader { 9 private String name; 10 private String parent; 11 private List<AttributeResource> attributeResources; 12 StaxStyleLoader(PackageResourceTable resourceTable, String attrType, ResType resType)13 public StaxStyleLoader(PackageResourceTable resourceTable, String attrType, ResType resType) { 14 super(resourceTable, attrType, resType); 15 16 addHandler("item", new NodeHandler() { 17 private String attrName; 18 private StringBuilder buf = new StringBuilder(); 19 20 @Override 21 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 22 attrName = xml.getAttributeValue(null, "name"); 23 buf.setLength(0); 24 } 25 26 @Override 27 public void onCharacters(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 28 buf.append(xml.getText()); 29 } 30 31 @Override 32 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 33 ResName attrResName = ResName.qualifyResName(attrName, xmlContext.getPackageName(), "attr"); 34 attributeResources.add(new AttributeResource(attrResName, buf.toString(), xmlContext.getPackageName())); 35 } 36 }); 37 } 38 39 @Override onStart(XMLStreamReader xml, XmlContext xmlContext)40 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 41 name = xml.getAttributeValue(null, "name"); 42 parent = xml.getAttributeValue(null, "parent"); 43 attributeResources = new ArrayList<>(); 44 } 45 46 @Override onEnd(XMLStreamReader xml, XmlContext xmlContext)47 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 48 String styleParent = parent; 49 50 if (styleParent == null) { 51 int lastDot = name.lastIndexOf('.'); 52 if (lastDot != -1) { 53 styleParent = name.substring(0, lastDot); 54 } 55 } 56 57 StyleData styleData = new StyleData(xmlContext.getPackageName(), name, styleParent, attributeResources); 58 59 resourceTable.addResource("style", styleData.getName(), new TypedResource<>(styleData, resType, xmlContext)); 60 } 61 } 62