1package com.xtremelabs.robolectric.res; 2 3import java.io.File; 4import java.io.IOException; 5import java.util.ArrayList; 6import java.util.List; 7 8import org.w3c.dom.Document; 9import org.w3c.dom.Node; 10import org.w3c.dom.NodeList; 11 12/** 13 * 14 * XTagXmlResourceLoader is dedicated for mixed tags xml files. 15 * 16 */ 17public abstract class XTagXmlResourceLoader extends XmlLoader { 18 19 private String tag; 20 21 private static List< String > xPathXmlFiles = new ArrayList< String >( 6 ); 22 23 static { 24 xPathXmlFiles.add( "values/attrs" ); 25 xPathXmlFiles.add( "values/colors" ); 26 xPathXmlFiles.add( "values/strings" ); 27 xPathXmlFiles.add( "values/string_arrays" ); 28 xPathXmlFiles.add( "values/plurals" ); 29 xPathXmlFiles.add( "values/dimens" ); 30 } 31 32 public XTagXmlResourceLoader( ResourceExtractor resourceExtractor, String tag ) { 33 super( resourceExtractor ); 34 this.tag = tag; 35 } 36 37 @Override 38 protected void processResourceXml( File xmlFile, Document document, boolean isSystem ) throws Exception { 39 40 String resourceName = toResourceName( xmlFile ); 41 if ( xPathXmlFiles.contains( resourceName ) ) 42 return; 43 44 NodeList items = document.getElementsByTagName( tag ); 45 for ( int i = 0; i < items.getLength(); i++ ) { 46 Node node = items.item( i ); 47 String name = node.getAttributes().getNamedItem( "name" ).getNodeValue(); 48 processNode( node, name, isSystem ); 49 } 50 51 } 52 53 /** 54 * Convert file name to resource name. 55 * 56 * @param xmlFile 57 * Xml File 58 * @return Resource name 59 */ 60 private String toResourceName( File xmlFile ) { 61 try { 62 return xmlFile.getCanonicalPath().replaceAll( "[/\\\\\\\\]", "/" ).replaceAll( "^.*?/res/", "" ) 63 .replaceAll( "\\..+$", "" ); 64 } catch ( IOException e ) { 65 throw new RuntimeException( e ); 66 } 67 } 68 69 protected abstract void processNode( Node node, String name, boolean isSystem ); 70 71} 72