mobile/android/base/util/StringUtils.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.util;
michael@0 7
michael@0 8 import android.net.Uri;
michael@0 9 import android.text.TextUtils;
michael@0 10
michael@0 11 public class StringUtils {
michael@0 12
michael@0 13 private static final String FILTER_URL_PREFIX = "filter://";
michael@0 14
michael@0 15 /*
michael@0 16 * This method tries to guess if the given string could be a search query or URL,
michael@0 17 * and returns a previous result if there is ambiguity
michael@0 18 *
michael@0 19 * Search examples:
michael@0 20 * foo
michael@0 21 * foo bar.com
michael@0 22 * foo http://bar.com
michael@0 23 *
michael@0 24 * URL examples
michael@0 25 * foo.com
michael@0 26 * foo.c
michael@0 27 * :foo
michael@0 28 * http://foo.com bar
michael@0 29 *
michael@0 30 * wasSearchQuery specifies whether text was a search query before the latest change
michael@0 31 * in text. In ambiguous cases where the new text can be either a search or a URL,
michael@0 32 * wasSearchQuery is returned
michael@0 33 */
michael@0 34 public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
michael@0 35 // We remove leading and trailing white spaces when decoding URLs
michael@0 36 text = text.trim();
michael@0 37 if (text.length() == 0)
michael@0 38 return wasSearchQuery;
michael@0 39
michael@0 40 int colon = text.indexOf(':');
michael@0 41 int dot = text.indexOf('.');
michael@0 42 int space = text.indexOf(' ');
michael@0 43
michael@0 44 // If a space is found before any dot and colon, we assume this is a search query
michael@0 45 if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
michael@0 46 return true;
michael@0 47 }
michael@0 48 // Otherwise, if a dot or a colon is found, we assume this is a URL
michael@0 49 if (dot > -1 || colon > -1) {
michael@0 50 return false;
michael@0 51 }
michael@0 52 // Otherwise, text is ambiguous, and we keep its status unchanged
michael@0 53 return wasSearchQuery;
michael@0 54 }
michael@0 55
michael@0 56 public static class UrlFlags {
michael@0 57 public static final int NONE = 0;
michael@0 58 public static final int STRIP_HTTPS = 1;
michael@0 59 }
michael@0 60
michael@0 61 public static String stripScheme(String url) {
michael@0 62 return stripScheme(url, UrlFlags.NONE);
michael@0 63 }
michael@0 64
michael@0 65 public static String stripScheme(String url, int flags) {
michael@0 66 if (url == null) {
michael@0 67 return url;
michael@0 68 }
michael@0 69
michael@0 70 int start = 0;
michael@0 71 int end = url.length();
michael@0 72
michael@0 73 if (url.startsWith("http://")) {
michael@0 74 start = 7;
michael@0 75 } else if (url.startsWith("https://") && flags == UrlFlags.STRIP_HTTPS) {
michael@0 76 start = 8;
michael@0 77 }
michael@0 78
michael@0 79 if (url.endsWith("/")) {
michael@0 80 end--;
michael@0 81 }
michael@0 82
michael@0 83 return url.substring(start, end);
michael@0 84 }
michael@0 85
michael@0 86 public static String stripCommonSubdomains(String host) {
michael@0 87 if (host == null) {
michael@0 88 return host;
michael@0 89 }
michael@0 90
michael@0 91 // In contrast to desktop, we also strip mobile subdomains,
michael@0 92 // since its unlikely users are intentionally typing them
michael@0 93 int start = 0;
michael@0 94
michael@0 95 if (host.startsWith("www.")) {
michael@0 96 start = 4;
michael@0 97 } else if (host.startsWith("mobile.")) {
michael@0 98 start = 7;
michael@0 99 } else if (host.startsWith("m.")) {
michael@0 100 start = 2;
michael@0 101 }
michael@0 102
michael@0 103 return host.substring(start);
michael@0 104 }
michael@0 105
michael@0 106 /**
michael@0 107 * Searches the url query string for the first value with the given key.
michael@0 108 */
michael@0 109 public static String getQueryParameter(String url, String desiredKey) {
michael@0 110 if (TextUtils.isEmpty(url) || TextUtils.isEmpty(desiredKey)) {
michael@0 111 return null;
michael@0 112 }
michael@0 113
michael@0 114 final String[] urlParts = url.split("\\?");
michael@0 115 if (urlParts.length < 2) {
michael@0 116 return null;
michael@0 117 }
michael@0 118
michael@0 119 final String query = urlParts[1];
michael@0 120 for (final String param : query.split("&")) {
michael@0 121 final String pair[] = param.split("=");
michael@0 122 final String key = Uri.decode(pair[0]);
michael@0 123
michael@0 124 // Key is empty or does not match the key we're looking for, discard
michael@0 125 if (TextUtils.isEmpty(key) || !key.equals(desiredKey)) {
michael@0 126 continue;
michael@0 127 }
michael@0 128 // No value associated with key, discard
michael@0 129 if (pair.length < 2) {
michael@0 130 continue;
michael@0 131 }
michael@0 132 final String value = Uri.decode(pair[1]);
michael@0 133 if (TextUtils.isEmpty(value)) {
michael@0 134 return null;
michael@0 135 }
michael@0 136 return value;
michael@0 137 }
michael@0 138
michael@0 139 return null;
michael@0 140 }
michael@0 141
michael@0 142 public static boolean isFilterUrl(String url) {
michael@0 143 if (TextUtils.isEmpty(url)) {
michael@0 144 return false;
michael@0 145 }
michael@0 146
michael@0 147 return url.startsWith(FILTER_URL_PREFIX);
michael@0 148 }
michael@0 149
michael@0 150 public static String getFilterFromUrl(String url) {
michael@0 151 if (TextUtils.isEmpty(url)) {
michael@0 152 return null;
michael@0 153 }
michael@0 154
michael@0 155 return url.substring(FILTER_URL_PREFIX.length());
michael@0 156 }
michael@0 157 }

mercurial