/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. To do this it uses * {@link #indexOfLastSeparator(String)} which will handle a file in either Unix or Windows format. * </p> * <p> * The output will be the same irrespective of the machine that the code is running on. * </p> * * @param filename * the filename to find the last extension separator in, null returns -1 * @return the index of the last extension separator character, or -1 if there is no such character */ public static int indexOfExtension(final String filename) { if (filename == null) { return NOT_FOUND; } final int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); final int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? NOT_FOUND : extensionPos; }
/** * Gets the name minus the path from a full filename. * <p> * This method will handle a file in either Unix or Windows format. * The text after the last forward or backslash is returned. * <pre> * a/b/c.txt --> c.txt * a.txt --> a.txt * a/b/c --> c * a/b/c/ --> "" * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to query, null returns null * @return the name of the file without the path, or an empty string if none exists. * Null bytes inside string will be removed */ public static String getName(final String filename) { if (filename == null) { return null; } failIfNullBytePresent(filename); final int index = indexOfLastSeparator(filename); return filename.substring(index + 1); }
/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. * To do this it uses {@link #indexOfLastSeparator(String)} which will * handle a file in either Unix or Windows format. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return (lastSeparator > extensionPos ? -1 : extensionPos); }
/** * Gets the name minus the path from a full filename. * <p> * This method will handle a file in either Unix or Windows format. * The text after the last forward or backslash is returned. * <pre> * a/b/c.txt --> c.txt * a.txt --> a.txt * a/b/c --> c * a/b/c/ --> "" * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to query, null returns null * @return the name of the file without the path, or an empty string if none exists */ public static String getName(String filename) { if (filename == null) { return null; } int index = indexOfLastSeparator(filename); return filename.substring(index + 1); }
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path. Null bytes inside string will be removed */ private static String doGetPath(final String filename, final int separatorAdd) { if (filename == null) { return null; } final int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } final int index = indexOfLastSeparator(filename); final int endIndex = index+separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return ""; } final String path = filename.substring(prefix, endIndex); failIfNullBytePresent(path); return path; }
final int index = indexOfLastSeparator(filename); if (index < 0) { return filename.substring(0, prefix);
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path */ private static String doGetPath(String filename, int separatorAdd) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } int index = indexOfLastSeparator(filename); if (prefix >= filename.length() || index < 0) { return ""; } return filename.substring(prefix, index + separatorAdd); }
@Test public void testIndexOfLastSeparator() { assertEquals(-1, FilenameUtils.indexOfLastSeparator(null)); assertEquals(-1, FilenameUtils.indexOfLastSeparator("noseperator.inthispath")); assertEquals(3, FilenameUtils.indexOfLastSeparator("a/b/c")); assertEquals(3, FilenameUtils.indexOfLastSeparator("a\\b\\c")); }
/** * Does the work of getting the path. * * @param filename the filename * @param includeSeparator true to include the end separator * @return the path */ private static String doGetFullPath(String filename, boolean includeSeparator) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } if (prefix >= filename.length()) { if (includeSeparator) { return getPrefix(filename); // add end slash if necessary } else { return filename; } } int index = indexOfLastSeparator(filename); if (index < 0) { return filename.substring(0, prefix); } int end = index + (includeSeparator ? 1 : 0); return filename.substring(0, end); }
public String resolvePath(String parentName, String templateName, String basePath, String extension) { // Path currentPath = Paths.get(filename); // Path templatePath = Paths.get(templateName); // Path parent = currentPath.getParent(); // String filePath = templatePath.toString(); // if(parent!=null) // filePath = parent.resolve(templatePath).toString(); String filePath; if(templateName.startsWith("/")) { filePath = basePath + templateName; }else { if (FilenameUtils.indexOfLastSeparator(parentName) == -1) filePath = templateName; else { // String currentDir = FilenameUtils.getFullPath(parentName); String currentDir = parentName.substring(0, FilenameUtils.indexOfLastSeparator(parentName) + 1); filePath = currentDir + templateName; } } if(StringUtils.lastIndexOf(filePath,"/") >= StringUtils.lastIndexOf(filePath,".")) filePath += "."+extension; filePath = FilenameUtils.normalize(filePath); return filePath; } }
/** * Returns the index of the last directory separator character. * <p/> * The position of the last forward or backslash is returned. * <p/> * * @param filename * the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there is no such character */ public static int indexOfLastSeparator( final String filename ) { return FilenameUtils.indexOfLastSeparator( filename ); }
/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. * To do this it uses {@link #indexOfLastSeparator(String)} which will * handle a file in either Unix or Windows format. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return (lastSeparator > extensionPos ? -1 : extensionPos); }
/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. * To do this it uses {@link #indexOfLastSeparator(String)} which will * handle a file in either Unix or Windows format. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; }
/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. * To do this it uses {@link #indexOfLastSeparator(String)} which will * handle a file in either Unix or Windows format. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; }
/** * This method fixes the problem when a resource in a group uses deep wildcard and starts at the root. * <p/> * Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>. */ private String getFullPathNoEndSeparator(final String uri) { String result = FilenameUtils.getFullPathNoEndSeparator(uri); if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result)) return ""; return result; } };
/** * * @param path The path of a directory * @return String[] a string array containing the parent folders, each of * which is a path in its own right (not just the names of each individual folder) */ public static List<String> getAncestorPaths(String path) { ArrayList<String> paths = new ArrayList<String>(); if (path != null && !path.isEmpty()) { String processPath = path; int lastSeparator = processPath.length() - 1; while (lastSeparator >= 0) { String separator = path.substring(lastSeparator, lastSeparator + 1); processPath = processPath.substring(0, lastSeparator); paths.add(processPath + separator); lastSeparator = FilenameUtils.indexOfLastSeparator(processPath); } } return paths; }
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path */ private static String doGetPath(String filename, int separatorAdd) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } int index = indexOfLastSeparator(filename); int endIndex = index+separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return ""; } return filename.substring(prefix, endIndex); }
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path */ private static String doGetPath(String filename, int separatorAdd) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } int index = indexOfLastSeparator(filename); int endIndex = index+separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return ""; } return filename.substring(prefix, endIndex); }
/** * This method fixes the problem when a resource in a group uses deep wildcard and starts at the root. * <p/> * Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>. */ private String getFullPathNoEndSeparator(final Resource resource1) { final String result = FilenameUtils.getFullPathNoEndSeparator(resource1.getUri()); if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result)) { return ""; } return result; } };
/** * This method fixes the problem when a resource in a group uses deep wildcard and starts at the root. * <p/> * Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>. */ private String getFullPathNoEndSeparator(final Resource resource) { final String result = FilenameUtils.getFullPathNoEndSeparator(resource.getUri()); if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result)) { return ""; } return result; } };