1package com.xtremelabs.robolectric.shadows;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.regex.Pattern;
6
7import android.util.Patterns;
8
9import com.xtremelabs.robolectric.Robolectric;
10import com.xtremelabs.robolectric.internal.Implements;
11
12@Implements(Patterns.class)
13public class ShadowPatterns {
14
15    public static final String TOP_LEVEL_DOMAIN_STR =
16        "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
17        + "|(biz|b[abdefghijmnorstvwyz])"
18        + "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
19        + "|d[ejkmoz]"
20        + "|(edu|e[cegrstu])"
21        + "|f[ijkmor]"
22        + "|(gov|g[abdefghilmnpqrstuwy])"
23        + "|h[kmnrtu]"
24        + "|(info|int|i[delmnoqrst])"
25        + "|(jobs|j[emop])"
26        + "|k[eghimnprwyz]"
27        + "|l[abcikrstuvy]"
28        + "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
29        + "|(name|net|n[acefgilopruz])"
30        + "|(org|om)"
31        + "|(pro|p[aefghklmnrstwy])"
32        + "|qa"
33        + "|r[eosuw]"
34        + "|s[abcdeghijklmnortuvyz]"
35        + "|(tel|travel|t[cdfghjklmnoprtvwz])"
36        + "|u[agksyz]"
37        + "|v[aceginu]"
38        + "|w[fs]"
39        + "|(\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
40        + "|y[et]"
41        + "|z[amw])";
42
43    public static final Pattern TOP_LEVEL_DOMAIN =
44        Pattern.compile(TOP_LEVEL_DOMAIN_STR);
45
46    public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
47        "(?:"
48        + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
49        + "|(?:biz|b[abdefghijmnorstvwyz])"
50        + "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
51        + "|d[ejkmoz]"
52        + "|(?:edu|e[cegrstu])"
53        + "|f[ijkmor]"
54        + "|(?:gov|g[abdefghilmnpqrstuwy])"
55        + "|h[kmnrtu]"
56        + "|(?:info|int|i[delmnoqrst])"
57        + "|(?:jobs|j[emop])"
58        + "|k[eghimnprwyz]"
59        + "|l[abcikrstuvy]"
60        + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
61        + "|(?:name|net|n[acefgilopruz])"
62        + "|(?:org|om)"
63        + "|(?:pro|p[aefghklmnrstwy])"
64        + "|qa"
65        + "|r[eosuw]"
66        + "|s[abcdeghijklmnortuvyz]"
67        + "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
68        + "|u[agksyz]"
69        + "|v[aceginu]"
70        + "|w[fs]"
71        + "|(?:\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
72        + "|y[et]"
73        + "|z[amw]))";
74
75    public static final String GOOD_IRI_CHAR =
76        "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
77
78    public static final Pattern WEB_URL = Pattern.compile(
79        "((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
80        + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
81        + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
82        + "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+"   // named host
83        + TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
84        + "|(?:(?:25[0-5]|2[0-4]" // or ip address
85        + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
86        + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
87        + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
88        + "|[1-9][0-9]|[0-9])))"
89        + "(?:\\:\\d{1,5})?)" // plus option port number
90        + "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"  // plus option query params
91        + "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
92        + "(?:\\b|$)"); // and finally, a word boundary or end of
93                        // input.  This is to stop foo.sure from
94                        // matching as foo.su
95
96    public static final Pattern IP_ADDRESS
97        = Pattern.compile(
98            "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
99            + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
100            + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
101            + "|[1-9][0-9]|[0-9]))");
102
103    public static final Pattern DOMAIN_NAME
104        = Pattern.compile(
105            "(((([" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]*)*[" + GOOD_IRI_CHAR + "]\\.)+"
106            + TOP_LEVEL_DOMAIN + ")|"
107            + IP_ADDRESS + ")");
108
109    public static final Pattern EMAIL_ADDRESS
110        = Pattern.compile(
111            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
112            "\\@" +
113            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
114            "(" +
115                "\\." +
116                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
117            ")+"
118        );
119
120    public static final Pattern PHONE
121        = Pattern.compile(                      // sdd = space, dot, or dash
122                "(\\+[0-9]+[\\- \\.]*)?"        // +<digits><sdd>*
123                + "(\\([0-9]+\\)[\\- \\.]*)?"   // (<digits>)<sdd>*
124                + "([0-9][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit>
125
126    public static void reset() {
127        Robolectric.Reflection.setFinalStaticField(Patterns.class, "DOMAIN_NAME", DOMAIN_NAME);
128        Robolectric.Reflection.setFinalStaticField(Patterns.class, "EMAIL_ADDRESS", EMAIL_ADDRESS);
129        Robolectric.Reflection.setFinalStaticField(Patterns.class, "IP_ADDRESS", IP_ADDRESS);
130        Robolectric.Reflection.setFinalStaticField(Patterns.class, "PHONE", PHONE);
131        Robolectric.Reflection.setFinalStaticField(Patterns.class, "TOP_LEVEL_DOMAIN",
132            TOP_LEVEL_DOMAIN);
133        Robolectric.Reflection.setFinalStaticField(Patterns.class, "WEB_URL", WEB_URL);
134    }
135}
136