michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import android.net.Uri; michael@0: import android.text.TextUtils; michael@0: michael@0: public class StringUtils { michael@0: michael@0: private static final String FILTER_URL_PREFIX = "filter://"; michael@0: michael@0: /* michael@0: * This method tries to guess if the given string could be a search query or URL, michael@0: * and returns a previous result if there is ambiguity michael@0: * michael@0: * Search examples: michael@0: * foo michael@0: * foo bar.com michael@0: * foo http://bar.com michael@0: * michael@0: * URL examples michael@0: * foo.com michael@0: * foo.c michael@0: * :foo michael@0: * http://foo.com bar michael@0: * michael@0: * wasSearchQuery specifies whether text was a search query before the latest change michael@0: * in text. In ambiguous cases where the new text can be either a search or a URL, michael@0: * wasSearchQuery is returned michael@0: */ michael@0: public static boolean isSearchQuery(String text, boolean wasSearchQuery) { michael@0: // We remove leading and trailing white spaces when decoding URLs michael@0: text = text.trim(); michael@0: if (text.length() == 0) michael@0: return wasSearchQuery; michael@0: michael@0: int colon = text.indexOf(':'); michael@0: int dot = text.indexOf('.'); michael@0: int space = text.indexOf(' '); michael@0: michael@0: // If a space is found before any dot and colon, we assume this is a search query michael@0: if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) { michael@0: return true; michael@0: } michael@0: // Otherwise, if a dot or a colon is found, we assume this is a URL michael@0: if (dot > -1 || colon > -1) { michael@0: return false; michael@0: } michael@0: // Otherwise, text is ambiguous, and we keep its status unchanged michael@0: return wasSearchQuery; michael@0: } michael@0: michael@0: public static class UrlFlags { michael@0: public static final int NONE = 0; michael@0: public static final int STRIP_HTTPS = 1; michael@0: } michael@0: michael@0: public static String stripScheme(String url) { michael@0: return stripScheme(url, UrlFlags.NONE); michael@0: } michael@0: michael@0: public static String stripScheme(String url, int flags) { michael@0: if (url == null) { michael@0: return url; michael@0: } michael@0: michael@0: int start = 0; michael@0: int end = url.length(); michael@0: michael@0: if (url.startsWith("http://")) { michael@0: start = 7; michael@0: } else if (url.startsWith("https://") && flags == UrlFlags.STRIP_HTTPS) { michael@0: start = 8; michael@0: } michael@0: michael@0: if (url.endsWith("/")) { michael@0: end--; michael@0: } michael@0: michael@0: return url.substring(start, end); michael@0: } michael@0: michael@0: public static String stripCommonSubdomains(String host) { michael@0: if (host == null) { michael@0: return host; michael@0: } michael@0: michael@0: // In contrast to desktop, we also strip mobile subdomains, michael@0: // since its unlikely users are intentionally typing them michael@0: int start = 0; michael@0: michael@0: if (host.startsWith("www.")) { michael@0: start = 4; michael@0: } else if (host.startsWith("mobile.")) { michael@0: start = 7; michael@0: } else if (host.startsWith("m.")) { michael@0: start = 2; michael@0: } michael@0: michael@0: return host.substring(start); michael@0: } michael@0: michael@0: /** michael@0: * Searches the url query string for the first value with the given key. michael@0: */ michael@0: public static String getQueryParameter(String url, String desiredKey) { michael@0: if (TextUtils.isEmpty(url) || TextUtils.isEmpty(desiredKey)) { michael@0: return null; michael@0: } michael@0: michael@0: final String[] urlParts = url.split("\\?"); michael@0: if (urlParts.length < 2) { michael@0: return null; michael@0: } michael@0: michael@0: final String query = urlParts[1]; michael@0: for (final String param : query.split("&")) { michael@0: final String pair[] = param.split("="); michael@0: final String key = Uri.decode(pair[0]); michael@0: michael@0: // Key is empty or does not match the key we're looking for, discard michael@0: if (TextUtils.isEmpty(key) || !key.equals(desiredKey)) { michael@0: continue; michael@0: } michael@0: // No value associated with key, discard michael@0: if (pair.length < 2) { michael@0: continue; michael@0: } michael@0: final String value = Uri.decode(pair[1]); michael@0: if (TextUtils.isEmpty(value)) { michael@0: return null; michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: public static boolean isFilterUrl(String url) { michael@0: if (TextUtils.isEmpty(url)) { michael@0: return false; michael@0: } michael@0: michael@0: return url.startsWith(FILTER_URL_PREFIX); michael@0: } michael@0: michael@0: public static String getFilterFromUrl(String url) { michael@0: if (TextUtils.isEmpty(url)) { michael@0: return null; michael@0: } michael@0: michael@0: return url.substring(FILTER_URL_PREFIX.length()); michael@0: } michael@0: }