JAL-629 sort files from java glob for predictability. reduce maxdepth of walkfiletree
[jalview.git] / src / jalview / util / FileUtils.java
index 9aa27f9..f5b3701 100644 (file)
@@ -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<File> getFilesFromGlob(String pattern)
   {
+    return getFilesFromGlob(pattern, true);
+  }
+
+  public static List<File> getFilesFromGlob(String pattern,
+          boolean allowSingleFilenameThatDoesNotExist)
+  {
+    pattern = substituteHomeDir(pattern);
     List<File> 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<Path>()
@@ -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,56 @@ 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 the first --open or --opennew value. 
+   * Used primarily for substitutions in output filenames.
+   */
+  public static String getBasename(File file)
+  {
+    if (file == null)
+      return null;
+
+    String basename = null;
+    String filename = file.getName();
+    int lastDot = filename.lastIndexOf('.');
+    if (lastDot > 0) // don't truncate if starts with '.'
+    {
+      basename = filename.substring(0, lastDot);
+    }
+    else
+    {
+      basename = filename;
+    }
+    return basename;
+  }
+
+  /*
+   * This method returns the dirname of the first --open or --opennew value. 
+   * Used primarily for substitutions in output filenames.
+   */
+  public static String getDirname(File file)
+  {
+    if (file == null)
+      return null;
+
+    String dirname = null;
+    try
+    {
+      dirname = file.getParentFile().getCanonicalPath();
+    } catch (IOException e)
+    {
+      Console.debug(
+              "Exception when getting dirname of '" + file.getPath() + "'",
+              e);
+      dirname = "";
+    }
+    return dirname;
+  }
+}