Java URI Value Check validateUri(String uri, String name)

Here you can find the source of validateUri(String uri, String name)

Description

Does input validation for uri parameters.

License

Apache License

Parameter

Parameter Description
uri the uri string to validate
name the name of this uri for use in error messages

Exception

Parameter Description
NullPointerException if the uri string is null
IllegalArgumentException if the uri is not a valid absolute URI

Return

Returns the uri

Declaration

public static URI validateUri(String uri, String name) 

Method Source Code

//package com.java2s;
/*//from ww w  . j av a 2s . c o  m
 * $HeadURL$
 * $Id$
 *
 * Copyright (c) 2006-2011 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * 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 {
    /**
     * Does input validation for uri parameters. Only absolute (non-relative) URIs are valid.
     * <p/>
     * In usage, it essentailly asserts that a URI string is a valid URI and throws a subclass of RuntimeException if
     * not.
     * <p/>
     * As a helpful side-effect, this function also returns the uri as a proper java.net.URI that can be used for further
     * processing.
     *
     * @param uri  the uri string to validate
     * @param name the name of this uri for use in error messages
     * @return Returns the uri
     * @throws NullPointerException     if the uri string is null
     * @throws IllegalArgumentException if the uri is not a valid absolute URI
     */
    public static URI validateUri(String uri, String name) {
        if (uri == null)
            throw new NullPointerException("'" + name + "' cannot be null");

        try {
            URI u = new URI(uri);

            if (!u.isAbsolute())
                throw new URISyntaxException(uri, "missing scheme component", 0);

            return u;
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("'" + name + "' must be a valid absolute URI", e);
        }
    }
}

Related

  1. isWellFormedUriString(final String uriString)
  2. validateURI(final String uri)
  3. validateUri(String uri)
  4. validateURI(String uri)
  5. validateUri(String uri)