blob: 0a7e90c40a5730bcbd7996a2f3fd284beeb4cd6a [file] [log] [blame]
John Reckfb3017f2010-10-26 19:01:24 -07001/*
2 * Copyright (C) 2010 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 */
Michael Kolb8233fac2010-10-26 16:08:53 -070016
John Reckfb3017f2010-10-26 19:01:24 -070017package com.android.browser;
18
John Reck324d4402011-01-11 16:56:42 -080019import com.android.browser.homepages.HomeProvider;
20
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.net.Uri;
22import android.util.Patterns;
23import android.webkit.URLUtil;
24
John Reckfb3017f2010-10-26 19:01:24 -070025import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27
Michael Kolb8233fac2010-10-26 16:08:53 -070028/**
29 * Utility methods for Url manipulation
30 */
John Reckfb3017f2010-10-26 19:01:24 -070031public class UrlUtils {
32
Michael Kolb8233fac2010-10-26 16:08:53 -070033 static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
34 "(?i)" + // switch on case insensitive matching
35 "(" + // begin group for schema
36 "(?:http|https|file):\\/\\/" +
Patrick Scottb92bbb42011-01-05 11:38:58 -050037 "|(?:inline|data|about|javascript):" +
Michael Kolb8233fac2010-10-26 16:08:53 -070038 ")" +
39 "(.*)" );
40
41 // Google search
42 private final static String QUICKSEARCH_G = "http://www.google.com/m?q=%s";
43 private final static String QUERY_PLACE_HOLDER = "%s";
44
John Reckfb3017f2010-10-26 19:01:24 -070045 // Regular expression which matches http://, followed by some stuff, followed by
46 // optionally a trailing slash, all matched as separate groups.
47 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
48
49 private UrlUtils() { /* cannot be instantiated */ }
50
51 /**
52 * Strips the provided url of preceding "http://" and any trailing "/". Does not
53 * strip "https://". If the provided string cannot be stripped, the original string
54 * is returned.
55 *
56 * TODO: Put this in TextUtils to be used by other packages doing something similar.
57 *
58 * @param url a url to strip, like "http://www.google.com/"
59 * @return a stripped url like "www.google.com", or the original string if it could
60 * not be stripped
61 */
62 /* package */ static String stripUrl(String url) {
63 if (url == null) return null;
64 Matcher m = STRIP_URL_PATTERN.matcher(url);
65 if (m.matches() && m.groupCount() == 3) {
66 return m.group(2);
67 } else {
68 return url;
69 }
70 }
Michael Kolb8233fac2010-10-26 16:08:53 -070071
72 protected static String smartUrlFilter(Uri inUri) {
73 if (inUri != null) {
74 return smartUrlFilter(inUri.toString());
75 }
76 return null;
77 }
78
79 /**
80 * Attempts to determine whether user input is a URL or search
81 * terms. Anything with a space is passed to search.
82 *
83 * Converts to lowercase any mistakenly uppercased schema (i.e.,
84 * "Http://" converts to "http://"
85 *
86 * @return Original or modified URL
87 *
88 */
89 protected static String smartUrlFilter(String url) {
90
91 String inUrl = url.trim();
92 boolean hasSpace = inUrl.indexOf(' ') != -1;
93
94 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
95 if (matcher.matches()) {
96 // force scheme to lowercase
97 String scheme = matcher.group(1);
98 String lcScheme = scheme.toLowerCase();
99 if (!lcScheme.equals(scheme)) {
100 inUrl = lcScheme + matcher.group(2);
101 }
102 if (hasSpace) {
103 inUrl = inUrl.replace(" ", "%20");
104 }
105 return inUrl;
106 }
107 if (!hasSpace) {
108 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
109 return URLUtil.guessUrl(inUrl);
110 }
111 }
112
113 // FIXME: Is this the correct place to add to searches?
114 // what if someone else calls this function?
115
116// Browser.addSearchUrl(mBrowser.getContentResolver(), inUrl);
117 return URLUtil.composeSearchUrl(inUrl, QUICKSEARCH_G, QUERY_PLACE_HOLDER);
118 }
119
120 /* package */ static String fixUrl(String inUrl) {
121 // FIXME: Converting the url to lower case
122 // duplicates functionality in smartUrlFilter().
123 // However, changing all current callers of fixUrl to
124 // call smartUrlFilter in addition may have unwanted
125 // consequences, and is deferred for now.
126 int colon = inUrl.indexOf(':');
127 boolean allLower = true;
128 for (int index = 0; index < colon; index++) {
129 char ch = inUrl.charAt(index);
130 if (!Character.isLetter(ch)) {
131 break;
132 }
133 allLower &= Character.isLowerCase(ch);
134 if (index == colon - 1 && !allLower) {
135 inUrl = inUrl.substring(0, colon).toLowerCase()
136 + inUrl.substring(colon);
137 }
138 }
139 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
140 return inUrl;
141 if (inUrl.startsWith("http:") ||
142 inUrl.startsWith("https:")) {
143 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
144 inUrl = inUrl.replaceFirst("/", "//");
145 } else inUrl = inUrl.replaceFirst(":", "://");
146 }
147 return inUrl;
148 }
149
John Reck324d4402011-01-11 16:56:42 -0800150 // Returns the filtered URL. Cannot return null, but can return an empty string
151 /* package */ static String filteredUrl(String inUrl) {
152 if (inUrl == null) {
153 return "";
154 }
155 if (inUrl.startsWith(HomeProvider.MOST_VISITED)) {
156 return "";
157 }
158 return inUrl;
159 }
160
John Reckfb3017f2010-10-26 19:01:24 -0700161}