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 StaxAttrLoader extends StaxLoader { 9 private String name; 10 private String format; 11 private final List<AttrData.Pair> pairs = new ArrayList<>(); 12 StaxAttrLoader(PackageResourceTable resourceTable, String attrType, ResType resType)13 public StaxAttrLoader(PackageResourceTable resourceTable, String attrType, ResType resType) { 14 super(resourceTable, attrType, resType); 15 16 addHandler("*", new NodeHandler() { 17 private String value; 18 private String name; 19 20 @Override 21 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 22 String type = xml.getLocalName(); 23 if (pairs.isEmpty()) { 24 if (format == null) { 25 format = type; 26 } else { 27 format = format + "|" + type; 28 } 29 } 30 name = xml.getAttributeValue(null, "name"); 31 value = xml.getAttributeValue(null, "value"); 32 pairs.add(new AttrData.Pair(name, value)); 33 } 34 35 @Override 36 public void onCharacters(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 37 } 38 39 @Override 40 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 41 } 42 }); 43 } 44 45 @Override onStart(XMLStreamReader xml, XmlContext xmlContext)46 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 47 name = xml.getAttributeValue(null, "name"); 48 format = xml.getAttributeValue(null, "format"); 49 } 50 51 @Override onEnd(XMLStreamReader xml, XmlContext xmlContext)52 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 53 AttrData attrData = new AttrData(name, format, new ArrayList<>(pairs)); 54 pairs.clear(); 55 56 // xmlContext = xmlContext.withLineNumber(xml.getLocation().getLineNumber()); 57 if (attrData.getFormat() != null) { 58 resourceTable.addResource(attrType, name, new TypedResource<>(attrData, resType, xmlContext)); 59 } 60 } 61 } 62