Java URI Value Check isAbsoluteUri(String uriAsString)

Here you can find the source of isAbsoluteUri(String uriAsString)

Description

Determines whether a string is an absolute URI.

License

Open Source License

Parameter

Parameter Description
uriAsString the potential URI, as a string

Return

true if it is an absolute URI, false otherwise or if it is not a valid URI

Declaration

public static boolean isAbsoluteUri(String uriAsString) 

Method Source Code

//package com.java2s;
/****************************************************************************
 *
 * Copyright (c) 2008-2011, EBM WebSourcing
 *
 * This source code is available under agreement available at
 * http://www.petalslink.com/legal/licenses/petals-studio
 *
 * You should have received a copy of the agreement along with this program.
 * If not, write to EBM WebSourcing (4, rue Amelie - 31200 Toulouse, France).
 *
 *****************************************************************************/

import java.net.URI;

import java.net.URL;

public class Main {
    /**//w  ww. jav a2 s  . c  om
     * Determines whether a string is an absolute URI.
     * @param uriAsString the potential URI, as a string
     * @return true if it is an absolute URI, false otherwise or if it is not a valid URI
     */
    public static boolean isAbsoluteUri(String uriAsString) {

        boolean result;
        try {
            URI uri = urlToUri(uriAsString);
            result = uri.isAbsolute();

        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * Builds an URI from an URL (with an handle for URLs not compliant with RFC 2396).
     * @param url
     * @return
     */
    public static URI urlToUri(URL url) {

        URI uri;
        try {
            // Possible failing step.
            uri = url.toURI();

        } catch (Exception e) {
            // URL did not comply with RFC 2396 => illegal un-escaped characters.
            try {
                uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                        url.getQuery(), url.getRef());
            } catch (Exception e1) {
                // No automatic repair.
                throw new IllegalArgumentException("Broken URL: " + url);
            }
        }

        uri = uri.normalize();
        return uri;
    }

    /**
     * Builds an URI from an URL string (with an handle for URLs not compliant with RFC 2396).
     * @param urlAsString
     * @return
     */
    public static URI urlToUri(String urlAsString) {

        try {
            URL url = new URL(urlAsString);
            return urlToUri(url);

        } catch (Exception e) {
            // e.printStackTrace();
        }

        throw new IllegalArgumentException("Broken URL: " + urlAsString);
    }
}

Related

  1. isAbsoluteURI(String path)
  2. isAbsoluteURI(String uri)
  3. isAnyAddress(final URI u)
  4. isAvailable(URI address)
  5. isBagUri(final URI uri)
  6. isBase(URI base, URI uri)