1 package org.robolectric.res; 2 3 import java.util.regex.Pattern; 4 import javax.annotation.Nonnull; 5 6 public class AttributeResource { 7 public static final String ANDROID_NS = "http://schemas.android.com/apk/res/android"; 8 public static final String ANDROID_RES_NS_PREFIX = "http://schemas.android.com/apk/res/"; 9 public static final String RES_AUTO_NS_URI = "http://schemas.android.com/apk/res-auto"; 10 11 public static final String NULL_VALUE = "@null"; 12 public static final String EMPTY_VALUE = "@empty"; 13 public static final Pattern IS_RESOURCE_REFERENCE = Pattern.compile("^\\s*@"); 14 15 public final @Nonnull ResName resName; 16 public final @Nonnull String value; 17 public final @Nonnull String trimmedValue; 18 public final @Nonnull String contextPackageName; 19 private final Integer referenceResId; 20 AttributeResource(@onnull ResName resName, @Nonnull String value, @Nonnull String contextPackageName)21 public AttributeResource(@Nonnull ResName resName, @Nonnull String value, @Nonnull String contextPackageName) { 22 this(resName, value, contextPackageName, null); 23 } 24 AttributeResource(@onnull ResName resName, @Nonnull String value, @Nonnull String contextPackageName, Integer referenceResId)25 public AttributeResource(@Nonnull ResName resName, @Nonnull String value, @Nonnull String contextPackageName, Integer referenceResId) { 26 this.referenceResId = referenceResId; 27 if (!resName.type.equals("attr")) throw new IllegalStateException("\"" + resName.getFullyQualifiedName() + "\" unexpected"); 28 29 this.resName = resName; 30 this.value = value; 31 this.trimmedValue = value.trim(); 32 this.contextPackageName = contextPackageName; 33 } 34 isResourceReference()35 public boolean isResourceReference() { 36 return isResourceReference(trimmedValue); 37 } 38 getResourceReference()39 public @Nonnull ResName getResourceReference() { 40 if (!isResourceReference()) throw new RuntimeException("not a resource reference: " + this); 41 return ResName.qualifyResName(deref(trimmedValue).replace("+", ""), contextPackageName, "style"); 42 } 43 isStyleReference()44 public boolean isStyleReference() { 45 return isStyleReference(trimmedValue); 46 } 47 getStyleReference()48 public ResName getStyleReference() { 49 if (!isStyleReference()) throw new RuntimeException("not a style reference: " + this); 50 return ResName.qualifyResName(value.substring(1), contextPackageName, "attr"); 51 } 52 isNull()53 public boolean isNull() { 54 return NULL_VALUE.equals(trimmedValue); 55 } 56 isEmpty()57 public boolean isEmpty() { 58 return EMPTY_VALUE.equals(trimmedValue); 59 } 60 61 @Override toString()62 public String toString() { 63 return "Attribute{" + 64 "name='" + resName + '\'' + 65 ", value='" + value + '\'' + 66 ", contextPackageName='" + contextPackageName + '\'' + 67 '}'; 68 } 69 isResourceReference(String value)70 public static boolean isResourceReference(String value) { 71 return IS_RESOURCE_REFERENCE.matcher(value).find() && !isNull(value); 72 } 73 getResourceReference(String value, String defPackage, String defType)74 public static @Nonnull ResName getResourceReference(String value, String defPackage, String defType) { 75 if (!isResourceReference(value)) throw new IllegalArgumentException("not a resource reference: " + value); 76 return ResName.qualifyResName(deref(value).replace("+", ""), defPackage, defType); 77 } 78 deref(@onnull String value)79 private static @Nonnull String deref(@Nonnull String value) { 80 return value.substring(value.indexOf('@') + 1); 81 } 82 isStyleReference(String value)83 public static boolean isStyleReference(String value) { 84 return value.startsWith("?"); 85 } 86 getStyleReference(String value, String defPackage, String defType)87 public static ResName getStyleReference(String value, String defPackage, String defType) { 88 if (!isStyleReference(value)) throw new IllegalArgumentException("not a style reference: " + value); 89 return ResName.qualifyResName(value.substring(1), defPackage, defType); 90 } 91 isNull(String value)92 public static boolean isNull(String value) { 93 return NULL_VALUE.equals(value); 94 } 95 isEmpty(String value)96 public static boolean isEmpty(String value) { 97 return EMPTY_VALUE.equals(value); 98 } 99 getReferenceResId()100 public Integer getReferenceResId() { 101 return referenceResId; 102 } 103 } 104