Java URI to Parent URI getParentUriPath(String uriPath)

Here you can find the source of getParentUriPath(String uriPath)

Description

Calculate the parent URI path of the given URI path.

License

CDDL license

Parameter

Parameter Description
uriPath the uriPath (as returned by java.net.URI#getPath()

Return

the parent URI path of the given URI path

Declaration

public static String getParentUriPath(String uriPath) 

Method Source Code

//package com.java2s;
/*//from   w w w.  java 2s .c  o  m
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://jwsdp.dev.java.net/CDDLv1.0.html
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
 * add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your
 * own identifying information: Portions Copyright [yyyy]
 * [name of copyright owner]
 */

public class Main {
    /**
     * Calculate the parent URI path of the given URI path.
     *
     * @param uriPath the uriPath (as returned by java.net.URI#getPath()
     * @return the parent URI path of the given URI path
     */
    public static String getParentUriPath(String uriPath) {
        int idx = uriPath.lastIndexOf('/');

        if (uriPath.endsWith("/")) {
            uriPath = uriPath.substring(0, idx); // trim trailing slash
            idx = uriPath.lastIndexOf('/'); // move idx to parent context
        }

        return uriPath.substring(0, idx) + "/";
    }
}

Related

  1. getParentName(URI uri)
  2. getParents(final URI uri)
  3. getParentURI(URI uri)
  4. getParentURI(URI uri)
  5. getParentURI(URI uri)