List of usage examples for org.apache.commons.io FilenameUtils normalizeNoEndSeparator
public static String normalizeNoEndSeparator(String filename)
From source file:com.apporiented.hermesftp.usermanager.impl.PermissionDataTest.java
private String getDir(String relDir) { File dir = new File(ftproot, relDir); return FilenameUtils.normalizeNoEndSeparator(dir.getAbsolutePath()); }
From source file:com.igormaznitsa.jcp.AbstractSpyPreprocessorContextTest.java
protected String getCurrentTestFolder() { final String testFolder = FilenameUtils.normalizeNoEndSeparator(System.getProperty("test.folder")); final String fullClassPath = this.getClass().getName().replace('.', File.separatorChar); return FilenameUtils.normalize(testFolder + File.separator + fullClassPath.substring(0, fullClassPath.lastIndexOf(File.separatorChar))); }
From source file:com.github.neio.filesystem.paths.FilePath.java
/** * @param file the path to the file that is to be pointed at by this path * @throws PathException If the path exists, but is not a file then an exception will be thrown. *///w ww. java 2 s . c o m public FilePath(String file) throws PathException { super(FilenameUtils.normalizeNoEndSeparator(file), File.class); java.io.File tempFile = new java.io.File(file); if (tempFile.exists() == true) { if (tempFile.isFile() == false) { throw new PathException("[" + file + "] is not a file"); } } this.platformFile = tempFile; }
From source file:me.childintime.childintime.util.file.PathUtils.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 '\'. * * @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 The relative path.//from w w w . j ava2 s . c om */ 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 switch (pathSeparator) { case "/": normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath); break; case "\\": normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath); break; default: 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 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; 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:de.mpg.imeji.presentation.beans.PropertyBean.java
/** * Default constructor//from w w w . j a v a2 s . c o m */ public PropertyBean() { try { this.digilibEnabled = Boolean.parseBoolean(PropertyReader.getProperty("imeji.digilib.enable")); this.internalStorageBase = FilenameUtils.getBaseName( FilenameUtils.normalizeNoEndSeparator(PropertyReader.getProperty("imeji.storage.path"))); applicationURL = StringHelper.normalizeURI(PropertyReader.getProperty("imeji.instance.url")); css_default = PropertyReader.getProperty("imeji.layout.css_default"); css_alternate = PropertyReader.getProperty("imeji.layout.css_alternate"); readBaseUri(); this.appName = PropertyReader.getProperty("imeji.instance.name"); } catch (Exception e) { throw new RuntimeException("Error reading properties: ", e); } }
From source file:AIR.ResourceBundler.Xml.FileSetInput.java
public String getRelative(String basePath, String targetPath) { targetPath = targetPath.replace('\\', '/'); basePath = basePath.replace('\\', '/'); String pathSeparator = "/"; // Normalize the paths String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath); String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath); // Undo the changes to the separators made by normalization normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath); 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++;//from ww w . j av a 2s . co m } if (commonIndex == 0) { // No single common path element. This most // likely indicates differing drive letters, like C: and D:. // These paths cannot be relativized. try { throw new Exception("No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + "'"); } catch (Exception e) { } } // 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:info.schnatterer.songbirdDbTools.Utils.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 . jav a 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 behavior when * running on Windows (for example) * @return <code>targetPath</code> relativized to <code>basePath</code> * * @author http://stackoverflow.com/questions/204784/how-to-construct-a-relative * -path-in-java-from-two-absolute-paths-or-urls */ public static String getRelativePath(final String basePath, final String targetPath, final 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; if (baseIsFile) { numDirsUp = base.length - commonIndex - 1; } else { numDirsUp = 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:com.apporiented.hermesftp.usermanager.model.PermissionData.java
/** * Fills the placeholders in the path template and checks if the passed path matches the * template./*from w w w. ja v a2 s .c om*/ * * @param checkPath The path to check. * @param ftproot The ftp root folder. * @param username The username. * @return True, if the path matches the configured pattern. * @throws FtpConfigException Error on reading or processing a configuration file. */ public boolean matches(String checkPath, String ftproot, String username) throws FtpConfigException { if (checkPath == null) { return false; } AntPathMatcher pathMatcher = new AntPathMatcher(); String antPath = replacePlaceholders(ftproot, username); antPath = FilenameUtils.normalizeNoEndSeparator(antPath); antPath = FilenameUtils.separatorsToUnix(antPath); checkPath = FilenameUtils.normalizeNoEndSeparator(checkPath); checkPath = FilenameUtils.separatorsToUnix(checkPath); return pathMatcher.match(antPath, checkPath); }
From source file:hudson.plugins.project_inheritance.util.PathMapping.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 * '\'./* ww w . j a v 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 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; } 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:de.mpg.imeji.presentation.servlet.FileServlet.java
@Override public void init() { try {/* www .j a v a 2s.c o m*/ storageController = new StorageController(); logger.info("ImageServlet initialized"); authorization = new Authorization(); navivation = new Navigation(); domain = StringHelper.normalizeURI(navivation.getDomain()); domain = domain.substring(0, domain.length() - 1); digilibUrl = PropertyReader.getProperty("digilib.imeji.instance.url"); if (digilibUrl != null && !digilibUrl.isEmpty()) digilibUrl = StringHelper.normalizeURI(digilibUrl); InternalStorageManager ism = new InternalStorageManager(); internalStorageRoot = FilenameUtils .getBaseName(FilenameUtils.normalizeNoEndSeparator(ism.getStoragePath())); } catch (Exception e) { throw new RuntimeException("Image servlet not initialized! " + e); } }