Get Parent URI : URI « Network Protocol « Java

Get Parent URI

  Milyn - Copyright (C) 2006

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License (version 2.1) as published by the Free Software

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of

  See the GNU Lesser General Public License for more details:


 * {@link} utility methods.
 * @author <a href=""></a>
public abstract class URIUtil {

     * Get the parent URI of the supplied URI
     * @param uri The input URI for which the parent URI is being requrested.
     * @return The parent URI.  Returns a URI instance equivalent to "../" if
     * the supplied URI path has no parent.
     * @throws URISyntaxException Failed to reconstruct the parent URI.
    public static URI getParent(URI uri) throws URISyntaxException {

        String parentPath = new File(uri.getPath()).getParent();

        if(parentPath == null) {
            return new URI("../");

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


Related examples in the same category

1.Encode a URI path
2.Add two URI path segments
3.Remove parameters from a uri.
4.Remove any authorisation details from a URI
5.A class to represent a Uniform Resource Identifier (URI).
6.Utility to convert File to URL.
7.Return True if the uri has a scheme
8.Normalize a uri containing ../ and ./ paths.
9.URI Support
10.URI utilities
11.Create and resolve URI
12.Add parameters stored in the Map to the uri string.
13.Make a URL from the given string
14.Is Equal URI
15.Resolves the specified URI, and returns the file represented by the URI.
16.Returns the filename for the specified URI.