1 package com.xtremelabs.robolectric.res; 2 3 import android.graphics.Color; 4 import org.w3c.dom.Node; 5 6 import java.util.HashMap; 7 import java.util.Map; 8 9 public class ColorResourceLoader extends XpathResourceXmlLoader implements ResourceValueConverter { 10 private ResourceReferenceResolver<Integer> colorResolver = new ResourceReferenceResolver<Integer>("color"); 11 private static Map<String, Integer> androidColors = new HashMap<String, Integer>(); 12 13 static { 14 androidColors.put("black", Color.BLACK); 15 androidColors.put("darkgray", Color.DKGRAY); 16 androidColors.put("gray", Color.GRAY); 17 androidColors.put("lightgray", Color.LTGRAY); 18 androidColors.put("white", Color.WHITE); 19 androidColors.put("red", Color.RED); 20 androidColors.put("green", Color.GREEN); 21 androidColors.put("blue", Color.BLUE); 22 androidColors.put("yellow", Color.YELLOW); 23 androidColors.put("cyan", Color.CYAN); 24 androidColors.put("magenta", Color.MAGENTA); 25 } 26 27 public ColorResourceLoader(ResourceExtractor resourceExtractor) { 28 super(resourceExtractor, "/resources/color"); 29 } 30 31 public int getValue(int colorId) { 32 String resourceName = resourceExtractor.getResourceName(colorId); 33 if (resourceName == null) { 34 return -1; 35 } 36 37 Integer colorResolverValue = colorResolver.getValue(resourceName); 38 return colorResolverValue == null ? -1 : colorResolverValue; 39 } 40 41 @Override 42 protected void processNode(Node node, String name, boolean isSystem) { 43 colorResolver.processResource(name, node.getTextContent(), this, isSystem); 44 } 45 46 @Override 47 public Integer convertRawValue(String rawValue) { 48 if (rawValue.startsWith("#")) { 49 long color = Long.parseLong(rawValue.substring(1), 16); 50 return (int) color; 51 } else if (androidColors.containsKey(rawValue)) { 52 return androidColors.get(rawValue); 53 } 54 return null; 55 } 56 } 57