Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.net.MalformedURLException;

public class Main {
    public static String COLON = ":";
    public static String SINGLE_FORWARD_SLASH = "/";
    public static String SELENIUM_HOST_DEFAULT = "127.0.0.1";
    public static String SELENIUM_LOCAL_PORT_DEFAULT = "4723";
    public static String SELENIUM_REMOTE_PORT_DEFAULT = "4444";

    protected static String getPort(final String url) throws MalformedURLException {
        String retPort;
        if (url != null) {
            final String[] urlArray = url.split(COLON);
            retPort = null;
            if (urlArray.length == 1) {
                retPort = getDefaultPort(urlArray[0]);
            } else if (urlArray.length == 2) {
                final String[] portPlusUriArray = splitPortUri(urlArray[1]);
                if (isInteger(portPlusUriArray[0])) {
                    retPort = portPlusUriArray[0];
                } else {
                    retPort = getDefaultPort(urlArray[1]);
                }
            } else if (urlArray.length == 3) {
                final String[] portPlusUriArray = splitPortUri(urlArray[2]);

                if (isInteger(portPlusUriArray[0])) {
                    retPort = portPlusUriArray[0];
                } else {
                    throw new MalformedURLException("Port invalid");
                }
            }
        } else {
            retPort = SELENIUM_LOCAL_PORT_DEFAULT;
        }
        return retPort;
    }

    protected static String getDefaultPort(String host) {
        String retPort = null;
        host = host.replaceAll("/", "");
        if (host.equals(SELENIUM_HOST_DEFAULT) || host.equals("localhost")) {
            retPort = SELENIUM_LOCAL_PORT_DEFAULT;
        } else {
            retPort = SELENIUM_REMOTE_PORT_DEFAULT;
        }
        return retPort;
    }

    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;
    }
}