Example usage for org.apache.commons.io FilenameUtils normalizeNoEndSeparator

List of usage examples for org.apache.commons.io FilenameUtils normalizeNoEndSeparator

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils normalizeNoEndSeparator.

Prototype

public static String normalizeNoEndSeparator(String filename) 

Source Link

Document

Normalizes a path, removing double and single dot path steps, and removing any final directory separator.

Usage

From source file:be.hikage.maven.plugin.xmlmerge.utils.PathUtils.java

public static String getRelativePath(File children, File base) {

    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(children.getAbsolutePath());
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(base.getAbsolutePath());

    System.out.println(normalizedTargetPath);
    System.out.println(normalizedBasePath);

    return normalizedTargetPath.substring(normalizedBasePath.length());

}

From source file:avantssar.aslanpp.testing.HTMLHelper.java

public static String removePrefix(File root, File f) {
    String fNorm = FilenameUtils.normalize(f.getAbsolutePath());
    String relativePath = fNorm;//from w  w w . j  av  a  2  s. c om
    if (root.isDirectory()) {
        String rootNorm = FilenameUtils.normalizeNoEndSeparator(root.getAbsolutePath()) + File.separator;
        if (relativePath.startsWith(rootNorm)) {
            relativePath = relativePath.substring(rootNorm.length());
        }
    }
    return relativePath;
}

From source file:com.splunk.shuttl.archiver.util.UtilsPath.java

public static String getNameOfPath(String path) {
    return FilenameUtils.getName(FilenameUtils.normalizeNoEndSeparator(path));
}

From source file:com.flysystem.core.util.PathUtil.java

/**
 * Normalize path./*ww  w  . java  2 s  . c  o m*/
 *
 * @return string
 */
public static String normalizePath(String path) {
    path = path.replaceAll("^[^[/\\\\]]*[/\\\\]", ""); //replace all / and \ at the start of the string
    return FilenameUtils.normalizeNoEndSeparator(path);
}

From source file:aurelienribon.utils.io.FilenameHelper.java

/**
 * Gets the relative path from one file to another.
 *//*from   w  w w .  jav  a2 s.  co  m*/
