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: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  ww  w  .java 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:MSUmpire.BaseDataStructure.TandemParam.java

public void SetCombineFileName(String filename, String tag) {
    CombinedPepXML = FilenameUtils.separatorsToUnix(FilenameUtils.getFullPath(filename) + "interact-"
            + FilenameUtils.getBaseName(filename) + tag + ".tandem.combine.pep.xml");
    CombinedProt = FilenameUtils.getFullPath(filename) + FilenameUtils.getBaseName(filename) + tag
            + ".tandem.Qcombine.prot.xml";
}

From source file:com.thoughtworks.go.server.dao.handlers.FileTypeHandlerCallback.java

protected File valueOf(String text) {
    if (text == null) {
        return null;
    }/*w  ww .ja v  a 2  s  . c o m*/
    return new File(FilenameUtils.separatorsToUnix(text));
}

From source file:edu.cornell.med.icb.util.ICBFilenameUtils.java

/**
 * This works somewhat differently than Apache Commons FilenameUtils.concat(). Notably
 * it always uses unix separators, if a "parts" entry starts with a "/" that's OK,
 * it will just be stripped out. Multiple ending "/" (such as s3://) are just fine.
 * This does NOT handle "..", etc. as FilenameUtils.concat() does. Also, this takes
 * n number of parts instead of just two. If ANY of them are null, this will return
 * null. See the associated test for some examples
 * @param parts the filename parts/*from  ww  w  . j a v  a 2  s . com*/
 * @return the path concat'd together
 */
public static String concatPathParts(final String... parts) {
    if (parts == null || parts.length == 0) {
        return null;
    }
    int estimatedLength = parts.length;
    for (final String part : parts) {
        if (part == null) {
            return null;
        }
        estimatedLength += part.length();
    }
    final MutableString result = new MutableString(estimatedLength);
    for (int i = 0; i < parts.length; i++) {
        final String part = FilenameUtils.separatorsToUnix(parts[i]);
        if (part.length() == 0) {
            // Doesn't contribute
            continue;
        }
        if (i == 0) {
            result.append(part);
            continue;
        }
        int skipInitialChars = 0;
        while (part.charAt(skipInitialChars) == '/') {
            skipInitialChars++;
        }
        if (!result.endsWith("/")) {
            result.append('/');
        }
        result.append(part, skipInitialChars, part.length());
    }
    return result.toString();
}

From source file:com.thoughtworks.go.config.BuildArtifactConfig.java

@Override
public String getDestination() {
    return StringUtils.isBlank(destination) ? DEFAULT_ROOT.getPath()
            : FilenameUtils.separatorsToUnix(destination);
}

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

public static String applyBaseDirIfRelativeAndNormalize(File baseDir, File actualFileToUse) {
    return FilenameUtils.separatorsToUnix(applyBaseDirIfRelative(baseDir, actualFileToUse).getPath());
}

From source file:com.thoughtworks.go.config.NantTask.java

public String arguments() {
    ArrayList<String> args = new ArrayList<>();
    if (buildFile != null) {
        args.add("-buildfile:\"" + FilenameUtils.separatorsToUnix(buildFile) + "\"");
    }/*from www  . j  a  v  a  2 s.c o m*/

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

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

From source file:com.thoughtworks.go.config.TestArtifactConfig.java

@Override
public String getDestination() {
    return StringUtils.isBlank(destination) ? TEST_OUTPUT_FOLDER : FilenameUtils.separatorsToUnix(destination);
}

From source file:com.googlecode.fascinator.common.storage.StorageUtils.java

/**
 * Generates a Object identifier for a given file
 * //from ww  w. j a  v  a2s .c o m
 * @param file the File to store
 * @return a String object id
 */
public static String generateOid(File file) {
    // MD5 hash the file path,
    String path = FilenameUtils.separatorsToUnix(file.getAbsolutePath());
    String hostname = "localhost";
    try {
        hostname = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException uhe) {
    }
    String username = System.getProperty("user.name", "anonymous");
    // log.debug("Generating OID path:'{}' hostname:'{}' username:'{}'",
    // new String[] { path, hostname, username });
    return DigestUtils.md5Hex(path + hostname + username);
}

From source file:com.jaeksoft.searchlib.util.FileUtils.java

public final static String systemPathToUnix(String filePath) {
    if ("\\".equals(File.separator))
        filePath = FilenameUtils.separatorsToUnix(filePath);
    return filePath;
}