Java URL Resolve resolveURL(URL contextURL, String url)

Here you can find the source of resolveURL(URL contextURL, String url)

Description

resolve URL

License

Open Source License

Declaration

public static URL resolveURL(URL contextURL, String url) 

Method Source Code

//package com.java2s;
/**/*from www. j  ava  2  s.  c  o m*/
 * Copyright (c) 2006-2010, Cloudsmith Inc.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the copyright holder
 * listed above, as the Initial Contributor under such license. The text of
 * such license is available at www.eclipse.org.
 */

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

import java.net.URL;

public class Main {
    public static URL resolveURL(URL contextURL, String url) {
        if (url == null)
            return null;

        try {
            if (contextURL == null)
                return normalizeToURL(url);
            return new URL(contextURL, url);
        } catch (MalformedURLException e) {
            return null;
        }
    }

    public static URL normalizeToURL(String surl)
            throws MalformedURLException {
        if (surl == null)
            return null;

        try {
            return new URL(surl);
        } catch (MalformedURLException e) {
            // Do a space check.
            //
            if (surl.indexOf(' ') > 0) {
                try {
                    return new URL(surl.replaceAll("\\s", "%20")); //$NON-NLS-1$ //$NON-NLS-2$
                } catch (MalformedURLException me1) {
                }
            }

            try {
                return new File(surl).toURI().toURL();
            } catch (MalformedURLException me2) {
                // Throw the original exception
                //
                throw e;
            }
        }
    }
}

Related

  1. resolveResourceURL(Class base, String resource)
  2. resolveRootUrl(Class knownClass)
  3. resolveUrl(String url, Properties properties)
  4. resolveURL(String urlValue)
  5. resolveURL(URL base, String target)
  6. resolveURL(URL url)
  7. resolveWsURI(String url)
  8. resolveXmlConfigUrl(@Nullable URL defaultXmlConfigUrl)
  9. reverseUrl(String urlString)