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 = new ResourceReferenceResolver<Float>("dimen"); 9 DimenResourceLoader(ResourceExtractor resourceExtractor)10 public DimenResourceLoader(ResourceExtractor resourceExtractor) { 11 super(resourceExtractor, "/resources/dimen"); 12 } 13 getValue(int resourceId)14 public float getValue(int resourceId) { 15 return dimenResolver.getValue(resourceExtractor.getResourceName(resourceId)); 16 } 17 getValue(String resourceName, boolean isSystem)18 public float getValue(String resourceName, boolean isSystem) { 19 return getValue(resourceExtractor.getResourceId(resourceName, isSystem)); 20 } 21 22 @Override processNode(Node node, String name, boolean isSystem)23 protected void processNode(Node node, String name, boolean isSystem) { 24 dimenResolver.processResource(name, node.getTextContent(), this, isSystem); 25 } 26 27 @Override convertRawValue(String rawValue)28 public Object convertRawValue(String rawValue) { 29 int end = rawValue.length(); 30 for ( int i = 0; i < UNITS.length; i++ ) { 31 int index = rawValue.indexOf(UNITS[i]); 32 if ( index >= 0 && end > index ) { 33 end = index; 34 } 35 } 36 37 return Float.parseFloat(rawValue.substring(0, end)); 38 } 39 } 40 41