public static String getRelativePath(String targetPath, String basePath) {
    if (basePath == null || basePath.equals(""))
        return targetPath;
    if (targetPath == null || targetPath.equals(""))
        return "";

    String pathSeparator = File.separator;

    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    if (basePath.equals(targetPath))
        return "";

    // Undo the changes to the separators made by normalization
    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);

    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuilder common = new StringBuilder();

    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].equals(base[commonIndex])) {
        common.append(target[commonIndex]).append(pathSeparator);
        commonIndex++;
    }

    if (commonIndex == 0) {
        return targetPath;
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }

    StringBuilder relative = new StringBuilder();

    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

        for (int i = 0; i < numDirsUp; i++) {
            relative.append("..").append(pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:com.badlogic.gdx.box2deditor.utils.FileUtils.java

/**
 * Get the relative path from one file to another, specifying the directory separator.
 * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
 * '\'./*from w ww .ja v  a2s  .  c o  m*/
 *
 * @param target targetPath is calculated to this file
 * @param base basePath is calculated from this file
 * @param separator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example)
 * @return
 */
public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {
    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    // Undo the changes to the separators made by normalization
    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);

    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuilder common = new StringBuilder();

    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].equals(base[commonIndex])) {
        common.append(target[commonIndex]).append(pathSeparator);
        commonIndex++;
    }

    if (commonIndex == 0) {
        // No single common path element. This most
        // likely indicates differing drive letters, like C: and D:.
        // These paths cannot be relativized.
        throw new NoCommonPathFoundException("No common path element found for '" + normalizedTargetPath
                + "' and '" + normalizedBasePath + "'");
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }

    StringBuilder relative = new StringBuilder();

    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

        for (int i = 0; i < numDirsUp; i++) {
            relative.append("..").append(pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:ch.unibas.fittingwizard.infrastructure.base.ResourceUtils.java

/**
 * Get the relative path from one file to another, specifying the directory separator.
 * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
 * '\'./*from www . ja  v  a  2  s .  c  om*/
 *
 * @param targetPath targetPath is calculated to this file
 * @param basePath basePath is calculated from this file
 * @param pathSeparator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example)
 * @return
 */
public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {

    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    // Undo the changes to the separators made by normalization
    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);

    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuffer common = new StringBuffer();

    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].equals(base[commonIndex])) {
        common.append(target[commonIndex] + pathSeparator);
        commonIndex++;
    }

    if (commonIndex == 0) {
        // No single common path element. This most
        // likely indicates differing drive letters, like C: and D:.
        // These paths cannot be relativized.
        throw new PathResolutionException("No common path element found for '" + normalizedTargetPath
                + "' and '" + normalizedBasePath + "'");
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }

    StringBuffer relative = new StringBuffer();

    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

        for (int i = 0; i < numDirsUp; i++) {
            relative.append(".." + pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:alluxio.util.io.PathUtils.java

/**
 * Checks and normalizes the given path.
 *
 * @param path The path to clean up/*ww  w . j  av a 2 s.co  m*/
 * @return a normalized version of the path, with single separators between path components and
 *         dot components resolved
 * @throws InvalidPathException if the path is invalid
 */
public static String cleanPath(String path) throws InvalidPathException {
    validatePath(path);
    return FilenameUtils.separatorsToUnix(FilenameUtils.normalizeNoEndSeparator(path));
}

From source file:gov.nih.nci.cbiit.cmts.util.ResourceUtils.java

/**
 * Get the relative path from one file to another, specifying the directory separator.
* If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
* '\'./* w ww .j av  a 2  s .  com*/
*
 * @param targetPath is calculated to this file
* @param basePath is calculated from this file
* @param pathSeparator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example)
* @return target relative path from the base
*/

public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {
    if (targetPath.toLowerCase().startsWith("file:/")) {
        targetPath = targetPath.substring(6);
        while (targetPath.startsWith("/"))
            targetPath = targetPath.substring(1);
    }
    if (basePath.toLowerCase().startsWith("file:/")) {
        basePath = basePath.substring(6);
        while (basePath.startsWith("/"))
            basePath = basePath.substring(1);
    }

    String tempS = basePath;
    while (true) {
        File baseF = new File(tempS);
        if ((baseF.exists()) && (baseF.isFile())) {
            basePath = baseF.getAbsolutePath();
            break;
        }
        if (!pathSeparator.equals("/"))
            break;
        if (tempS.startsWith("/"))
            break;
        tempS = "/" + tempS;
    }
    tempS = targetPath;
    while (true) {
        File targetF = new File(tempS);
        if ((targetF.exists()) && (targetF.isFile())) {
            targetPath = targetF.getAbsolutePath();
            break;
        }
        if (!pathSeparator.equals("/"))
            break;
        if (tempS.startsWith("/"))
            break;
        tempS = "/" + tempS;
    }

    // Normalize the paths
    //System.out.println("ResourceUtils.getRelativePath()..target("+pathSeparator+"):"+targetPath);
    //System.out.println("ResourceUtils.getRelativePath()..base("+pathSeparator+"):"+basePath);
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);
    // Undo the changes to the separators made by normalization

    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);
    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);
    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    //System.out.println("ResourceUtils.getRelativePath()..normalizedTarget("+pathSeparator+"):"+normalizedTargetPath);
    //System.out.println("ResourceUtils.getRelativePath()..normalizedBase("+pathSeparator+"):"+normalizedBasePath);

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));
    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuffer common = new StringBuffer();
    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].trim().equals(base[commonIndex].trim())) {
        common.append(target[commonIndex].trim() + pathSeparator);
        commonIndex++;
    }
    if (commonIndex == 0) {
        // No single common path element. This most
        // likely indicates differing drive letters, like C: and D:.
        // These paths cannot be relativized.
        File ff = new File(targetPath);
        if ((ff.exists()) && (ff.isFile()))
            return ff.getAbsolutePath();
        else
            throw new IllegalArgumentException("No common path element found for '" + normalizedTargetPath
                    + "' and '" + normalizedBasePath + "'");
    }
    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;
    File baseResource = new File(normalizedBasePath);
    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();
    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }
    StringBuffer relative = new StringBuffer();
    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;
        for (int i = 0; i < numDirsUp; i++) {
            relative.append(".." + pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    //System.out.println("ResourceUtils.getRelativePath()..relativeTarget:"+relative.toString());
    return relative.toString();
}

From source file:com.github.neio.filesystem.paths.DirectoryPath.java

/**
 * Construct a directory based off of a file system path
 * @param directory the directory to be constructed
 * @throws PathException if the path represented is not a directory
 *//*from  w  w w .  j  a  v a2s. com*/
public DirectoryPath(String directory) throws PathException {
    super(FilenameUtils.normalizeNoEndSeparator(directory) + File.separator, Directory.class);
    this.platformDirectory = new File(super.path);

    if (platformDirectory.exists() == true) {
        if (platformDirectory.isDirectory() == false) {
            throw new PathException("[" + directory + "] is not a directory");
        }
    }
}