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

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

Introduction

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

Prototype

public static String separatorsToWindows(String path) 

Source Link

Document

Converts all separators to the Windows separator of backslash.

Usage

From source file:org.geotools.gce.imagemosaic.CatalogManager.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 .j  av a  2  s .c  o  m*/
 * 
 * @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.
    StringBuilder common = new StringBuilder();

    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 RuntimeException("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(".." + pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:org.gradle.api.tasks.wrapper.internal.WrapperScriptGenerator.java

private void createWindowsScript(String jarPath, File scriptFile, String wrapperPropertiesPath)
        throws IOException {
    String windowsWrapperScriptHead = IOUtils
            .toString(getClass().getResourceAsStream("windowsWrapperScriptHead.txt"));
    String windowsWrapperScriptTail = IOUtils
            .toString(getClass().getResourceAsStream("windowsWrapperScriptTail.txt"));
    String fillingWindows = "" + WINDOWS_NL + "set STARTER_MAIN_CLASS=" + FULLY_QUALIFIED_WRAPPER_NAME
            + WINDOWS_NL + "set CLASSPATH=" + CURRENT_DIR_WINDOWS + "\\"
            + FilenameUtils.separatorsToWindows(jarPath) + WINDOWS_NL + "set WRAPPER_PROPERTIES="
            + CURRENT_DIR_WINDOWS + "\\" + FilenameUtils.separatorsToWindows(wrapperPropertiesPath)
            + WINDOWS_NL;/*from  w w w  .ja v a  2  s. co m*/
    String windowsScript = windowsWrapperScriptHead + fillingWindows + windowsWrapperScriptTail;
    File windowsScriptFile = new File(scriptFile.getParentFile(), scriptFile.getName() + ".bat");
    FileUtils.writeStringToFile(windowsScriptFile, transformIntoWindowsNewLines(windowsScript));
}

From source file:org.gradle.api.tasks.wrapper.WrapperScriptGenerator.java

private void createWindowsScript(String jarPath, File scriptDestinationDir, String wrapperPropertiesPath)
        throws IOException {
    String windowsWrapperScriptHead = IOUtils
            .toString(Wrapper.class.getResourceAsStream("windowsWrapperScriptHead.txt"));
    String windowsWrapperScriptTail = IOUtils
            .toString(Wrapper.class.getResourceAsStream("windowsWrapperScriptTail.txt"));
    String fillingWindows = "" + WINDOWS_NL + "set STARTER_MAIN_CLASS=" + FULLY_QUALIFIED_WRAPPER_NAME
            + WINDOWS_NL + "set CLASSPATH=" + CURRENT_DIR_WINDOWS + "\\"
            + FilenameUtils.separatorsToWindows(jarPath) + WINDOWS_NL + "set WRAPPER_PROPERTIES="
            + CURRENT_DIR_WINDOWS + "\\" + FilenameUtils.separatorsToWindows(wrapperPropertiesPath)
            + WINDOWS_NL;/*www . j  a v a2  s.c o m*/
    String windowsScript = windowsWrapperScriptHead + fillingWindows + windowsWrapperScriptTail;
    File windowsScriptFile = new File(scriptDestinationDir, "gradlew.bat");
    FileUtils.writeStringToFile(windowsScriptFile, transformIntoWindowsNewLines(windowsScript));
}

From source file:org.jumpmind.symmetric.file.FileSyncUtils.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.j  av a 2 s  .  com
 * @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)
 */
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:org.larz.dom4.editor.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
 * '\'./*www . ja va 2 s.  co m*/
 * 
 * @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:org.sejda.cli.MergeTaskTest.java

private static List<Matcher<Iterable<? super File>>> filesList(String... filenames) {
    List<Matcher<Iterable<? super File>>> result = new ArrayList<Matcher<Iterable<? super File>>>();
    for (String current : filenames) {
        String filename = current.toString();
        if (FilenameUtils.getPrefixLength(filename) > 0) {
            result.add(CombinableMatcher.<Iterable<? super File>>either(hasItem(new File(filename)))
                    .or(hasItem(new File(FilenameUtils.separatorsToWindows("C:" + filename)))));
        }//from  w  w  w .j a  va 2s  .c o m
        result.add(hasItem(new File(filename)));
    }
    return result;
}

From source file:org.sonar.api.batch.fs.internal.DefaultFilePredicatesTest.java

@Test
public void has_absolute_path() throws Exception {
    String path = javaFile.file().getAbsolutePath();
    assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
    assertThat(predicates.hasAbsolutePath(FilenameUtils.separatorsToWindows(path)).apply(javaFile)).isTrue();

    assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
    assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
}

From source file:org.sourcepit.common.utils.path.PathUtils.java

public static String normalize(String targetPath, String pathSeparator) {
    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);

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

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

    } else {/*from  w  ww.j a va 2 s.  co  m*/
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }
    return normalizedTargetPath;
}