Java URL Value Check isValid(String url)

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

Description

is Valid

License

Open Source License

Declaration

public static boolean isValid(String url) 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2012-2016 Red Hat, Inc. 
 * Distributed under license by Red Hat, Inc. All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * //  w w  w.ja v  a  2s  .c o  m
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 ******************************************************************************/

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern SIMPLE_URL_PATTERN = Pattern
            .compile("(\\w+://)(.+@)*([\\w\\d\\.]+)(:[\\d]+){0,1}/*(.*)");

    public static boolean isValid(String url) {
        // Test via regex first. If passes then check via new URL(url) and URI(url) which are slower
        if (SIMPLE_URL_PATTERN.matcher(url).matches()) {
            try {
                new URI(url);
                new URL(url);
            } catch (MalformedURLException | URISyntaxException e) {
                return false;
            }
            return true;
        }
        return false;
    }
}

Related

  1. isUrlInJar(URL url)
  2. isUrlResponding(String url)
  3. isUrlValid(final URL url)
  4. isUrlValid(URL url)
  5. isValid(String candidateUrl)
  6. isValidChannelUrl(String url)
  7. isValidServerURL(String serverURL)
  8. isValidSuccessfulSignInURL(String url)
  9. isValidURL(CharSequence urlStr)