Java URI to Parent URI getParent(URI uri)

Here you can find the source of getParent(URI uri)

Description

Gets the URI of the directory that contains the resource.

License

Common Public License

Parameter

Parameter Description
uri URI to get the parent of.

Exception

Parameter Description
URISyntaxException an exception
IOException an exception
MalformedURLException an exception

Return

URI of the parent. Note that the path always ends in "/".

Declaration

public static URI getParent(URI uri) throws URISyntaxException, MalformedURLException, IOException 

Method Source Code


//package com.java2s;
//License from project: Common Public License 

import java.io.File;
import java.io.IOException;

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

public class Main {
    /**//from   www  . j  a  v  a  2 s  . c  o  m
     * Gets the URI of the directory that contains the resource. 
     * @param uri URI to get the parent of.
     * @return URI of the parent. Note that the path always ends in "/".
     * @throws URISyntaxException
     * @throws IOException 
     * @throws MalformedURLException 
     */
    public static URI getParent(URI uri) throws URISyntaxException, MalformedURLException, IOException {

        if (uri.toString().startsWith("jar:")) {
            JarURLConnection jarConn = (JarURLConnection) (uri.toURL().openConnection());
            String entryName = jarConn.getEntryName();
            String parentPath = "";

            if (entryName != null) {
                parentPath = (new File(entryName)).getParent();
            }
            String jarUrlStr = "jar:" + jarConn.getJarFileURL().toExternalForm() + "!/";
            URL jarUrl = new URL(jarUrlStr);
            URL parentUrl = null;
            if (parentPath != null)
                parentUrl = new URL(jarUrl, parentPath + "/");
            else
                parentUrl = jarUrl;
            return parentUrl.toURI();

        } else {
            String parentPath = null;
            File baseFile = null;
            String path = uri.getPath();
            if (path != null) {
                baseFile = new File(path);
            } else {
                throw new RuntimeException("getPath() on URI \"" + uri + "\" returned null");
            }
            parentPath = baseFile.getParent();
            if (parentPath == null) {
                return new URI("../");
            }
            if (!"/".equals(parentPath)) {
                parentPath += "/";
            }

            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                    parentPath.replace('\\', '/'), uri.getQuery(), uri.getFragment());
        }

    }
}

Related

  1. getParams(URI uri)
  2. getParent(final URI uri)
  3. getParent(URI path)
  4. getParentName(URI uri)
  5. getParents(final URI uri)
  6. getParentURI(URI uri)
  7. getParentURI(URI uri)