X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFileUtils.java;h=e62a7d600892584996bed6bdff1aee8aec075757;hb=1624d4fc89f17c4a6cdb80b7d4cc37a095b6fe67;hp=9aa27f9b89b4fea446901be6c8a2363a79ab1aa8;hpb=72479cdc25ff4d23a23b38e5bb25e01cd18bcd45;p=jalview.git diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 9aa27f9..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 { /* @@ -26,9 +29,16 @@ public class FileUtils */ public static List getFilesFromGlob(String pattern) { + return getFilesFromGlob(pattern, true); + } + + public static List getFilesFromGlob(String pattern, + boolean allowSingleFilenameThatDoesNotExist) + { + pattern = substituteHomeDir(pattern); List files = new ArrayList<>(); /* - * For efficiency of the Files.walkFileTree, let's find the longest path that doesn't need globbing. + * For efficiency of the Files.walkFileTree(), let's find the longest path that doesn't need globbing. * We look for the first glob character * { ? and then look for the last File.separator before that. * Then we can reset the path to look at and shorten the globbing pattern. * Relative paths can be used in pattern, which work from the pwd (though these are converted into @@ -58,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() @@ -94,11 +105,12 @@ public class FileUtils { // no wildcards File f = new File(pattern); - if (f.exists()) + if (allowSingleFilenameThatDoesNotExist || f.exists()) { files.add(f); } } + Collections.sort(files); return files; } @@ -110,4 +122,71 @@ public class FileUtils .collect(Collectors.toList()); } -} \ No newline at end of file + public static String substituteHomeDir(String path) + { + return path.startsWith("~" + File.separator) + ? 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; + } +}