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