michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.setup.activities; michael@0: michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: import java.util.ArrayList; michael@0: import java.util.Collection; michael@0: import java.util.LinkedList; michael@0: import java.util.List; michael@0: import java.util.regex.Matcher; michael@0: michael@0: import android.util.Patterns; michael@0: michael@0: public class WebURLFinder { michael@0: public final List candidates; michael@0: michael@0: public WebURLFinder(String string) { michael@0: if (string == null) { michael@0: throw new IllegalArgumentException("string must not be null"); michael@0: } michael@0: michael@0: this.candidates = candidateWebURLs(string); michael@0: } michael@0: michael@0: public WebURLFinder(List strings) { michael@0: if (strings == null) { michael@0: throw new IllegalArgumentException("strings must not be null"); michael@0: } michael@0: michael@0: this.candidates = candidateWebURLs(strings); michael@0: } michael@0: michael@0: /** michael@0: * Check if string is a Web URL. michael@0: *

michael@0: * A Web URL is a URI that is not a file: or michael@0: * javascript: scheme. michael@0: * michael@0: * @param string michael@0: * to check. michael@0: * @return true if string is a Web URL. michael@0: */ michael@0: public static boolean isWebURL(String string) { michael@0: try { michael@0: new URI(string); michael@0: } catch (Exception e) { michael@0: return false; michael@0: } michael@0: michael@0: if (android.webkit.URLUtil.isFileUrl(string) || michael@0: android.webkit.URLUtil.isJavaScriptUrl(string)) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Return best Web URL. michael@0: *

michael@0: * "Best" means a Web URL with a scheme, and failing that, a Web URL without a michael@0: * scheme. michael@0: * michael@0: * @return a Web URL or null. michael@0: */ michael@0: public String bestWebURL() { michael@0: String firstWebURLWithScheme = firstWebURLWithScheme(); michael@0: if (firstWebURLWithScheme != null) { michael@0: return firstWebURLWithScheme; michael@0: } michael@0: michael@0: return firstWebURLWithoutScheme(); michael@0: } michael@0: michael@0: protected static List candidateWebURLs(Collection strings) { michael@0: List candidates = new ArrayList(); michael@0: michael@0: for (String string : strings) { michael@0: if (string == null) { michael@0: continue; michael@0: } michael@0: michael@0: candidates.addAll(candidateWebURLs(string)); michael@0: } michael@0: michael@0: return candidates; michael@0: } michael@0: michael@0: protected static List candidateWebURLs(String string) { michael@0: Matcher matcher = Patterns.WEB_URL.matcher(string); michael@0: List matches = new LinkedList(); michael@0: michael@0: while (matcher.find()) { michael@0: // Remove URLs with bad schemes. michael@0: if (!isWebURL(matcher.group())) { michael@0: continue; michael@0: } michael@0: michael@0: // Remove parts of email addresses. michael@0: if (matcher.start() > 0 && (string.charAt(matcher.start() - 1) == '@')) { michael@0: continue; michael@0: } michael@0: michael@0: matches.add(matcher.group()); michael@0: } michael@0: michael@0: return matches; michael@0: } michael@0: michael@0: protected String firstWebURLWithScheme() { michael@0: for (String match : candidates) { michael@0: try { michael@0: if (new URI(match).getScheme() != null) { michael@0: return match; michael@0: } michael@0: } catch (URISyntaxException e) { michael@0: // Ignore: on to the next. michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: protected String firstWebURLWithoutScheme() { michael@0: if (!candidates.isEmpty()) { michael@0: return candidates.get(0); michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: }