Java URL Create createUrl(final String spec)

Here you can find the source of createUrl(final String spec)

Description

Creates a new URL from the specified URL spec.

License

Apache License

Parameter

Parameter Description
spec URL spec to create a URL from

Exception

Parameter Description
IllegalArgumentException if the specified URL spec is a malformed URL

Return

a new from the specified URL spec

Declaration

public static URL createUrl(final String spec) throws IllegalArgumentException 

Method Source Code

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

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**//from  www .  j ava2  s.com
     * Creates a new {@link URL} from the specified URL spec.
     * 
     * <p>
     * Primary goal of this factory method is to suppress the {@link MalformedURLException} that comes with the {@link URL}'s constructor, it is thrown in a
     * "shadowed" manner, wrapped in an {@link IllegalArgumentException} (which is a {@link RuntimeException}).
     * </p>
     * 
     * @param spec URL spec to create a {@link URL} from
     * @return a new {@link URL} from the specified URL spec
     * @throws IllegalArgumentException if the specified URL spec is a malformed URL
     * 
     * @see #createUrl(URL, String)
     * @see URL#URL(String)
     */
    public static URL createUrl(final String spec) throws IllegalArgumentException {
        return createUrl(null, spec);
    }

    /**
     * Creates a new {@link URL} from the specified URL context and spec.
     * 
     * <p>
     * Primary goal of this factory method is to suppress the {@link MalformedURLException} that comes with the {@link URL}'s constructor, it is thrown in a
     * "shadowed" manner, wrapped in an {@link IllegalArgumentException} (which is a {@link RuntimeException}).
     * </p>
     * 
     * @param context URL context in which to interpret the URL spec
     * @param spec URL spec to create a {@link URL} from
     * @return a new {@link URL} from the specified URL spec
     * @throws IllegalArgumentException if the specified URL spec is a malformed URL
     * 
     * @see #createUrl(String)
     * @see URL#URL(URL, String)
     */
    public static URL createUrl(final URL context, final String spec) throws IllegalArgumentException {
        try {
            return new URL(context, spec);
        } catch (final MalformedURLException mue) {
            throw new RuntimeException("Malformed URL: " + spec, mue);
        }
    }
}

Related

  1. convertToURL(String url)
  2. convertToUrl(String[] paths)
  3. convertToURLs(String[] hosts)
  4. createURL(final String address)
  5. createURL(final String protocol, final String userInfo, final String host, final int port, final String path, final String query, final String ref)
  6. createURL(final String url)
  7. createURL(String fileName)
  8. createUrl(String spec)
  9. createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)