Java String Shorten shortenPath(final String path)

Here you can find the source of shortenPath(final String path)

Description

Shortens the path to a maximum of 4 path elements.

License

Open Source License

Parameter

Parameter Description
path the path to the file (relative or absolute)

Return

shortened path

Declaration

public static String shortenPath(final String path) 

Method Source Code

//package com.java2s;

public class Main {
    public static final int DEFAULT_SHORTENER_THRESHOLD = 4;
    public static final String SHORTENER_BACKSLASH_REGEX = "\\\\";
    public static final String SHORTENER_SLASH_REGEX = "/";
    public static final String SHORTENER_BACKSLASH = "\\";
    public static final String SHORTENER_SLASH = "/";
    public static final String SHORTENER_ELLIPSE = "...";

    /**//  w  w  w. ja v a  2s .  co  m
     * Shortens the path to a maximum of 4 path elements.
     * 
     * @param path the path to the file (relative or absolute)
     * @return shortened path
     */
    public static String shortenPath(final String path) {
        return shortenPath(path, DEFAULT_SHORTENER_THRESHOLD);
    }

    /**
     * Shortens the path based on the given maximum number of path elements. E.g.,
     * "C:/1/2/test.txt" returns "C:/1/.../test.txt" if threshold is 1.
     * 
     * @param path the path to the file (relative or absolute)
     * @param threshold the number of directories to keep unshortened
     * @return shortened path
     */
    public static String shortenPath(final String path, final int threshold) {
        String regex = SHORTENER_BACKSLASH_REGEX;
        String sep = SHORTENER_BACKSLASH;

        if (path.indexOf("/") > 0) {
            regex = SHORTENER_SLASH_REGEX;
            sep = SHORTENER_SLASH;
        }

        String pathtemp[] = path.split(regex);
        // remove empty elements
        int elem = 0;
        {
            final String newtemp[] = new String[pathtemp.length];
            int j = 0;
            for (int i = 0; i < pathtemp.length; i++) {
                if (!pathtemp[i].equals("")) {
                    newtemp[j++] = pathtemp[i];
                    elem++;
                }
            }
            pathtemp = newtemp;
        }

        if (elem > threshold) {
            final StringBuilder sb = new StringBuilder();
            int index = 0;

            // drive or protocol
            final int pos2dots = path.indexOf(":");
            if (pos2dots > 0) {
                // case c:\ c:/ etc.
                sb.append(path.substring(0, pos2dots + 2));
                index++;
                // case http:// ftp:// etc.
                if (path.indexOf(":/") > 0 && pathtemp[0].length() > 2) {
                    sb.append(SHORTENER_SLASH);
                }
            } else {
                final boolean isUNC = path.substring(0, 2).equals(SHORTENER_BACKSLASH_REGEX);
                if (isUNC) {
                    sb.append(SHORTENER_BACKSLASH).append(SHORTENER_BACKSLASH);
                }
            }

            for (; index <= threshold; index++) {
                sb.append(pathtemp[index]).append(sep);
            }

            if (index == (elem - 1)) {
                sb.append(pathtemp[elem - 1]);
            } else {
                sb.append(SHORTENER_ELLIPSE).append(sep).append(pathtemp[elem - 1]);
            }
            return sb.toString();
        }
        return path;
    }
}

Related

  1. shortenName(final String name, final int toBeAdded)
  2. shortenName(String name)
  3. shortenOperationName(String operation)
  4. shortenPackageName(String name, boolean remove)
  5. shortenPart(String currentPart)
  6. shortenPrefix(String prefix)
  7. shortenRemoteRef(final String origin, final String ref)
  8. shortenString(String orig, int charsToRemove)
  9. shortenString(String orig, int maxLength)