Home | History | Annotate | Download | only in res
      1 package com.xtremelabs.robolectric.res;
      2 
      3 import org.w3c.dom.Node;
      4 import org.w3c.dom.NodeList;
      5 
      6 import javax.xml.xpath.XPathConstants;
      7 import javax.xml.xpath.XPathExpression;
      8 import javax.xml.xpath.XPathExpressionException;
      9 import javax.xml.xpath.XPathFactory;
     10 import java.util.ArrayList;
     11 import java.util.HashMap;
     12 import java.util.List;
     13 import java.util.Map;
     14 
     15 public 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