Java Path to URL getUrl(URL base, String path)

Here you can find the source of getUrl(URL base, String path)

Description

Attempts to return a valid URL from either an absolute 'path' or a relative 'path' combined with 'base'.

License

Open Source License

Parameter

Parameter Description
base An optional URL which is used with a relative path parameter.
path Either an absolute path or a relative path together with the base parameter.

Exception

Parameter Description
MalformedURLException an exception

Return

A valid URL or null in case of an error.

Declaration

public static URL getUrl(URL base, String path) throws MalformedURLException 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

import java.net.MalformedURLException;

import java.net.URL;

public class Main {
    /**// w  w w.j a v  a2 s.  c  o  m
     * Attempts to return a valid URL from either an absolute 'path' or a relative 'path'
     * combined with 'base'.
     * @param base An optional URL which is used with a relative path parameter.
     * @param path Either an absolute path or a relative path together with the base parameter.
     * @return A valid URL or {@code null} in case of an error.
     * @throws MalformedURLException
     */
    public static URL getUrl(URL base, String path) throws MalformedURLException {
        URL retVal = null;
        if (path != null) {
            try {
                // try absolute url first
                retVal = new URL(path);
            } catch (MalformedURLException mue) {
                retVal = null;
            }

            if (retVal == null && base != null) {
                // try relative url
                String baseUrl = base.toExternalForm();
                int idx = baseUrl.indexOf('?');
                String basePath = (idx >= 0) ? baseUrl.substring(0, idx) : baseUrl;
                String suffix = (idx >= 0) ? baseUrl.substring(idx) : "";
                if (basePath.contains(path)) {
                    retVal = new URL(basePath + suffix);
                } else {
                    int cnt = 0;
                    if (basePath.charAt(basePath.length() - 1) == '/') {
                        cnt++;
                    }
                    if (path.charAt(0) == '/') {
                        cnt++;
                    }
                    if (cnt == 2) {
                        path = path.substring(1);
                    } else if (cnt == 0) {
                        path = '/' + path;
                    }
                    retVal = new URL(basePath + path + suffix);
                }
            }
        }
        return retVal;
    }
}

Related

  1. getURL(String path)
  2. getURL(String path)
  3. getURL(String path, URL context)
  4. getUrl(String pluginId, String relativePath)
  5. getURL(String schema, String host, String path)
  6. getUrlForLocalPath(String path)
  7. getURLFromClassPath(String source)
  8. getURLFromPath(String jarPath)
  9. getUrlFromPath(String path)