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:org.structr.common.PathHelper.java

/**
 * Assemble a relative path for the given absolute paths
 *
 * @param basePath//from  w  ww.  ja  v a 2s  .c  om
 * @param targetPath
 * @return relative node path
 */
public static String getRelativeNodePath(String basePath, String targetPath) {

    // Both paths are equal
    if (basePath.equals(targetPath)) {

        return ".";

    }

    if (basePath.equals(PATH_SEP) && (targetPath.length() > 1)) {

        // Base path is root path
        return targetPath.substring(1);
    }

    String[] baseAncestors = FilenameUtils.normalizeNoEndSeparator(basePath).split(PATH_SEP);
    String[] targetAncestors = FilenameUtils.normalizeNoEndSeparator(targetPath).split(PATH_SEP);
    int length = (baseAncestors.length < targetAncestors.length) ? baseAncestors.length
            : targetAncestors.length;
    int lastCommonRoot = -1;
    int i;

    // Iterate over the shorter path
    for (i = 0; i < length; i++) {

        if (baseAncestors[i].equals(targetAncestors[i])) {

            lastCommonRoot = i;

        } else {

            break;

        }

    }

    // Last common root is the common base path
    if (lastCommonRoot != -1) {

        StringBuilder newRelativePath = new StringBuilder();

        // How often must we go back from base path to common root?
        for (i = lastCommonRoot + 1; i < baseAncestors.length; i++) {

            if (baseAncestors[i].length() > 0) {

                newRelativePath.append(".." + PATH_SEP);

            }

        }

        // How often must we go forth from common root to get to tagret path?
        for (i = lastCommonRoot + 1; i < targetAncestors.length; i++) {

            newRelativePath.append(targetAncestors[i]).append(PATH_SEP);

        }

        // newRelativePath.append(targetAncestors[targetAncestors.length - 1]);
        String result = newRelativePath.toString();

        if (result.endsWith(PATH_SEP)) {

            result = result.substring(0, result.length() - 1);

        }

        return result;

    }

    return targetPath;
}

From source file:pt.webdetails.cda.utils.PathRelativizer.java

/**
 * Given two absolute paths, this method returns the targetPath relative to the basePath. It won't work with Windows
 * paths./*from   www  .  j a v  a 2 s .c  o  m*/
 *
 * @param basePath   Path to relativize against
 * @param targetPath Path to relativize
 * @return targetPath relative to basePath
 */
public static String relativizePath(String basePath, String targetPath) {
    basePath = FilenameUtils.normalizeNoEndSeparator(basePath);
    targetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);

    String[] basePathArray = basePath.length() > 1 ? basePath.split("/") : new String[] { "/" };
    String[] targetPathArray = targetPath.length() > 1 ? targetPath.split("/") : new String[] { "/" };

    //if we're dealing with folders, add a mock file
    if (basePathArray.length > 1 && !basePathArray[basePathArray.length - 1].contains(".")) {
        basePath += "/" + MOCK_FILE;
        basePathArray = basePath.split("/");
    }
    if (targetPathArray.length > 1 && !targetPathArray[targetPathArray.length - 1].contains(".")) {
        targetPath += "/" + MOCK_FILE;
        targetPathArray = targetPath.split("/");
    }

    int differCount = -1;
    for (int i = 0; (i < basePathArray.length) && (i < targetPathArray.length); i++) {
        if (basePathArray[i].equals(targetPathArray[i])) {
            continue;
        }
        differCount = i;
        break;
    }
    if (differCount == -1) {
        targetPathArray = cleanSamePath(basePathArray, targetPathArray);
        return StringUtils.join(targetPathArray, "/");
    } else {
        if (differCount == basePathArray.length - 1) { //case same folder or some folders ahead
            targetPathArray = cleanSamePath(basePathArray, targetPathArray);
            return StringUtils.join(targetPathArray, "/");
        } else { // case some folders back
            String backUp = "";
            for (int i = differCount; i < basePathArray.length - 1; i++) {
                backUp += "../";
            }
            targetPathArray = cleanSamePath(basePathArray, targetPathArray);
            return backUp + StringUtils.join(targetPathArray, "/");
        }

    }

}

From source file:pt.webdetails.cdf.dd.DashboardManagerWrapper.java

public static JXPathContext openDashboardAsJXPathContext(String dashboardLocation, DashboardWcdfDescriptor wcdf)
        throws IOException, FileNotFoundException, JSONException {
    InputStream input = null;/*from w w w.  j a v a  2s.co m*/
    String pathToFile = FilenameUtils
            .normalizeNoEndSeparator(System.getProperty("user.dir") + dashboardLocation);
    try {
        input = new FileInputStream(pathToFile);
        final JSONObject json = JsonUtils.readJsonFromInputStream(input);

        if (wcdf != null) {
            json.put("settings", wcdf.toJSON());
        }

        return JsonUtils.toJXPathContext(json);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:tauargus.model.batch.java

public static void setBatchDataPath(String f) {
    batchDataPath = FilenameUtils.normalizeNoEndSeparator(f);
    if (!batchDataPath.endsWith("\\")) {
        batchDataPath = batchDataPath + "\\";
    }//from  w ww.j  a v a  2  s.  c  om
}