• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
inferType(String itemString)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    */
inferFromValue(String value)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 if (isInteger(value)) {
54       return INTEGER;
55     } else if (isFloat(value)) {
56       return FRACTION;
57     } else {
58       return CHAR_SEQUENCE;
59     }
60   }
61 
isInteger(String value)62   private static boolean isInteger(String value) {
63     try {
64       Integer.parseInt(value);
65       return true;
66     } catch (NumberFormatException nfe) {
67       return false;
68     }
69   }
70 
isFloat(String value)71   private static boolean isFloat(String value) {
72     try {
73       Float.parseFloat(value);
74       return true;
75     } catch (NumberFormatException nfe) {
76       return false;
77     }
78   }
79 }
80