mobile/android/base/sync/setup/activities/WebURLFinder.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.sync.setup.activities;
michael@0 6
michael@0 7 import java.net.URI;
michael@0 8 import java.net.URISyntaxException;
michael@0 9 import java.util.ArrayList;
michael@0 10 import java.util.Collection;
michael@0 11 import java.util.LinkedList;
michael@0 12 import java.util.List;
michael@0 13 import java.util.regex.Matcher;
michael@0 14
michael@0 15 import android.util.Patterns;
michael@0 16
michael@0 17 public class WebURLFinder {
michael@0 18 public final List<String> candidates;
michael@0 19
michael@0 20 public WebURLFinder(String string) {
michael@0 21 if (string == null) {
michael@0 22 throw new IllegalArgumentException("string must not be null");
michael@0 23 }
michael@0 24
michael@0 25 this.candidates = candidateWebURLs(string);
michael@0 26 }
michael@0 27
michael@0 28 public WebURLFinder(List<String> strings) {
michael@0 29 if (strings == null) {
michael@0 30 throw new IllegalArgumentException("strings must not be null");
michael@0 31 }
michael@0 32
michael@0 33 this.candidates = candidateWebURLs(strings);
michael@0 34 }
michael@0 35
michael@0 36 /**
michael@0 37 * Check if string is a Web URL.
michael@0 38 * <p>
michael@0 39 * A Web URL is a URI that is not a <code>file:</code> or
michael@0 40 * <code>javascript:</code> scheme.
michael@0 41 *
michael@0 42 * @param string
michael@0 43 * to check.
michael@0 44 * @return <code>true</code> if <code>string</code> is a Web URL.
michael@0 45 */
michael@0 46 public static boolean isWebURL(String string) {
michael@0 47 try {
michael@0 48 new URI(string);
michael@0 49 } catch (Exception e) {
michael@0 50 return false;
michael@0 51 }
michael@0 52
michael@0 53 if (android.webkit.URLUtil.isFileUrl(string) ||
michael@0 54 android.webkit.URLUtil.isJavaScriptUrl(string)) {
michael@0 55 return false;
michael@0 56 }
michael@0 57
michael@0 58 return true;
michael@0 59 }
michael@0 60
michael@0 61 /**
michael@0 62 * Return best Web URL.
michael@0 63 * <p>
michael@0 64 * "Best" means a Web URL with a scheme, and failing that, a Web URL without a
michael@0 65 * scheme.
michael@0 66 *
michael@0 67 * @return a Web URL or <code>null</code>.
michael@0 68 */
michael@0 69 public String bestWebURL() {
michael@0 70 String firstWebURLWithScheme = firstWebURLWithScheme();
michael@0 71 if (firstWebURLWithScheme != null) {
michael@0 72 return firstWebURLWithScheme;
michael@0 73 }
michael@0 74
michael@0 75 return firstWebURLWithoutScheme();
michael@0 76 }
michael@0 77
michael@0 78 protected static List<String> candidateWebURLs(Collection<String> strings) {
michael@0 79 List<String> candidates = new ArrayList<String>();
michael@0 80
michael@0 81 for (String string : strings) {
michael@0 82 if (string == null) {
michael@0 83 continue;
michael@0 84 }
michael@0 85
michael@0 86 candidates.addAll(candidateWebURLs(string));
michael@0 87 }
michael@0 88
michael@0 89 return candidates;
michael@0 90 }
michael@0 91
michael@0 92 protected static List<String> candidateWebURLs(String string) {
michael@0 93 Matcher matcher = Patterns.WEB_URL.matcher(string);
michael@0 94 List<String> matches = new LinkedList<String>();
michael@0 95
michael@0 96 while (matcher.find()) {
michael@0 97 // Remove URLs with bad schemes.
michael@0 98 if (!isWebURL(matcher.group())) {
michael@0 99 continue;
michael@0 100 }
michael@0 101
michael@0 102 // Remove parts of email addresses.
michael@0 103 if (matcher.start() > 0 && (string.charAt(matcher.start() - 1) == '@')) {
michael@0 104 continue;
michael@0 105 }
michael@0 106
michael@0 107 matches.add(matcher.group());
michael@0 108 }
michael@0 109
michael@0 110 return matches;
michael@0 111 }
michael@0 112
michael@0 113 protected String firstWebURLWithScheme() {
michael@0 114 for (String match : candidates) {
michael@0 115 try {
michael@0 116 if (new URI(match).getScheme() != null) {
michael@0 117 return match;
michael@0 118 }
michael@0 119 } catch (URISyntaxException e) {
michael@0 120 // Ignore: on to the next.
michael@0 121 continue;
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 return null;
michael@0 126 }
michael@0 127
michael@0 128 protected String firstWebURLWithoutScheme() {
michael@0 129 if (!candidates.isEmpty()) {
michael@0 130 return candidates.get(0);
michael@0 131 }
michael@0 132
michael@0 133 return null;
michael@0 134 }
michael@0 135 }

mercurial