Java Utililty Methods URI to Relative URI

List of utility methods to do URI to Relative URI

Description

The list of methods to do URI to Relative URI are organized into topic(s).

Method

URIgetRedirectUri(URI uri, String location)
get Redirect Uri
URI newUri = uri.resolve(location);
String scheme = newUri.getScheme();
if (scheme == null || !scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
    throw new IllegalArgumentException("The URI scheme, of the URI " + newUri
            + ", must be equal (ignoring case) to 'http' or 'https'");
return newUri;
URIgetRelativeLocalURI(String suffix)
Now works for both linux and windows
return URI.create("file:///" + getCurrentDir().replace("\\", "/") + suffix);
StringgetRelativeName(File file, URI directory)
get Relative Name
URI fUri = file.toURI();
return directory.relativize(fUri).getPath();
StringgetRelativePath(URI base, File file)
Return the path for the file relative to the base uri.
return toString(getRelativeURI(base, file.toURI()));
URIgetRelativePath(URI targetURI, URI baseURI)
Returns a relative path between basePath and targetPath if possible.
if (!targetURI.isAbsolute() || !baseURI.isAbsolute() || targetURI.isOpaque() || baseURI.isOpaque())
    return targetURI;
if (!targetURI.getScheme().equals(baseURI.getScheme()))
    return targetURI;
String basePath = baseURI.getPath().substring(1);
String targetPath = targetURI.getPath().substring(1);
String[] base = basePath.split("/", -1);
String[] target = targetPath.split("/", 0);
...
StringgetRelativePath(URI targetUri, URI baseUri)
Finds the relative path from the baseUri to the target URI.
String pathSeparator = "/";
String targetPath = targetUri.getPath();
String[] base;
String[] target;
if (baseUri.toString().startsWith("jar:")) {
    try {
        JarURLConnection conn = (JarURLConnection) (baseUri.toURL().openConnection());
        String entryName = conn.getEntryName();
...
StringgetRelativeURI(String href)
Returns the relative URI of the href argument
if (href == null)
    return href;
int idx = -1;
try {
    new URL(href);
    idx = href.lastIndexOf(HREF_PATH_SEP);
} catch (MalformedURLException muex) {
    int idx2 = href.lastIndexOf(HREF_PATH_SEP);
...
URIgetRelativeURI(URI base, URI uri)
Return the uri for the uri relative to the base uri.
if (base != null && uri != null && (uri.getScheme() == null || uri.getScheme().equals(base.getScheme()))) {
    StringTokenizer baseParts = tokenizeBase(base);
    StringTokenizer uriParts = new StringTokenizer(uri.getSchemeSpecificPart(), "/");
    StringBuffer relativePath = new StringBuffer();
    String part = null;
    boolean first = true;
    if (!baseParts.hasMoreTokens()) {
        return uri;
...
URIgetRelativeURI(URI repositoryXml, URI bundleJar)
get Relative URI
try {
    String repositoryPath = repositoryXml.getPath();
    if (repositoryPath.toLowerCase().endsWith(DOT_XML)) {
        int dirnameIndex = repositoryPath.lastIndexOf('/');
        repositoryPath = repositoryPath.substring(0, dirnameIndex);
    URI rootURI = new URI(null, repositoryPath, null);
    URI localURI = new URI(null, bundleJar.getPath(), null);
...
URIgetRelativeUri(URI rootURI, File file)
get Relative Uri
if (file == null) {
    return null;
URI uri = file.toURI();
if (rootURI == null) {
    return uri;
return rootURI.relativize(uri);
...