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.apache.torque.generator.configuration.ClasspathConfigurationProvider.java

protected String getFileName(String name, String directory) {
    String fileName = getConfigResourceBase() + "/" + directory + "/" + name;
    // make double dots work for resources in jar files
    fileName = FilenameUtils.normalizeNoEndSeparator(fileName);
    fileName = FilenameUtils.separatorsToUnix(fileName);
    return fileName;
}

From source file:org.batoo.jpa.parser.impl.acl.ClassloaderAnnotatedClassLocator.java

private Set<Class<?>> findClasses(ClassLoader cl, Set<Class<?>> classes, String root, String path) {
    final File file = new File(path);

    if (file.isDirectory()) {
        ClassloaderAnnotatedClassLocator.LOG.debug("Processing directory {0}", path);

        for (final String child : file.list()) {
            this.findClasses(cl, classes, root, path + "/" + child);
        }/*w w  w.  ja va 2  s  . co  m*/
    } else {
        if (FilenameUtils.isExtension(path, "class")) {
            final String normalizedPath = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(path));

            final int rootLength = FilenameUtils.normalizeNoEndSeparator(root).length();
            String className = normalizedPath.substring(rootLength + 1).replaceAll("/", ".");
            className = StringUtils.left(className, className.length() - 6);

            final Class<?> clazz = this.isPersistentClass(cl, className);
            if (clazz != null) {
                ClassloaderAnnotatedClassLocator.LOG.debug("Found persistent class {0}", className);
                classes.add(clazz);
            }
        }
    }

    return classes;
}

From source file:org.broad.igv.util.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  w w .j  a v  a2s.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)
 * @return
 */
public static String getRelativePath(String basePath, String targetPath, 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.
        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;
    }

    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.codehaus.groovy.grails.web.pages.GroovyPageResourceLoader.java

protected boolean compareFilePaths(File f1, File f2) {
    return FilenameUtils.normalizeNoEndSeparator(f1.getAbsolutePath())
            .equals(FilenameUtils.normalizeNoEndSeparator(f2.getAbsolutePath()));
}

From source file:org.craftercms.engine.view.CrafterPageViewResolver.java

@Override
public View resolveViewName(String renderUrl, Locale locale) throws Exception {
    String storeUrl = urlTransformationService.transform(renderUrlToStoreUrlTransformerName, renderUrl,
            cacheUrlTransformations);//from   w  ww  . ja v  a2  s  .c om
    View view = getCachedLocalizedView(storeUrl, locale);

    if (view != null && view instanceof CrafterPageView) {
        CrafterPageView pageView = (CrafterPageView) view;

        if (SiteProperties.isRedirectToTargetedUrl()) {
            String finalStoreUrl = pageView.getPage().getStoreUrl();
            String finalRenderUrl = urlTransformationService.transform(storeUrlToRenderUrlTransformerName,
                    finalStoreUrl, cacheUrlTransformations);

            renderUrl = FilenameUtils.normalizeNoEndSeparator(renderUrl);
            finalRenderUrl = FilenameUtils.normalizeNoEndSeparator(finalRenderUrl);

            if (!renderUrl.equals(finalRenderUrl)) {
                return getRedirectView(finalRenderUrl, true);
            }
        }

        accessManager.checkAccess(pageView.getPage());
    }

    return view;
}

From source file:org.eclim.installer.step.EclipseStep.java

/**
 * {@inheritDoc}/*from   www.ja v a  2 s  .  c o  m*/
 * @see org.formic.wizard.WizardStep#proceed()
 */
public boolean proceed() {
    boolean proceed = super.proceed();
    String home = (String) Installer.getContext().getValue("eclipse.home");
    home = FilenameUtils.normalizeNoEndSeparator(home).replace('\\', '/');
    Installer.getContext().setValue("eclipse.home", home);
    return proceed;
}

From source file:org.geotools.gce.imagecollection.ImageCollectionReader.java

/**
 * Creates a new instance of ImageCollectionReader
 * //from   ww  w . jav a 2  s  . c om
 * @param input
 *            the input folder
 * @param uHints
 *            user-supplied hints TODO currently are unused
 * @throws DataSourceException
 */
public ImageCollectionReader(Object input, Hints uHints) throws DataSourceException {
    super(input, uHints);

    //
    // Set the source being careful in case it is an URL pointing to a file
    //
    File file = null;
    try {
        this.sourceURL = Utils.checkSource(source);
        source = DataUtilities.urlToFile(sourceURL);
        if (source instanceof File) {
            file = (File) source;
            rootPath = FilenameUtils.getFullPath(
                    FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath()) + Utils.SEPARATOR);
            loadConfig();
        }

    } catch (IOException e) {
        throw new DataSourceException(e);
    }

    rasterManager = new RasterManager(this);
    getHRInfo();
}

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 ww w  .  j  ava 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.jaggeryjs.hostobjects.ws.WSRequestHostObject.java

private static CryptoConfig getCryptoConfig(WSRequestHostObject wsRequest, NativeObject crypto)
        throws ScriptException {
    Properties merlinProp = new Properties();
    File file = new File(FilenameUtils.normalizeNoEndSeparator(getObjectProperty(crypto, "file")));
    merlinProp.put("org.apache.ws.security.crypto.merlin.file", file.getAbsolutePath());
    merlinProp.put("org.apache.ws.security.crypto.merlin.keystore.type", getObjectProperty(crypto, "type"));
    merlinProp.put("org.apache.ws.security.crypto.merlin.keystore.password",
            getObjectProperty(crypto, "password"));
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setProvider("org.apache.ws.security.components.crypto.Merlin");
    cryptoConfig.setProp(merlinProp);//  www  .  j a  v  a  2  s.  c  o  m
    Object property = crypto.get(CryptoConfig.CACHE_ENABLED, crypto);
    if ((property instanceof Boolean && (Boolean) property)
            || (property instanceof String && Boolean.parseBoolean((String) property))) {
        cryptoConfig.setCacheEnabled(true);
        cryptoConfig.setCryptoKey("org.apache.ws.security.crypto.merlin.file");
    } else if (property != null && !(property instanceof Undefined) && !(property instanceof UniqueTag)) {
        throw new ScriptException(
                "Invalid value for property '" + CryptoConfig.CACHE_ENABLED + "' in rampart configuration");
    }

    property = crypto.get(CryptoConfig.CACHE_REFRESH_INTVL, crypto);
    if (property instanceof Integer) {
        cryptoConfig.setCacheRefreshInterval(Integer.toString((Integer) property));
    } else if (property instanceof String) {
        cryptoConfig.setCacheRefreshInterval((String) property);
    } else if (property != null && !(property instanceof Undefined) && !(property instanceof UniqueTag)) {
        throw new ScriptException("Invalid value for property '" + CryptoConfig.CACHE_REFRESH_INTVL
                + "' in rampart configuration");
    }
    return cryptoConfig;

}

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   w w  w. j ava 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)
 */
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();
}