X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFileUtils.java;h=e62a7d600892584996bed6bdff1aee8aec075757;hb=a7169b1c72607f3c9357195b4999869650a2a891;hp=63af0bc4d96e8840d4fa52200f73d096ce8f066c;hpb=988d7abeef3e08d01ff8ebb918325d95caae70b7;p=jalview.git diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 63af0bc..e62a7d6 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -12,10 +12,13 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.Collections; import java.util.EnumSet; import java.util.List; import java.util.stream.Collectors; +import jalview.bin.Console; + public class FileUtils { /* @@ -65,10 +68,11 @@ public class FileUtils String glob = "glob:" + parentDir.toString() + File.separator + rest; PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob); - int maxDepth = rest.contains("**") ? Integer.MAX_VALUE + int maxDepth = rest.contains("**") ? 1028 : (int) (rest.chars() .filter(ch -> ch == File.separatorChar).count()) + 1; + Files.walkFileTree(parentDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth, new SimpleFileVisitor() @@ -106,6 +110,7 @@ public class FileUtils files.add(f); } } + Collections.sort(files); return files; } @@ -123,4 +128,65 @@ public class FileUtils ? System.getProperty("user.home") + path.substring(1) : path; } + + /* + * This method returns the basename of File file + */ + public static String getBasename(File file) + { + return getBasenameOrExtension(file, false); + } + + /* + * This method returns the extension of File file. + */ + public static String getExtension(File file) + { + return getBasenameOrExtension(file, true); + } + + public static String getBasenameOrExtension(File file, boolean extension) + { + if (file == null) + return null; + + String value = null; + String filename = file.getName(); + int lastDot = filename.lastIndexOf('.'); + if (lastDot > 0) // don't truncate if starts with '.' + { + value = extension ? filename.substring(lastDot + 1) + : filename.substring(0, lastDot); + } + else + { + value = extension ? "" : filename; + } + return value; + } + + /* + * This method returns the dirname of the first --append or --open value. + * Used primarily for substitutions in output filenames. + */ + public static String getDirname(File file) + { + if (file == null) + return null; + + String dirname = null; + try + { + File p = file.getParentFile(); + File d = new File(substituteHomeDir(p.getPath())); + dirname = d.getCanonicalPath(); + } catch (IOException e) + { + Console.debug( + "Exception when getting dirname of '" + file.getPath() + "'", + e); + dirname = ""; + } + return dirname; + } }