Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.util.regex.Matcher;
     18 import java.util.regex.Pattern;
     19 
     20 public class Main {
     21     // from android.util.Patterns
     22     public static final String GOOD_IRI_CHAR =
     23         "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
     24 
     25     public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
     26         "(?:"
     27         + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
     28         + "|(?:biz|b[abdefghijmnorstvwyz])"
     29         + "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
     30         + "|d[ejkmoz]"
     31         + "|(?:edu|e[cegrstu])"
     32         + "|f[ijkmor]"
     33         + "|(?:gov|g[abdefghilmnpqrstuwy])"
     34         + "|h[kmnrtu]"
     35         + "|(?:info|int|i[delmnoqrst])"
     36         + "|(?:jobs|j[emop])"
     37         + "|k[eghimnprwyz]"
     38         + "|l[abcikrstuvy]"
     39         + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
     40         + "|(?:name|net|n[acefgilopruz])"
     41         + "|(?:org|om)"
     42         + "|(?:pro|p[aefghklmnrstwy])"
     43         + "|qa"
     44         + "|r[eosuw]"
     45         + "|s[abcdeghijklmnortuvyz]"
     46         + "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
     47         + "|u[agksyz]"
     48         + "|v[aceginu]"
     49         + "|w[fs]"
     50         + "|(?:\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)"
     51         + "|y[et]"
     52         + "|z[amw]))";
     53 
     54     public static final String WEB_URL_STR =
     55         "((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
     56         + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
     57         + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
     58         + "((?:(?:[" + GOOD_IRI_CHAR + "][" + GOOD_IRI_CHAR + "\\-]{0,64}\\.)+"   // named host
     59         + TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
     60         + "|(?:(?:25[0-5]|2[0-4]" // or ip address
     61         + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
     62         + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
     63         + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
     64         + "|[1-9][0-9]|[0-9])))"
     65         + "(?:\\:\\d{1,5})?)" // plus option port number
     66         + "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"  // plus option query params
     67         + "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
     68         + "(?:\\b|$)"; // and finally, a word boundary or end of
     69                         // input.  This is to stop foo.sure from
     70                         // matching as foo.su
     71 
     72     public static final Pattern WEB_URL = Pattern.compile(WEB_URL_STR);
     73 
     74     public static final String testStr1 =
     75         "http://www.google.com/blah?client=tablet-android&source=android-home";
     76     public static final String testStr2 = "http:///www.google.com/";
     77     public static final String testStr3 =
     78         "http://www.google.com/search?hl=en&redir_esc=&client=tablet-android-verizon&source=android-browser-type&v=141000000&qsubts=1327020479959&action=devloc&q=cnn";
     79 
     80     public static void main(String[] args) {
     81         System.out.println("str1 matches: " + WEB_URL.matcher(testStr1).matches());
     82         System.out.println("str2 matches: " + WEB_URL.matcher(testStr2).matches());
     83         System.out.println("str3 matches: " + WEB_URL.matcher(testStr3).matches());
     84     }
     85 
     86     static String getStringAsHex(String text) {
     87         StringBuilder sb = new StringBuilder(text.length() * 4);
     88 
     89         for (int i = 0; i < text.length(); i++) {
     90             sb.append(Integer.toHexString((int) text.charAt(i)));
     91         }
     92 
     93         return sb.toString();
     94     }
     95 }
     96