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

Return

a parent uri or null if there is none

Declaration

public static URI getParentURI(URI uri) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2013 CWI//from   www  .  j a  v a2 s.c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   * Davy Landman  - Davy.Landman@cwi.nl
*******************************************************************************/

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**
     * @return a parent uri or null if there is none
     */
    public static URI getParentURI(URI uri) {
        File file = new File(uri.getPath());
        File parent = file.getParentFile();

        if (parent != null && !parent.getName().isEmpty()) {
            try {
                return changePath(uri, parent.getAbsolutePath());
            } catch (URISyntaxException e) {
                // can not happen
            }
        }

        return null; // there is no parent;
    }

    public static URI changePath(URI uri, String newPath) throws URISyntaxException {
        return create(uri.getScheme(), getCorrectAuthority(uri), newPath, uri.getQuery(), uri.getFragment());
    }

    /**
     * Create a new URI, non-encoded input is assumed.
     * @throws URISyntaxException
     */
    public static URI create(String scheme, String authority, String path, String query, String fragment)
            throws URISyntaxException {
        return fixUnicode(new URI(scheme, authority, path, query, fragment));
    }

    /**
     * Create a new URI, non-encoded input is assumed.
     * This is a shorthand for common cases were the query and fragment part are empty.
     * @throws URISyntaxException
     */
    public static URI create(String scheme, String authority, String path) throws URISyntaxException {
        return create(scheme, authority, path, null, null);
    }

    /**
     * Create a new URI, non-encoded input is assumed.
     * This is a version in case of a scheme which has a server-based authority part.
     * And thus allows to set user information, host, and port.
     * @throws URISyntaxException
     */
    public static URI create(String scheme, String userInformation, String host, int port, String path,
            String query, String fragment) throws URISyntaxException {
        return fixUnicode(new URI(scheme, userInformation, host, port, path, query, fragment));
    }

    private static String getCorrectAuthority(URI uri) {
        if (uri.getAuthority() == null)
            return "";
        return uri.getAuthority();
    }

    /**
     * In case you want to use an external URI not created by this class, call this method to ensure RFC compliant unicode support.
     * @throws URISyntaxException
     */
    public static URI fixUnicode(URI uri) throws URISyntaxException {
        return new URI(uri.toASCIIString());
    }
}

Related

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