Java Relative Path Get relativizePath(final File path, final File base)

Here you can find the source of relativizePath(final File path, final File base)

Description

Relativize a path from a base path.

License

LGPL

Parameter

Parameter Description
path path to relativize
base base path (must be a directory)

Return

a File object with the relative path

Declaration

public static final File relativizePath(final File path, final File base) 

Method Source Code

//package com.java2s;
/*// w w  w. java 2 s.c o  m
 *                  Eoulsan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public License version 2.1 or
 * later and CeCILL-C. This should be distributed with the code.
 * If you do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/lgpl-2.1.txt
 *      http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'?cole normale sup?rieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Eoulsan project and its aims,
 * or to join the Eoulsan Google group, visit the home page
 * at:
 *
 *      http://outils.genomique.biologie.ens.fr/eoulsan
 *
 */

import java.io.File;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    /**
     * Relativize a path from a base path.
     * @param path path to relativize
     * @param base base path (must be a directory)
     * @return a File object with the relative path
     */
    public static final File relativizePath(final File path, final File base) {

        if (path == null) {
            throw new NullPointerException("The path is null");
        }

        if (base == null) {
            return path;
        }

        final File absPath = path.getAbsoluteFile();
        final File absBase = base.getAbsoluteFile();

        List<String> pathNodes = new ArrayList<>();
        List<String> baseNodes = new ArrayList<>();

        File parent = absPath;
        do {
            pathNodes.add(parent.getName());
        } while ((parent = parent.getParentFile()) != null);

        parent = absBase;
        do {
            baseNodes.add(parent.getName());
        } while ((parent = parent.getParentFile()) != null);

        Collections.reverse(pathNodes);
        Collections.reverse(baseNodes);

        final int minSize = Math.min(pathNodes.size(), baseNodes.size());
        int i = 0;

        while (i < minSize && pathNodes.get(i).equals(baseNodes.get(i))) {
            i++;
        }

        final List<String> resultNodes = new ArrayList<>();

        if (i < baseNodes.size()) {
            for (int j = 0; j < baseNodes.size() - i; j++) {
                resultNodes.add("..");
            }
        }

        resultNodes.addAll(pathNodes.subList(i, pathNodes.size()));

        return createFile(resultNodes);
    }

    /**
     * Create a File object from a list of the node of the file path.
     * @param pathNodes the list of the nodes of the path
     * @return a new File object with the requested path
     */
    private static File createFile(final List<String> pathNodes) {

        File result = null;

        for (String f : pathNodes) {
            if (result == null) {
                result = new File(f);
            } else {
                result = new File(result, f);
            }
        }

        return result;
    }
}

Related

  1. relativize(final File root, final File target)
  2. relativize(String path)
  3. relativize(String path)
  4. relativize(String path, String base)
  5. relativizePath(File from, File to)
  6. relativizePath(String parent, String child)
  7. relativizePathSegments(String[] srcSegments, String[] destSegments)