JAL-629 start of tests and more examples
[jalview.git] / src / jalview / util / FileUtils.java
1 package jalview.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.FileSystems;
6 import java.nio.file.FileVisitOption;
7 import java.nio.file.FileVisitResult;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.nio.file.PathMatcher;
11 import java.nio.file.Paths;
12 import java.nio.file.SimpleFileVisitor;
13 import java.nio.file.attribute.BasicFileAttributes;
14 import java.util.ArrayList;
15 import java.util.EnumSet;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 public class FileUtils
20 {
21   /*
22    * Given string glob pattern (see
23    * https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)
24    * ) return a List of Files that match the pattern.
25    * Note this is a java style glob, not necessarily a bash-style glob, though there are sufficient similarities. 
26    */
27   public static List<File> getFilesFromGlob(String pattern)
28   {
29     List<File> files = new ArrayList<>();
30     /*
31      * For efficiency of the Files.walkFileTree, let's find the longest path that doesn't need globbing.
32      * We look for the first glob character * { ? and then look for the last File.separator before that.
33      * Then we can reset the path to look at and shorten the globbing pattern.
34      * Relative paths can be used in pattern, which work from the pwd (though these are converted into
35      * full paths in the match). 
36      */
37     int firstGlobChar = -1;
38     boolean foundGlobChar = false;
39     for (char c : new char[] { '*', '{', '?' })
40     {
41       if (pattern.indexOf(c) > -1
42               && (pattern.indexOf(c) < firstGlobChar || !foundGlobChar))
43       {
44         firstGlobChar = pattern.indexOf(c);
45         foundGlobChar = true;
46       }
47     }
48     int lastFS = pattern.lastIndexOf(File.separatorChar, firstGlobChar);
49     if (foundGlobChar)
50     {
51       String pS = pattern.substring(0, lastFS + 1);
52       String rest = pattern.substring(lastFS + 1);
53       Path parentDir = Paths.get(pS).toAbsolutePath();
54       if (parentDir.toFile().exists())
55       {
56         try
57         {
58           String glob = "glob:" + parentDir.toString() + File.separator
59                   + rest;
60           PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob);
61           int maxDepth = rest.contains("**") ? Integer.MAX_VALUE
62                   : (int) (rest.chars()
63                           .filter(ch -> ch == File.separatorChar).count())
64                           + 1;
65           Files.walkFileTree(parentDir,
66                   EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth,
67                   new SimpleFileVisitor<Path>()
68                   {
69                     @Override
70                     public FileVisitResult visitFile(Path path,
71                             BasicFileAttributes attrs) throws IOException
72                     {
73                       if (pm.matches(path))
74                       {
75                         files.add(path.toFile());
76                       }
77                       return FileVisitResult.CONTINUE;
78                     }
79
80                     @Override
81                     public FileVisitResult visitFileFailed(Path file,
82                             IOException exc) throws IOException
83                     {
84                       return FileVisitResult.CONTINUE;
85                     }
86                   });
87         } catch (IOException e)
88         {
89           e.printStackTrace();
90         }
91       }
92     }
93     else
94     {
95       // no wildcards
96       File f = new File(pattern);
97       if (f.exists())
98       {
99         files.add(f);
100       }
101     }
102
103     return files;
104   }
105
106   public static List<String> getFilenamesFromGlob(String pattern)
107   {
108     // convert list of Files to list of File.getPath() Strings
109     return getFilesFromGlob(pattern).stream().map(f -> f.getPath())
110             .collect(Collectors.toList());
111   }
112
113 }