Java Path Normalize normalizeFullPath(String path)

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

Description

Normalizes full path names by resolving .

License

Open Source License

Parameter

Parameter Description
path The path to operate on.

Return

The normalized path.

Declaration

public static final String normalizeFullPath(String path) 

Method Source Code

//package com.java2s;
/*//ww  w. j  a  v  a  2s .  c o m
 * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

public class Main {
    /**
     * Normalizes full path names by resolving . and .. path portions.
     *
     * @param path The path to operate on.
     * @return The normalized path.
     */
    public static final String normalizeFullPath(String path) {
        if (path != null) {
            int index;
            StringBuilder buffer;
            char ch;

            path = path.replace('\\', '/');

            do {
                index = path.indexOf("/./");
                if (index != -1) {
                    buffer = new StringBuilder(path);
                    buffer.delete(index, index + 2);
                    path = buffer.toString();
                }
            } while (index != -1);

            do {
                index = path.indexOf("/../");
                if (index != -1) {
                    int length = 3;

                    buffer = new StringBuilder(path);

                    while (index > 0) {
                        ch = buffer.charAt(--index);
                        length++;
                        if (ch == '/') {
                            break;
                        } else if (ch == ':') {
                            index++;
                            length--;
                            break;
                        }
                    }

                    buffer.delete(index, index + length);
                    path = buffer.toString();
                }
            } while (index != -1);

            if (path.endsWith("/.")) {
                path = path.substring(0, path.length() - 2);
            }

            if (path.endsWith("/..")) {
                index = path.length() - 3;

                while (index > 0) {
                    ch = path.charAt(--index);
                    if (ch == '/' || ch == ':') {
                        break;
                    }
                }

                path = path.substring(0, index);
            }

            if (path.length() > 1 && path.charAt(1) == ':') {
                path = Character.toUpperCase(path.charAt(0)) + ":" + path.substring(2);
            }
        }

        return path;
    }
}

Related

  1. normalizeBasePath(String basePath)
  2. normalizeDbPath(String databasePath)
  3. normalizeDotSegment(String path)
  4. normalizedPath(String original)
  5. normalizeFileSystemPath(String path)
  6. normalizeLocalPath(final String localPath)
  7. normalizeMediaPath(String hrefBaseUrl, String localMediaPath, String href)
  8. normalizeName(final String path, final boolean isDirectory)
  9. NormalizeName(String absolutePath)