Java tutorial
//package com.java2s; import java.net.MalformedURLException; public class Main { public static String COLON = ":"; public static String SINGLE_FORWARD_SLASH = "/"; public static String DOUBLE_FORWARD_SLASH = "//"; public static String SELENIUM_PROTOCOL = "http"; protected static String getProtocol(final String url) throws MalformedURLException { String retVal = null; if (url != null) { final String[] urlArray = url.split(COLON); if (urlArray.length == 2 || urlArray.length == 3) { if (urlArray[0].equals(SELENIUM_PROTOCOL)) { retVal = SELENIUM_PROTOCOL; } else { final String[] portPlusUriArray = splitPortUri(urlArray[1]); if (urlArray[1].contains(DOUBLE_FORWARD_SLASH)) { // second element is a host, which means protocol is not supported throw new MalformedURLException("Unsupported protocol: " + urlArray[0]); } else if (isInteger(portPlusUriArray[0])) { // second element is a port, set protocol to default retVal = SELENIUM_PROTOCOL; } else { // second element is not a port, which means the first element is // not // a host, and // is also not a supported protocol. throw new MalformedURLException("Unsupported protocol: " + urlArray[0]); } } } else if (urlArray.length == 1) { retVal = SELENIUM_PROTOCOL; } else if (urlArray.length > 3) { throw new MalformedURLException("Unexpected number of colons within URL."); } } else { retVal = SELENIUM_PROTOCOL; } return retVal; } protected static String[] splitPortUri(final String portPlusUri) { final String[] stringArray = portPlusUri.split(SINGLE_FORWARD_SLASH); final StringBuilder uri = new StringBuilder(SINGLE_FORWARD_SLASH); final String port = stringArray[0]; for (int i = 0; i < stringArray.length; i++) { if (i > 0) { uri.append(stringArray[i]); } } return new String[] { port, uri.toString() }; } protected static boolean isInteger(final String port) { boolean retVal = true; try { Integer.valueOf(port); } catch (final NumberFormatException e) { retVal = false; } return retVal; } }