mobile/android/base/util/StringUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/StringUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.util;
    1.10 +
    1.11 +import android.net.Uri;
    1.12 +import android.text.TextUtils;
    1.13 +
    1.14 +public class StringUtils {
    1.15 +
    1.16 +    private static final String FILTER_URL_PREFIX = "filter://";
    1.17 +
    1.18 +    /*
    1.19 +     * This method tries to guess if the given string could be a search query or URL,
    1.20 +     * and returns a previous result if there is ambiguity
    1.21 +     *
    1.22 +     * Search examples:
    1.23 +     *  foo
    1.24 +     *  foo bar.com
    1.25 +     *  foo http://bar.com
    1.26 +     *
    1.27 +     * URL examples
    1.28 +     *  foo.com
    1.29 +     *  foo.c
    1.30 +     *  :foo
    1.31 +     *  http://foo.com bar
    1.32 +     *
    1.33 +     * wasSearchQuery specifies whether text was a search query before the latest change
    1.34 +     * in text. In ambiguous cases where the new text can be either a search or a URL,
    1.35 +     * wasSearchQuery is returned
    1.36 +    */
    1.37 +    public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
    1.38 +        // We remove leading and trailing white spaces when decoding URLs
    1.39 +        text = text.trim();
    1.40 +        if (text.length() == 0)
    1.41 +            return wasSearchQuery;
    1.42 +
    1.43 +        int colon = text.indexOf(':');
    1.44 +        int dot = text.indexOf('.');
    1.45 +        int space = text.indexOf(' ');
    1.46 +
    1.47 +        // If a space is found before any dot and colon, we assume this is a search query
    1.48 +        if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
    1.49 +            return true;
    1.50 +        }
    1.51 +        // Otherwise, if a dot or a colon is found, we assume this is a URL
    1.52 +        if (dot > -1 || colon > -1) {
    1.53 +            return false;
    1.54 +        }
    1.55 +        // Otherwise, text is ambiguous, and we keep its status unchanged
    1.56 +        return wasSearchQuery;
    1.57 +    }
    1.58 +
    1.59 +    public static class UrlFlags {
    1.60 +        public static final int NONE = 0;
    1.61 +        public static final int STRIP_HTTPS = 1;
    1.62 +    }
    1.63 +
    1.64 +    public static String stripScheme(String url) {
    1.65 +        return stripScheme(url, UrlFlags.NONE);
    1.66 +    }
    1.67 +
    1.68 +    public static String stripScheme(String url, int flags) {
    1.69 +        if (url == null) {
    1.70 +            return url;
    1.71 +        }
    1.72 +
    1.73 +        int start = 0;
    1.74 +        int end = url.length();
    1.75 +
    1.76 +        if (url.startsWith("http://")) {
    1.77 +            start = 7;
    1.78 +        } else if (url.startsWith("https://") && flags == UrlFlags.STRIP_HTTPS) {
    1.79 +            start = 8;
    1.80 +        }
    1.81 +
    1.82 +        if (url.endsWith("/")) {
    1.83 +            end--;
    1.84 +        }
    1.85 +
    1.86 +        return url.substring(start, end);
    1.87 +    }
    1.88 +
    1.89 +    public static String stripCommonSubdomains(String host) {
    1.90 +        if (host == null) {
    1.91 +            return host;
    1.92 +        }
    1.93 +
    1.94 +        // In contrast to desktop, we also strip mobile subdomains,
    1.95 +        // since its unlikely users are intentionally typing them
    1.96 +        int start = 0;
    1.97 +
    1.98 +        if (host.startsWith("www.")) {
    1.99 +            start = 4;
   1.100 +        } else if (host.startsWith("mobile.")) {
   1.101 +            start = 7;
   1.102 +        } else if (host.startsWith("m.")) {
   1.103 +            start = 2;
   1.104 +        }
   1.105 +
   1.106 +        return host.substring(start);
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Searches the url query string for the first value with the given key.
   1.111 +     */
   1.112 +    public static String getQueryParameter(String url, String desiredKey) {
   1.113 +        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(desiredKey)) {
   1.114 +            return null;
   1.115 +        }
   1.116 +
   1.117 +        final String[] urlParts = url.split("\\?");
   1.118 +        if (urlParts.length < 2) {
   1.119 +            return null;
   1.120 +        }
   1.121 +
   1.122 +        final String query = urlParts[1];
   1.123 +        for (final String param : query.split("&")) {
   1.124 +            final String pair[] = param.split("=");
   1.125 +            final String key = Uri.decode(pair[0]);
   1.126 +
   1.127 +            // Key is empty or does not match the key we're looking for, discard
   1.128 +            if (TextUtils.isEmpty(key) || !key.equals(desiredKey)) {
   1.129 +                continue;
   1.130 +            }
   1.131 +            // No value associated with key, discard
   1.132 +            if (pair.length < 2) {
   1.133 +                continue;
   1.134 +            }
   1.135 +            final String value = Uri.decode(pair[1]);
   1.136 +            if (TextUtils.isEmpty(value)) {
   1.137 +                return null;
   1.138 +            }
   1.139 +            return value;
   1.140 +        }
   1.141 +
   1.142 +        return null;
   1.143 +    }
   1.144 +
   1.145 +    public static boolean isFilterUrl(String url) {
   1.146 +        if (TextUtils.isEmpty(url)) {
   1.147 +            return false;
   1.148 +        }
   1.149 +
   1.150 +        return url.startsWith(FILTER_URL_PREFIX);
   1.151 +    }
   1.152 +
   1.153 +    public static String getFilterFromUrl(String url) {
   1.154 +        if (TextUtils.isEmpty(url)) {
   1.155 +            return null;
   1.156 +        }
   1.157 +
   1.158 +        return url.substring(FILTER_URL_PREFIX.length());
   1.159 +    }
   1.160 +}

mercurial