Java Unix Path unixizeWindowsAbsolutePath(final String pathToCheck)

Here you can find the source of unixizeWindowsAbsolutePath(final String pathToCheck)

Description

Turn absolute Windows paths into unix-friendly equivalent.

License

Apache License

Parameter

Parameter Description
pathToCheck Path to be checked for being a Windows path with drive letter. We assume it is canonical already, so the backslashes are turned to slashes.

Return

Cleaned up Windows path.

Declaration

public static String unixizeWindowsAbsolutePath(final String pathToCheck) 

Method Source Code

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

public class Main {
    /**/*from  w  ww.  jav  a  2s.  c  o m*/
     * Turn absolute Windows paths into unix-friendly equivalent. Keep as-is if not Windows absolute path.
     * Warning: A canonical version of <c>C:/hello</c> is actually <c>/C:/hello</c>. This needs to be worked out.
     * <p/>
     * Example:
     * c:something -> c/something
     * D:/test/test -> d/test/test
     * <p/>
     * Retaining the drive letter in the path prevents collisions in case multiple disk drives are used on
     * the Windows host. Removing the colon ensures friendly paths (colon is a special character).
     *
     * @param pathToCheck Path to be checked for being a Windows path with drive letter. We assume it is canonical already, so the backslashes are turned to slashes.
     * @return Cleaned up Windows path.
     */
    public static String unixizeWindowsAbsolutePath(final String pathToCheck) {
        final String path;
        if (pathToCheck.length() >= 1 && isKnownPathDelimiter(pathToCheck.charAt(0))) {
            // Initial slash. Remove temporarily;
            path = pathToCheck.substring(1);
        } else {
            path = pathToCheck;
        }
        if (path.length() >= 2 && path.charAt(1) == ':') {
            if (path.length() >= 3 && isKnownPathDelimiter(path.charAt(2))) {
                return Character.toLowerCase(path.charAt(0)) + path.substring(2);
            } else {
                return Character.toLowerCase(path.charAt(0)) + "/" + path.substring(2);
            }
        }
        return pathToCheck;
    }

    /**
     * True if given character can be a delimiter on some system. Currently / and \\
     *
     * @param delimiter Delimiter to test
     * @return True if it is a potential path delimiter
     */
    public static boolean isKnownPathDelimiter(final char delimiter) {
        return delimiter == '/' || delimiter == '\\';
    }
}

Related

  1. getUNIXfilePath(String fileName)
  2. getUnixRelativePath(File base, File path)
  3. toUNIXfilePath(String fileName)
  4. unixPath(String sText)
  5. unixPathStr(String anyPath)
  6. unixPathToWindows(String strPath)
  7. unixToUserPathName(String unixPathName)