1 package com.xtremelabs.robolectric.res; 2 3 import org.w3c.dom.Document; 4 import org.w3c.dom.Node; 5 import org.w3c.dom.NodeList; 6 7 import javax.xml.xpath.XPathConstants; 8 import javax.xml.xpath.XPathExpression; 9 import javax.xml.xpath.XPathExpressionException; 10 import javax.xml.xpath.XPathFactory; 11 import java.io.File; 12 13 public abstract class XpathResourceXmlLoader extends XmlLoader { 14 private String expression; 15 16 public XpathResourceXmlLoader(ResourceExtractor resourceExtractor, String expression) { 17 super(resourceExtractor); 18 this.expression = expression; 19 } 20 21 @Override protected void processResourceXml(File xmlFile, Document document, boolean isSystem) throws Exception { 22 XPathExpression stringsXPath = XPathFactory.newInstance().newXPath().compile(expression); 23 NodeList nodes = (NodeList) stringsXPath.evaluate(document, XPathConstants.NODESET); 24 for (int i = 0; i < nodes.getLength(); i++) { 25 Node node = nodes.item(i); 26 String name = node.getAttributes().getNamedItem("name").getNodeValue(); 27 processNode(node, name, isSystem); 28 } 29 } 30 31 protected abstract void processNode(Node node, String name, boolean isSystem) throws XPathExpressionException; 32 } 33