Java Path Normalize normalizePath(String path)

Here you can find the source of normalizePath(String path)

Description

normalize Path

License

Creative Commons License

Declaration

private static String normalizePath(String path) 

Method Source Code

//package com.java2s;

public class Main {
    private static String normalizePath(String path) {
        int len = path.length();
        if (len == 0 || path.equals("..") || path.equals(".")) {
            return "";
        }//  www  .j  a v  a  2 s .c om
        if (path.indexOf("/. ") < 0 && path.indexOf("./ ") < 0) {
            return path;
        }
        StringBuilder builder = new StringBuilder();
        int index = 0;
        while (index < len) {
            char c = path.charAt(index);
            if ((index + 3 <= len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '/')
                    || (index + 2 == len && c == '.' && path.charAt(index + 1) == '.')) {
                // begins with "/./" or is "..";
                // move index by 2
                index += 2;
                continue;
            }
            if (index + 3 <= len && c == '.' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '/') {
                // begins with "../";
                // move index by 3
                index += 3;
                continue;
            }
            if ((index + 2 <= len && c == '.' && path.charAt(index + 1) == '/') || (index + 1 == len && c == '.')) {
                // begins with "./" or is ".";
                // move index by 1
                ++index;
                continue;
            }
            if (index + 2 == len && c == '/' && path.charAt(index + 1) == '.') {
                // is "/."; append '/' and break
                builder.append('/');
                break;
            }
            if (index + 3 == len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '.') {
                // is "/.."; remove last segment,
                // append "/" and return
                int index2 = builder.length() - 1;
                while (index2 >= 0) {
                    if (builder.charAt(index2) == '/') {
                        break;
                    }
                    --index2;
                }
                if (index2 < 0) {
                    index2 = 0;
                }
                builder.setLength(index2);
                builder.append('/');
                break;
            }
            if (index + 4 <= len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '.'
                    && path.charAt(index + 3) == '/') {
                // begins with "/../"; remove last segment
                int index2 = builder.length() - 1;
                while (index2 >= 0) {
                    if (builder.charAt(index2) == '/') {
                        break;
                    }
                    --index2;
                }
                if (index2 < 0) {
                    index2 = 0;
                }
                builder.setLength(index2);
                index += 3;
                continue;
            }
            builder.append(c);
            ++index;
            while (index < len) {
                // Move the rest of the
                // path segment until the next '/'
                c = path.charAt(index);
                if (c == '/') {
                    break;
                }
                builder.append(c);
                ++index;
            }
        }
        return builder.toString();
    }
}

Related

  1. normalizePath(String path)
  2. normalizePath(String path)
  3. normalizePath(String path)
  4. normalizePath(String path)
  5. normalizePath(String path)
  6. normalizePath(String path)
  7. normalizePath(String path)
  8. normalizePath(String path)
  9. normalizePath(String path)