Java URL from makeURL(String location)

Here you can find the source of makeURL(String location)

Description

Obtains a URL from a string.

License

LGPL

Parameter

Parameter Description
location a string representing the location of a resource

Return

a URL representing the location of the resource

Declaration

public static URL makeURL(String location) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class Main {
    private static URL defaultContext;

    /**// www  . j  av  a2 s  . c  om
     * Obtains a URL from a string.  If the String has the form of a URL,
     * it is turned directly into a URL.  If it does not, it is treated as
     * a filename, and turned into a file-protocol URL.  In the latter
     * case a relative or absolute filename may be used.  If it is 
     * null or a blank string (or something else equally un-filename like?)
     * then null is returned.
     *
     * @param   location   a string representing the location of a resource
     * @return  a URL representing the location of the resource
     */
    public static URL makeURL(String location) {
        if (location == null || location.trim().length() == 0) {
            return null;
        }
        try {
            return new URL(location);
        } catch (MalformedURLException e) {
            try {
                URI uri = new File(location).toURI();
                return uri.toURL();
                //return new URL( uri.toString() );
            } catch (MalformedURLException e2) {
                throw protestFileProtocolIsLegal(e2);
            }
        } catch (SecurityException e) {
            try {
                return new URL("file:" + location);
            } catch (MalformedURLException e2) {
                throw protestFileProtocolIsLegal(e2);
            }
        }
    }

    /**
     * Obtains a URL from a string in a given context.
     * The string <tt>context</tt> is turned into a URL as per 
     * the {@link #makeURL(String)} method, unless it is null or
     * the empty string, in which case it is treated as a reference
     * to the current directory.
     * The string <tt>location</tt> is then turned into a URL in
     * the same way as using {@link #makeURL(String)}, except that
     * if it represents a relative path it is resolved in the context
     * of <tt>context</tt>, taking its protocol and/or relative position
     * from it.
     *
     * @param   context   a string representing the context within which
     *                    <tt>location</tt> is to be resolved
     * @param   location   a string representing the location of a resource
     * @return  a URL representing the location of the resource
     */
    public static URL makeURL(String context, String location) {
        URL contextURL;
        if (context == null || context.trim().length() == 0) {
            contextURL = defaultContext;
        } else {
            contextURL = makeURL(context);
        }
        try {
            return new URL(contextURL, location);
        } catch (MalformedURLException e) {
            try {
                return new URL(contextURL, makeURL(location).toString());
            } catch (MalformedURLException e2) {
                // can this happen??
                return makeURL(location);
            }
        }
    }

    /**
     * Returns an Error which can be thrown when you can't make a URL even
     * though you know you're using the "file:" protocol.  Although this
     * is permitted by the URL class, we consider ourselves to be on
     * an irretrievably broken system if it happens.
     */
    private static AssertionError protestFileProtocolIsLegal(MalformedURLException e) {
        AssertionError ae = new AssertionError("Illegal \"file:\" protocol in URL??");
        ae.initCause(e);
        return ae;
    }
}

Related

  1. makeUrl(String base, String suffix)
  2. makeURL(String id)
  3. makeURL(String title, String urlString)
  4. makeUrl(String url)
  5. makeUrl(String url)
  6. makeURL(String url)