Java URL Value Check isValidURL(String url)

Here you can find the source of isValidURL(String url)

Description

is Valid URL

License

Open Source License

Declaration

public static boolean isValidURL(String url) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.MalformedURLException;

public class Main {
    public static boolean isValidURL(String url) {
        if (url.length() < 10)
            return false;
        url = url.trim();//  w  w w  .  j  ava  2s .  com

        if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://")) {
            String host = null;
            try {
                host = getHostName(url);
            } catch (MalformedURLException e) {
                return false;
            }

            if (!isValidHostString(host))
                return false;
            if (host.contains(".."))
                return false;
        } else {//unknown schema
            return false;
        }

        //it seems right
        return true;
    }

    public static String getHostName(String url) throws MalformedURLException {
        if (url == null || url.length() == 0)
            return "";
        url = url.toLowerCase();
        if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("ftp://"))
            throw new MalformedURLException("Unknown schema!");

        int doubleslash = url.indexOf("//");
        doubleslash += 2;

        int end = url.length();

        int idx = url.indexOf('/', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf(':', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('&', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('=', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('#', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('?', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        return url.substring(doubleslash, end);
    }

    static public final boolean isValidHostString(String str) {
        str = str.trim();
        for (int i = 0; i < str.length(); i++)
            if (!isValidHostChar(str.charAt(i)))
                return false;
        if (str.startsWith("-") || str.endsWith("-"))
            return false;
        if (str.startsWith(".") || str.endsWith("."))
            return false;
        if (str.startsWith(":") || str.endsWith(":"))
            return false;

        int colon_count = 0;
        for (int i = 0; i < str.length(); i++)
            if (str.charAt(i) == ':')
                colon_count++;
        if (colon_count > 1)
            return false;

        if (str.endsWith("_"))
            return false;

        return true;
    }

    static private final boolean isValidHostChar(char ch) {
        return isAsciLetter(ch) || isAsciDigit(ch) || ch == '-' || ch == '.' || ch == '_' || ch == ':';
    }

    static private final boolean isAsciLetter(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }

    static private final boolean isAsciDigit(char ch) {
        return (ch >= '0' && ch <= '9');
    }
}

Related

  1. isValidURL(CharSequence urlStr)
  2. isValidURL(final String candidateString)
  3. isValidURL(String link)
  4. isValidURL(String url)
  5. isValidURL(String url)
  6. isValidUrl(String url)
  7. isValidURL(String URL_String)
  8. isValidURL(String urlStr)
  9. isValidURL(String urlStr)