Android Path Normalize normalizeWindowsPath(String path)

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

Description

normalize Windows Path

License

Open Source License

Declaration

public static String normalizeWindowsPath(String path) 

Method Source Code

//package com.java2s;
/*/*from  www  .  jav a2  s . c  om*/
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.StringTokenizer;

public class Main {
    private static final char COLON_CHAR = ':';
    private static final String UNC_PREFIX = "//";
    private static final String SLASH = "/";
    private static final char SLASH_CHAR = '/';
    private static final char BACKSLASH_CHAR = '\\';
    /** ??????????"." */
    public static final String CURRENT_DIR = ".";
    /** ?????????".." */
    public static final String UP_LEVEL_DIR = "..";

    public static String normalizeWindowsPath(String path) {
        return normalizePath(path, true);
    }

    public static String normalizePath(String path) {
        return normalizePath(path, isWindows());
    }

    private static String normalizePath(String path, boolean isWindows) {
        if (path == null) {
            return null;
        }

        path = path.trim();

        // ?"\\"??????"/"?????????
        path = path.replace(BACKSLASH_CHAR, SLASH_CHAR);

        // ?????????????????windows?????????"C:"??"//hostname"
        String prefix = getSystemDependentPrefix(path, isWindows);

        if (prefix == null) {
            return null;
        }

        path = path.substring(prefix.length());

        // ????????prefix???"/"??????????????prefix.length > 0
        if (prefix.length() > 0 || path.startsWith(SLASH)) {
            prefix += SLASH_CHAR;
        }

        // ???path???"/"
        boolean endsWithSlash = path.endsWith(SLASH);

        // ??????"."?".."
        StringTokenizer tokenizer = new StringTokenizer(path, "/");
        StringBuffer buffer = new StringBuffer(prefix.length()
                + path.length());
        int level = 0;

        buffer.append(prefix);

        while (tokenizer.hasMoreTokens()) {
            String element = tokenizer.nextToken();

            // ??"."
            if (CURRENT_DIR.equals(element)) {
                continue;
            }

            // ??".."
            if (UP_LEVEL_DIR.equals(element)) {
                if (level == 0) {
                    // ??prefix?????????????????????????
                    // ??null??????????
                    if (prefix.length() > 0) {
                        return null;
                    }

                    buffer.append(UP_LEVEL_DIR).append(SLASH_CHAR);
                } else {
                    level--;

                    boolean found = false;

                    for (int i = buffer.length() - 2; i >= prefix.length(); i--) {
                        if (buffer.charAt(i) == SLASH_CHAR) {
                            buffer.setLength(i + 1);
                            found = true;
                            break;
                        }
                    }

                    if (!found) {
                        buffer.setLength(prefix.length());
                    }
                }

                continue;
            }

            // ???path
            buffer.append(element).append(SLASH_CHAR);
            level++;
        }

        // ????????????"./"
        if (buffer.length() == 0) {
            buffer.append(CURRENT_DIR).append(SLASH_CHAR);
        }

        // ????????"/"
        if (!endsWithSlash && buffer.length() > prefix.length()
                && buffer.charAt(buffer.length() - 1) == SLASH_CHAR) {
            buffer.setLength(buffer.length() - 1);
        }

        return buffer.toString();
    }

    private static boolean isWindows() {
        return System.getProperty("os.name").startsWith("Windows");
    }

    private static String getSystemDependentPrefix(String path,
            boolean isWindows) {
        if (isWindows) {
            // ??UNC??
            if (path.startsWith(UNC_PREFIX)) {
                // ????UNC???"//"
                if (path.length() == UNC_PREFIX.length()) {
                    return null;
                }

                // ???????//hostname/subpath???//hostname
                int index = path.indexOf(SLASH, UNC_PREFIX.length());

                if (index != -1) {
                    return path.substring(0, index);
                } else {
                    return path;
                }
            }

            // ??Windows??????"c:/..."
            if (path.length() > 1 && path.charAt(1) == COLON_CHAR) {
                return path.substring(0, 2).toUpperCase();
            }
        }

        return "";
    }
}

Related

  1. getCanonicalPath(File pFile)
  2. normalizeFile(String path)
  3. normalizePath(String path)
  4. normalizePath(String path, boolean isWindows)
  5. normalizeUnixPath(String path)