Java URI to Parent URI getParentURI(URI uri)

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

Description

get Parent URI

License

Open Source License

Declaration

public static URI getParentURI(URI uri) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Copyright (c) 2006-2013, 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.net.URI;
import java.net.URISyntaxException;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class Main {
    public static URI getParentURI(URI uri) {
        if (uri == null)
            return uri;

        IPath uriPath = Path.fromPortableString(uri.getPath());
        if (uriPath.segmentCount() == 0)
            return null;

        uriPath = uriPath.removeLastSegments(1).addTrailingSeparator();
        try {//from w w w . j a va2s.  c  om
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                    uriPath.toPortableString(), uri.getQuery(), uri.getFragment());
        } catch (URISyntaxException e) {
            // Shouldn't happen since we started with a valid URI
            //
            return null;
        }
    }
}

Related

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