Java URL Value Check isValidURL(String urlStr)

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

Description

is Valid URL

License

Open Source License

Declaration

public static final boolean isValidURL(String urlStr) 

Method Source Code

//package com.java2s;
/**//from  w  w w. ja  v  a 2 s  .  co m
 * This file is part of the Gribbit Web Framework.
 * 
 *     https://github.com/lukehutch/gribbit
 * 
 * @author Luke Hutchison
 * 
 * --
 * 
 * @license Apache 2.0 
 * 
 * Copyright 2015 Luke Hutchison
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static final boolean isValidURL(String urlStr) {
        return parseURI(urlStr) != null;
    }

    /**
     * Run a URI through the Java URI parser class to validate the URI.
     * 
     * @return the parsed URI, or null if the URI is invalid.
     */
    public static final URI parseURI(String uriStr) {
        // Double-check for XSS-unsafe characters in URIs. Most of these (except for single quote) are
        // caught by the URI parser, but just to be safe we also manually check here.
        for (int i = 0; i < uriStr.length(); i++) {
            char c = uriStr.charAt(i);
            if (c < (char) 33 || c > (char) 126 || c == '<' || c == '>' || c == '\'' || c == '"' || c == '\\') {
                return null;
            }
        }
        try {
            // Returns new URI object if URI parses OK
            return new URI(uriStr);
        } catch (URISyntaxException e) {
            return null;
        }
    }
}

Related

  1. isValidUrl(String url)
  2. isValidURL(String url)
  3. isValidURL(String url)
  4. isValidURL(String URL_String)
  5. isValidURL(String urlStr)
  6. isValidURL(String urlString)
  7. isValidUrl(URI uri)
  8. isValidURL(URL url)
  9. isValidUrlDataType(URL value)