Java URI from toURI(URI parent, String child)

Here you can find the source of toURI(URI parent, String child)

Description

Convenient method to simulate a parent/child composition

License

Open Source License

Parameter

Parameter Description
parent the URI to parent directory
child the child name

Return

the resulting URI

Declaration

public static URI toURI(URI parent, String child) 

Method Source Code

//package com.java2s;
//  This software is released under the GNU General Public License.           //

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**/*from w  ww. j ava2s.  com*/
     * Convenient method to simulate a parent/child composition
     *
     * @param parent the URI to parent directory
     * @param child  the child name
     * @return the resulting URI
     */
    public static URI toURI(URI parent, String child) {
        try {
            // Make sure parent ends with a '/'
            if (parent == null) {
                throw new IllegalArgumentException("Parent is null");
            }

            StringBuilder dirName = new StringBuilder(parent.toString());

            if (dirName.charAt(dirName.length() - 1) != '/') {
                dirName.append('/');
            }

            // Make sure child does not start with a '/'
            if ((child == null) || child.isEmpty()) {
                throw new IllegalArgumentException("Child is null or empty");
            }

            if (child.startsWith("/")) {
                throw new IllegalArgumentException("Child is absolute: " + child);
            }

            return new URI(dirName.append(child).toString());
        } catch (URISyntaxException ex) {
            throw new IllegalArgumentException(ex.getMessage(), ex);
        }
    }
}

Related

  1. toURI(String str)
  2. toURI(String sUri)
  3. toURI(String uri)
  4. toURI(String uri)
  5. toUri(String uri)
  6. toURIArray(String[] array)
  7. toURIList(Collection stringList)
  8. toURIObject(String uri)
  9. toURIs(File... files)