Java URI to Path getPathAsArray(URI uri)

Here you can find the source of getPathAsArray(URI uri)

Description

get Path As Array

License

Apache License

Declaration

public static String[] getPathAsArray(URI uri) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.URI;

public class Main {
    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);
        }/*  w  w w. j  a va  2s.  co  m*/

        // 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;
    }

    /**
     * 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;
    }
}

Related

  1. getPath(final URI uri)
  2. getPath(String uriStr)
  3. getPath(URI addr)
  4. getPath(URI uri)
  5. getPathAndQueryURI(URI requestUri)
  6. getPathByTrimmingEndingFileSeparator(URI uri)
  7. getPathComponents(URI uri)
  8. getPathParamValue(URI uri)
  9. getPathSegments(URI uri)