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 StaxPluralsLoader extends StaxLoader { 9 protected String name; 10 private String quantity; 11 private final List<Plural> plurals = new ArrayList<>(); 12 StaxPluralsLoader(PackageResourceTable resourceTable, String attrType, ResType charSequence)13 public StaxPluralsLoader(PackageResourceTable resourceTable, String attrType, ResType charSequence) { 14 super(resourceTable, attrType, charSequence); 15 16 addHandler("item", new NodeHandler() { 17 private final StringBuilder buf = new StringBuilder(); 18 19 @Override 20 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 21 quantity = xml.getAttributeValue(null, "quantity"); 22 buf.setLength(0); 23 } 24 25 @Override 26 public void onCharacters(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 27 buf.append(xml.getText()); 28 } 29 30 @Override 31 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 32 plurals.add(new Plural(quantity, buf.toString())); 33 } 34 35 @Override 36 NodeHandler findMatchFor(XMLStreamReader xml) { 37 return new TextCollectingNodeHandler(buf); 38 } 39 }); 40 } 41 42 @Override onStart(XMLStreamReader xml, XmlContext xmlContext)43 public void onStart(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 44 name = xml.getAttributeValue(null, "name"); 45 } 46 47 @Override onEnd(XMLStreamReader xml, XmlContext xmlContext)48 public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { 49 resourceTable.addResource(attrType, name, new PluralRules(new ArrayList<>(plurals), resType, xmlContext)); 50 plurals.clear(); 51 } 52 } 53