1 package com.xtremelabs.robolectric.res; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 class ResourceReferenceResolver<T> { 9 private Map<String, T> attributeNamesToValues = new HashMap<String, T>(); 10 private Map<String, List<String>> unresolvedReferences = new HashMap<String, List<String>>(); 11 private String prefix; 12 ResourceReferenceResolver(String prefix)13 ResourceReferenceResolver(String prefix) { 14 this.prefix = prefix; 15 } 16 getValue(String resourceName)17 public T getValue(String resourceName) { 18 return attributeNamesToValues.get(resourceName); 19 } 20 processResource(String name, String rawValue, ResourceValueConverter loader, boolean isSystem)21 public void processResource(String name, String rawValue, ResourceValueConverter loader, boolean isSystem) { 22 String valuePointer = prefix + "/" + name; 23 if (rawValue.startsWith("@" + prefix) || rawValue.startsWith("@android:" + prefix)) { 24 addAttributeReference(rawValue, valuePointer); 25 } else { 26 if (isSystem) { 27 valuePointer = "android:" + valuePointer; 28 } 29 addAttribute(valuePointer, (T) loader.convertRawValue(rawValue)); 30 } 31 } 32 addAttribute(String valuePointer, T value)33 public void addAttribute(String valuePointer, T value) { 34 attributeNamesToValues.put(valuePointer, value); 35 resolveUnresolvedReferences(valuePointer, value); 36 } 37 resolveUnresolvedReferences(String attributeName, T value)38 private void resolveUnresolvedReferences(String attributeName, T value) { 39 List<String> references = unresolvedReferences.remove(attributeName); 40 if (references == null) { 41 return; 42 } 43 for (String reference : references) { 44 attributeNamesToValues.put(reference, value); 45 } 46 } 47 addUnresolvedReference(String valuePointer, String attributeName)48 private void addUnresolvedReference(String valuePointer, String attributeName) { 49 List<String> references = unresolvedReferences.get(attributeName); 50 if (references == null) { 51 references = new ArrayList<String>(); 52 unresolvedReferences.put(attributeName, references); 53 } 54 references.add(valuePointer); 55 } 56 addAttributeReference(String rawValue, String valuePointer)57 private void addAttributeReference(String rawValue, String valuePointer) { 58 String attributeName = rawValue.substring(1); 59 T value = attributeNamesToValues.get(attributeName); 60 if (value == null) { 61 addUnresolvedReference(valuePointer, attributeName); 62 } else { 63 attributeNamesToValues.put(valuePointer, value); 64 } 65 } 66 } 67