1package com.xtremelabs.robolectric.res; 2 3import org.w3c.dom.Node; 4import org.w3c.dom.NodeList; 5 6import javax.xml.xpath.XPathConstants; 7import javax.xml.xpath.XPathExpression; 8import javax.xml.xpath.XPathExpressionException; 9import javax.xml.xpath.XPathFactory; 10import java.util.ArrayList; 11import java.util.HashMap; 12import java.util.List; 13import java.util.Map; 14 15public class StringArrayResourceLoader extends XpathResourceXmlLoader { 16 Map<String, String[]> stringArrayValues = new HashMap<String, String[]>(); 17 private StringResourceLoader stringResourceLoader; 18 19 public StringArrayResourceLoader(ResourceExtractor resourceExtractor, StringResourceLoader stringResourceLoader) { 20 super(resourceExtractor, "/resources/string-array"); 21 this.stringResourceLoader = stringResourceLoader; 22 } 23 24 public String[] getArrayValue(int resourceId) { 25 String resourceName = resourceExtractor.getResourceName(resourceId); 26 return stringArrayValues.get(resourceName); 27 } 28 29 @Override protected void processNode(Node node, String name, boolean isSystem) throws XPathExpressionException { 30 XPathExpression itemXPath = XPathFactory.newInstance().newXPath().compile("item"); 31 NodeList childNodes = (NodeList) itemXPath.evaluate(node, XPathConstants.NODESET); 32 List<String> arrayValues = new ArrayList<String>(); 33 for (int j = 0; j < childNodes.getLength(); j++) { 34 Node childNode = childNodes.item(j); 35 36 String value = childNode.getTextContent(); 37 if (value.startsWith("@")) { 38 arrayValues.add(stringResourceLoader.getValue(value , isSystem)); 39 } else { 40 arrayValues.add(value); 41 } 42 } 43 String valuePointer = (isSystem ? "android:" : "") + "array/" + name; 44 stringArrayValues.put(valuePointer, arrayValues.toArray(new String[arrayValues.size()])); 45 } 46} 47