Here you can find the source of isRoot(URI uri)
public static boolean isRoot(URI uri)
//package com.java2s; //License from project: Apache License import java.net.URI; import java.net.URISyntaxException; public class Main { public static boolean isRoot(URI uri) { try {//w ww . j av a 2s. com return parseParentURI(uri) == null ? true : false; } catch (Exception e) { throw new RuntimeException(); } } public static URI parseParentURI(final URI uri) { String path[] = getPathAsArray(uri); // handle root case if (path.length == 1) { return null; } // obtain leaf (end of flattened path) String leaf = path[path.length - 1]; int index = new StringBuilder(uri.getPath()).lastIndexOf(leaf); // remove leaf StringBuilder newPath = new StringBuilder(uri.getPath().substring(0, index)); URI newURI = createURI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), newPath.toString(), uri.getQuery(), uri.getFragment()); newURI = ensureNoTrailingSlash(newURI); return newURI; } public static String[] getPathAsArray(URI uri) { StringBuilder path = new StringBuilder(uri.getPath()); // we treat URI paths at lists of trees, where the first "node" // is a tree root... for this reason, a uri path of "/" is not // a root, but an empty path // guard against empty path (root) if (uri.getPath().equals("/") || uri.getPath().equals("\\")) { // do not want to return null, since that indicates // uri is a "root"... need to distinguish String back = "\"" + "\\" + "\""; String forward = "\"" + "/" + "\""; String ie = "(ie: " + back + " or " + forward + ")"; throw new RuntimeException("URI path cannot be a root " + ie); } // strip leading slash (path will always begin with "/") path.deleteCharAt(0); stripTrailingSlash(path); // obtain path from root to leaf String s[] = path.toString().split("/"); return s; } public static URI createURI(final String scheme, final String userInfo, final String host, final String path, final String query, final String fragment) { if (path == null) { throw new NullPointerException("path cannot be null"); } Integer port = 0; try { return new URI(scheme, userInfo, host, port, path, query, fragment); } catch (URISyntaxException e) { throw new RuntimeException(e); } } protected static URI createURI(final String scheme, final String[] pathAsArray) { if (pathAsArray == null) { throw new NullPointerException("path = null"); } // always start with a slash StringBuilder path = new StringBuilder("/"); int i = 0; for (i = 0; i < pathAsArray.length; i++) { String pathElement = pathAsArray[i]; if (0 == i && pathElement.length() == 0) { // handle when leading slash was included in the split, resulting // in an empty string as first element (ignore) continue; } if (1 == pathAsArray.length && pathElement.equals("/")) { // we're on first and only element, // and we're saying "root", so do nothing continue; } validateResource(pathElement); // throws path.append(pathElement + "/"); } try { URI u = new URI(scheme, null, path.toString(), null); return ensureNoTrailingSlash(u); } catch (Exception e) { throw new RuntimeException(e); } } public static URI ensureNoTrailingSlash(URI rawURI) { if (rawURI == null) { throw new NullPointerException("URI path is null."); } if (rawURI.getPath().trim().length() == 0) { throw new RuntimeException("URI path cannot be empty."); } String path = rawURI.getPath(); int index = path.length() - 1; try { // if the path itself is not root, remove the trailing slash if (!path.equals("/") && path.charAt(index) == '/') { // strip slash String newPath = rawURI.getPath().substring(0, index); return new URI(rawURI.getScheme(), rawURI.getHost(), newPath, rawURI.getFragment()); } else { return new URI(rawURI.getScheme(), rawURI.getHost(), rawURI.getPath(), rawURI.getFragment()); } } catch (URISyntaxException e) { throw new RuntimeException(e); } } /** * Strips trailing slash from the end of a StringBuilder object, if one * exists, and return whether the slash was removed. * * @return boolean - whether a trailing slash was removed */ private static boolean stripTrailingSlash(StringBuilder s) { final boolean hasTrailingSlash = s.charAt(s.length() - 1) == '/'; // strip trailing slash s = hasTrailingSlash ? s.deleteCharAt(s.length() - 1) : s; return hasTrailingSlash; } private static void validateResource(String resourceId) { if (resourceId.indexOf("/") != -1 || resourceId.indexOf("\\") != -1) { throw new RuntimeException("Resource id cannot contain a slash."); } } }