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