mobile/android/base/sync/setup/activities/WebURLFinder.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/setup/activities/WebURLFinder.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.setup.activities;
     1.9 +
    1.10 +import java.net.URI;
    1.11 +import java.net.URISyntaxException;
    1.12 +import java.util.ArrayList;
    1.13 +import java.util.Collection;
    1.14 +import java.util.LinkedList;
    1.15 +import java.util.List;
    1.16 +import java.util.regex.Matcher;
    1.17 +
    1.18 +import android.util.Patterns;
    1.19 +
    1.20 +public class WebURLFinder {
    1.21 +  public final List<String> candidates;
    1.22 +
    1.23 +  public WebURLFinder(String string) {
    1.24 +    if (string == null) {
    1.25 +      throw new IllegalArgumentException("string must not be null");
    1.26 +    }
    1.27 +
    1.28 +    this.candidates = candidateWebURLs(string);
    1.29 +  }
    1.30 +
    1.31 +  public WebURLFinder(List<String> strings) {
    1.32 +    if (strings == null) {
    1.33 +      throw new IllegalArgumentException("strings must not be null");
    1.34 +    }
    1.35 +
    1.36 +    this.candidates = candidateWebURLs(strings);
    1.37 +  }
    1.38 +
    1.39 +  /**
    1.40 +   * Check if string is a Web URL.
    1.41 +   * <p>
    1.42 +   * A Web URL is a URI that is not a <code>file:</code> or
    1.43 +   * <code>javascript:</code> scheme.
    1.44 +   *
    1.45 +   * @param string
    1.46 +   *          to check.
    1.47 +   * @return <code>true</code> if <code>string</code> is a Web URL.
    1.48 +   */
    1.49 +  public static boolean isWebURL(String string) {
    1.50 +    try {
    1.51 +      new URI(string);
    1.52 +    } catch (Exception e) {
    1.53 +      return false;
    1.54 +    }
    1.55 +
    1.56 +    if (android.webkit.URLUtil.isFileUrl(string) ||
    1.57 +        android.webkit.URLUtil.isJavaScriptUrl(string)) {
    1.58 +      return false;
    1.59 +    }
    1.60 +
    1.61 +    return true;
    1.62 +  }
    1.63 +
    1.64 +  /**
    1.65 +   * Return best Web URL.
    1.66 +   * <p>
    1.67 +   * "Best" means a Web URL with a scheme, and failing that, a Web URL without a
    1.68 +   * scheme.
    1.69 +   *
    1.70 +   * @return a Web URL or <code>null</code>.
    1.71 +   */
    1.72 +  public String bestWebURL() {
    1.73 +    String firstWebURLWithScheme = firstWebURLWithScheme();
    1.74 +    if (firstWebURLWithScheme != null) {
    1.75 +      return firstWebURLWithScheme;
    1.76 +    }
    1.77 +
    1.78 +    return firstWebURLWithoutScheme();
    1.79 +  }
    1.80 +
    1.81 +  protected static List<String> candidateWebURLs(Collection<String> strings) {
    1.82 +    List<String> candidates = new ArrayList<String>();
    1.83 +
    1.84 +    for (String string : strings) {
    1.85 +      if (string == null) {
    1.86 +        continue;
    1.87 +      }
    1.88 +
    1.89 +      candidates.addAll(candidateWebURLs(string));
    1.90 +    }
    1.91 +
    1.92 +    return candidates;
    1.93 +  }
    1.94 +
    1.95 +  protected static List<String> candidateWebURLs(String string) {
    1.96 +    Matcher matcher = Patterns.WEB_URL.matcher(string);
    1.97 +    List<String> matches = new LinkedList<String>();
    1.98 +
    1.99 +    while (matcher.find()) {
   1.100 +      // Remove URLs with bad schemes.
   1.101 +      if (!isWebURL(matcher.group())) {
   1.102 +        continue;
   1.103 +      }
   1.104 +
   1.105 +      // Remove parts of email addresses.
   1.106 +      if (matcher.start() > 0 && (string.charAt(matcher.start() - 1) == '@')) {
   1.107 +        continue;
   1.108 +      }
   1.109 +
   1.110 +      matches.add(matcher.group());
   1.111 +    }
   1.112 +
   1.113 +    return matches;
   1.114 +  }
   1.115 +
   1.116 +  protected String firstWebURLWithScheme() {
   1.117 +    for (String match : candidates) {
   1.118 +      try {
   1.119 +        if (new URI(match).getScheme() != null) {
   1.120 +          return match;
   1.121 +        }
   1.122 +      } catch (URISyntaxException e) {
   1.123 +        // Ignore: on to the next.
   1.124 +        continue;
   1.125 +      }
   1.126 +    }
   1.127 +
   1.128 +    return null;
   1.129 +  }
   1.130 +
   1.131 +  protected String firstWebURLWithoutScheme() {
   1.132 +    if (!candidates.isEmpty()) {
   1.133 +      return candidates.get(0);
   1.134 +    }
   1.135 +
   1.136 +    return null;
   1.137 +  }
   1.138 +}

mercurial