1 package com.xtremelabs.robolectric.res; 2 3 import org.w3c.dom.Node; 4 public class DimenResourceLoader extends XpathResourceXmlLoader implements ResourceValueConverter { 5 6 private static final String[] UNITS = { "dp", "dip", "px", "sp", "mm" }; 7 8 private ResourceReferenceResolver<Float> dimenResolver = 9 new ResourceReferenceResolver<Float>("dimen", new String[] { "dimen", "integer" }); 10 DimenResourceLoader(ResourceExtractor resourceExtractor)11 public DimenResourceLoader(ResourceExtractor resourceExtractor) { 12 super(resourceExtractor, "/resources/dimen"); 13 } 14 getValue(int resourceId)15 public float getValue(int resourceId) { 16 return dimenResolver.getValue(resourceExtractor.getResourceName(resourceId)); 17 } 18 getValue(String resourceName, boolean isSystem)19 public float getValue(String resourceName, boolean isSystem) { 20 return getValue(resourceExtractor.getResourceId(resourceName, isSystem)); 21 } 22 23 @Override processNode(Node node, String name, boolean isSystem)24 protected void processNode(Node node, String name, boolean isSystem) { 25 dimenResolver.processResource(name, node.getTextContent(), this, isSystem); 26 } 27 28 @Override convertRawValue(String rawValue)29 public Object convertRawValue(String rawValue) { 30 int end = rawValue.length(); 31 for ( int i = 0; i < UNITS.length; i++ ) { 32 int index = rawValue.indexOf(UNITS[i]); 33 if ( index >= 0 && end > index ) { 34 end = index; 35 } 36 } 37 38 return Float.parseFloat(rawValue.substring(0, end)); 39 } 40 } 41 42