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