Java Path Relative Get getRelativePath(String baseFullPath, File file)

Here you can find the source of getRelativePath(String baseFullPath, File file)

Description

get Relative Path

License

Open Source License

Parameter

Parameter Description
baseFullPath The base full path.
file The target file.

Return

A relative path from a file based on a specified full path. If this is not possible, the original target full path is returned.

Declaration

public static final String getRelativePath(String baseFullPath, File file) 

Method Source Code


//package com.java2s;
/*/*from   w w  w .j  av  a  2 s .  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.
 */

import java.io.File;

public class Main {
    /**
     * @param baseFullPath The base full path.
     * @param targetFullPath The target full path.
     * @return A relative path based on a specified full path. If this is not possible, the original
     *         target full path is returned.
     */
    public static final String getRelativePath(String baseFullPath, String targetFullPath) {
        if (baseFullPath != null) {
            baseFullPath = baseFullPath.replace('\\', '/');
        }
        if (targetFullPath != null) {
            targetFullPath = targetFullPath.replace('\\', '/');
        }

        String common = getCommonRoot(baseFullPath, targetFullPath);

        if (common != null) {
            if (common.equals(baseFullPath)) {
                return targetFullPath.substring(common.length());
            }

            StringBuilder buffer = new StringBuilder(targetFullPath.length());
            String remainder = baseFullPath.substring(common.length());
            int i = remainder.indexOf('/');

            while (i != -1) {
                buffer.append("../");
                i = remainder.indexOf('/', i + 1);
            }
            buffer.append(targetFullPath.substring(common.length()));
            return buffer.toString();
        }
        return targetFullPath;
    }

    /**
     * @param baseFullPath The base full path.
     * @param file The target file.
     * @return A relative path from a file based on a specified full path. If this is not possible,
     *         the original target full path is returned.
     */
    public static final String getRelativePath(String baseFullPath, File file) {
        return getRelativePath(baseFullPath, getFullPath(file));
    }

    /**
     * @param fullPathOne The first full path.
     * @param fullPathTwo The second full path.
     * @return The common root path of two full paths, or <code>null</code> if there is none.
     */
    public static final String getCommonRoot(String fullPathOne, String fullPathTwo) {
        int i;
        int len1;
        int len2;
        int max;

        if (fullPathOne == null) {
            len1 = 0;
        } else {
            fullPathOne = fullPathOne.replace('\\', '/');
            len1 = fullPathOne.length();
        }

        if (fullPathTwo == null) {
            len2 = 0;
        } else {
            fullPathTwo = fullPathTwo.replace('\\', '/');
            len2 = fullPathTwo.length();
        }

        max = len1 > len2 ? len2 : len1;

        for (i = 0; i < max; i++) {
            if (fullPathOne.charAt(i) != fullPathTwo.charAt(i)) {
                i--;
                break;
            }
        }

        if (i == max) {
            i = max - 1;
        }
        while (i >= 0 && fullPathOne.charAt(i) != '/') {
            i--;
        }
        if (i < 0) {
            return null;
        }
        return fullPathOne.substring(0, i + 1);
    }

    /**
     * @param baseFullPath The base path.
     * @param relativePath The path relative to the base path.
     * @return A full path based on the specified base path. If the relative path passed in is
     *         actually a full path, the original relative path is returned.
     */
    public static final String getFullPath(String baseFullPath, String relativePath) {
        String result;

        if (relativePath != null) {
            if (baseFullPath != null) {
                baseFullPath = baseFullPath.replace('\\', '/');
                relativePath = relativePath.replace('\\', '/');
                if (isFullPath(relativePath)) {
                    result = relativePath;
                } else if (baseFullPath.endsWith("/")) {
                    result = baseFullPath + relativePath;
                } else {
                    result = baseFullPath + "/" + relativePath;
                }
            } else {
                result = relativePath.replace('\\', '/');
            }
        } else if (baseFullPath != null) {
            result = baseFullPath;
        } else {
            result = "./";
        }

        if (result.startsWith("./") || result.startsWith("../")) {
            return getFullPath(getFullPath(new File(".")), result);
        }

        return normalizeFullPath(result);
    }

    /**
     * @param file The file to operate on.
     * @return A full path from a file.
     */
    public static final String getFullPath(File file) {
        if (file != null) {
            return normalizeFullPath(file.getAbsolutePath().replace('\\', '/'));
        }
        return null;
    }

    /**
     * @param path The path to operate on.
     * @return <code>true</code> if the specified path is an absolute path.
     */
    public static final boolean isFullPath(String path) {
        boolean isFullPath = false;

        if (path != null) {
            int length = path.length();

            if (length > 0) {
                char ch;

                path = path.replace('\\', '/');
                ch = path.charAt(0);

                if (ch == '/' || path.startsWith("//") || length > 1 && path.charAt(1) == ':'
                        && (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')) {
                    isFullPath = true;
                }
            }
        }
        return isFullPath;
    }

    /**
     * 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. getRelativePath(final File parentDir, final File file)
  2. getRelativePath(final File parentDirectory, final File file)
  3. getRelativePath(final String base, final File targetFile)
  4. getRelativePath(String base, String fullPath)
  5. getRelativePath(String base, String relative, boolean isBaseFile)
  6. getRelativePath(String basePath, String path)
  7. getRelativePath(String basePath, String pathToRelativize)
  8. getRelativePath(String fileName, String projFile)
  9. getRelativePath(String filePath, String basePath)