Java URL from makeURL(String title, String urlString)

Here you can find the source of makeURL(String title, String urlString)

Description

Creates a URL from user input.

License

Open Source License

Parameter

Parameter Description
title title of the url being generated. (for debugging purposes only) ie. source or target
urlString The user input

Exception

Parameter Description

Return

A URL object representing the location of urlString

Declaration

public static URL makeURL(String title, String urlString) throws MalformedURLException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**/*from  w  w w .j a va  2  s.c  om*/
     * Creates a URL from user input. If no protocol is specified, This method will attempt to
     * create a file: url with the given urlString.
     *
     * @param title
     *            title of the url being generated. (for debugging purposes only) ie. source or
     *            target
     * @param urlString
     *            The user input
     * @return A URL object representing the location of urlString
     * @throws java.net.MalformedURLException
     *             if the user data is not a valid URL
     */
    public static URL makeURL(String title, String urlString) throws MalformedURLException {
        if (urlString == null) {
            return null;
        }
        if (urlString.trim().length() < 1) {
            return null;
        }

        URL url = null;
        int i = -1;
        try {
            i = urlString.indexOf(':');
            if (i < 0) {
                urlString = "file:" + urlString;
            }

            url = new URL(urlString);

        } catch (MalformedURLException e) {
            if (i >= 0) {
                try {
                    urlString = "file:" + urlString;
                    url = new URL(urlString);
                } catch (MalformedURLException e2a) {
                    MalformedURLException e2;
                    e2 = new MalformedURLException(title + ":  Malformed URL `" + urlString + "`");
                    e2.initCause(e);
                    throw e2;
                }
            }
        }

        // Handle the case where there is a query string or anchor in the URL
        // ie http://www.google.com#searchbar
        // http://www.google.com?search=apache
        if (url != null) {
            try {
                URI uri = url.toURI();
                if (uri.isOpaque()) {
                    String currentDir = System.getProperty("user.dir");

                    String path2 = currentDir + System.getProperty("file.separator") + uri.getSchemeSpecificPart();
                    uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path2,
                            uri.getQuery(), uri.getFragment());
                    uri = uri.normalize();
                    url = uri.toURL();
                }
            } catch (URISyntaxException e) {
                // do nothing
            }
        }

        return url;
    }
}

Related

  1. makeUrl(String base, String suffix)
  2. makeURL(String id)
  3. makeURL(String location)
  4. makeUrl(String url)
  5. makeUrl(String url)
  6. makeURL(String url)
  7. makeURL(String urlstr)