Java URL Create buildURL(String url)

Here you can find the source of buildURL(String url)

Description

Construct the URL based on the String

License

Open Source License

Parameter

Parameter Description
url a parameter

Exception

Parameter Description
MalformedURLException an exception

Declaration

public static URL buildURL(String url) throws MalformedURLException 

Method Source Code

//package com.java2s;
/*/*w  w  w  . java2 s  .  c  o m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import java.io.File;

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

public class Main {
    /**
     * Construct the URL based on the String
     * 
     * @param url
     * @return
     * @throws MalformedURLException
     * @since 4.4
     */
    public static URL buildURL(String url) throws MalformedURLException {

        if (url == null)
            throw new MalformedURLException();

        url = convertBackSlashes(url);

        final String filename = extractFileName(url);
        if (filename != null) {
            return new File(url).toURI().toURL();
        }
        return new URL(url);
    }

    public static URL buildURL(final URL url) {
        try {
            return buildURL(url.toExternalForm());
        } catch (final MalformedURLException e) {
            // since it came as url it should not have any issues with this
        }
        return null;
    }

    static String convertBackSlashes(final String str) {
        return str.replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    static String extractFileName(String file) {

        if (file.matches("^(\\w){2,}:.*")) // Handles URLs - No conversion necessary //$NON-NLS-1$
            // http://lib/foo.txt - currently do not support, converts to local
            // host with absolute path
            // file://lib/foo.txt
            // file:///c:/lib/foo.txt
            return null;
        else if (file.matches("^\\/.*")) // Handles absolute paths- it can be file or URL depending upon //$NON-NLS-1$
            // context Conversion needed
            // /lib/foo.txt
            return file;
        else if (file.matches("^\\w:[\\\\,\\/].*")) { //$NON-NLS-1$
            // Handles windows absolute path - no conversion needed
            // c:\\lib\\foo.txt
            // c:/lib.foo.txt
            file = file.replaceAll("\\\\", "\\/"); //$NON-NLS-1$ //$NON-NLS-2$
            return "/" + file; //$NON-NLS-1$
        } else if (file.matches("^(\\.)+\\/.*|^\\w+\\/.*|^\\w+.*")) // Handles relative paths - these can be URLs or files - //$NON-NLS-1$
            // conversion necessary
            // ./lib/foo.txt
            // ../lib/foo.txt
            // lib/foo.txt
            return file;
        return null;
    }
}

Related

  1. buildURL(String spec)
  2. buildURL(String spec)
  3. buildURL(String spec, boolean trailingSlash)
  4. buildURL(String url)
  5. buildUrl(String url)
  6. buildUrl(String url, HashMap params)
  7. buildUrl(String URL, Map params)
  8. buildUrl(String urlPrefix, String urlSuffix)
  9. buildURL(URI base, Multimap params)