Home | History | Annotate | Download | only in res
      1 package org.robolectric.res;
      2 
      3 import java.util.regex.Pattern;
      4 import javax.annotation.Nullable;
      5 
      6 public enum ResType {
      7   DRAWABLE,
      8   ATTR_DATA,
      9   BOOLEAN,
     10   COLOR,
     11   COLOR_STATE_LIST,
     12   DIMEN,
     13   FILE,
     14   FLOAT,
     15   FRACTION,
     16   INTEGER,
     17   LAYOUT,
     18   STYLE,
     19   CHAR_SEQUENCE,
     20   CHAR_SEQUENCE_ARRAY,
     21   INTEGER_ARRAY,
     22   TYPED_ARRAY,
     23   NULL;
     24 
     25   private static final Pattern DIMEN_RE = Pattern.compile("^\\d+(dp|dip|sp|pt|px|mm|in)$");
     26 
     27   @Nullable
     28   public static ResType inferType(String itemString) {
     29     ResType itemResType = ResType.inferFromValue(itemString);
     30     if (itemResType == ResType.CHAR_SEQUENCE) {
     31       if (AttributeResource.isStyleReference(itemString)) {
     32         itemResType = ResType.STYLE;
     33       } else if (itemString.equals("@null")) {
     34         itemResType = ResType.NULL;
     35       } else if (AttributeResource.isResourceReference(itemString)) {
     36         // This is a reference; no type info needed.
     37         itemResType = null;
     38       }
     39     }
     40     return itemResType;
     41   }
     42 
     43   /**
     44    * Parses a resource value to infer the type
     45    */
     46   public static ResType inferFromValue(String value) {
     47     if (value.startsWith("#")) {
     48       return COLOR;
     49     } else if ("true".equals(value) || "false".equals(value)) {
     50       return BOOLEAN;
     51     } else if (DIMEN_RE.matcher(value).find()) {
     52       return DIMEN;
     53     } else {
     54       try {
     55         Integer.parseInt(value);
     56         return INTEGER;
     57       } catch (NumberFormatException nfe) {}
     58 
     59       try {
     60         Float.parseFloat(value);
     61         return FRACTION;
     62       } catch (NumberFormatException nfe) {}
     63 
     64 
     65       return CHAR_SEQUENCE;
     66     }
     67   }
     68 }
     69