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

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

Introduction

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

Prototype

public static String separatorsToUnix(String path) 

Source Link

Document

Converts all separators to the Unix separator of forward slash.

Usage

From source file:com.geewhiz.pacify.model.utils.ModelUtils.java

public static PFile clonePFile(PFile cloneFrom, Path file) {
    String relativePath = FilenameUtils
            .separatorsToUnix(cloneFrom.getPMarker().getFolder().toPath().relativize(file).toString());
    return clonePFile(cloneFrom, relativePath, file.toFile());
}

From source file:it.plugins.Project.java

public static Iterable<String> allFilesInDir(final String dirPath) {
    Collection<File> files = FileUtils.listFiles(new File(basedir(), dirPath), null, true);
    return from(files).transform(new Function<File, String>() {
        @Nullable/*from   w  w  w  .  ja  v  a  2 s.c  o  m*/
        public String apply(File file) {
            // transforms /absolute/path/to/src/java/Foo.java to src/java/Foo.java
            String filePath = FilenameUtils.separatorsToUnix(file.getPath());
            return dirPath + StringUtils.substringAfterLast(filePath, dirPath);
        }
    });
}

From source file:com.igormaznitsa.nbmindmap.nb.refactoring.elements.MoveFileActionPlugin.java

protected static String replaceNameInPath(int pathItemIndexFromEnd, final String path, final String newName) {
    final String normalizedSeparators = FilenameUtils.separatorsToUnix(path);
    int start = normalizedSeparators.length();
    while (start >= 0 && pathItemIndexFromEnd >= 0) {
        start = normalizedSeparators.lastIndexOf('/', start - 1);
        pathItemIndexFromEnd--;/*w ww . j  a  v a 2  s.com*/
    }

    String result = path;

    if (start >= 0) {
        final int indexEnd = normalizedSeparators.indexOf('/', start + 1);
        if (indexEnd <= 0) {
            result = path.substring(0, start + 1) + newName;
        } else {
            result = path.substring(0, start + 1) + newName + path.substring(indexEnd);
        }
    }
    return result;
}

From source file:com.thoughtworks.go.util.ArtifactLogUtil.java

private static String getPath(long buildId, String fileName) {
    String path = "" + buildId + File.separator + CRUISE_OUTPUT_FOLDER + File.separator + fileName;

    return FilenameUtils.separatorsToUnix(new File(path).getPath());
}

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

/**
 * Checks and normalizes the given path.
 *
 * @param path The path to clean up// w  w w .  jav 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:com.thoughtworks.go.domain.WildcardScanner.java

public WildcardScanner(File rootPath, String pattern) {
    this.rootPath = rootPath;
    this.pattern = FilenameUtils.separatorsToUnix(pattern);
}

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.  j ava  2  s  . c om*/
 *
 * @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:com.thoughtworks.go.server.dao.handlers.FileTypeHandlerCallback.java

@Override
public void setNonNullParameter(PreparedStatement ps, int i, File parameter, JdbcType jdbcType)
        throws SQLException {
    ps.setString(i, FilenameUtils.separatorsToUnix(parameter.getPath()));
}

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

/**
 * Gets the relative path from one file to another.
 *//* w w  w. j  a v a2 s .  c o  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.thoughtworks.go.config.AntTask.java

public String arguments() {
    ArrayList<String> args = new ArrayList<>();
    if (buildFile != null) {
        args.add("-f \"" + FilenameUtils.separatorsToUnix(buildFile) + "\"");
    }/*w ww  . j  a va 2 s  . c o m*/

    if (target != null) {
        args.add(target);
    }

    return StringUtils.join(args, " ");
}