import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
import jalview.bin.Console;
return hasExtension ? filename0.substring(0, dot + 1) : filename0;
}
}
+
+ /**
+ * Look for files that use a template, with two "%s" formatting entries, to
+ * look for files with the template substituted with combinations of root and
+ * version. If versionWhitelist is not null then these paths will be added. If
+ * versionBlacklist is not null then globbed versions will be looked for and
+ * these versions excluded. If both are given then both will be included. If
+ * separator is not null then it will be added before the version number, and
+ * additionally a path without the separator and version will be looked for or
+ * added if the whitelist or blacklist are supplied respectively.
+ *
+ * @param templates
+ * @param roots
+ * @param versionWhitelist
+ * @param versionBlacklist
+ * @param separator
+ * @return
+ */
+ public static List<File> getMatchingVersionedFiles(String[] templates,
+ String[] roots, String[] versionWhitelist,
+ String[] versionBlacklist, String separator)
+ {
+ Set<File> matchingFiles = new HashSet<>();
+ if (templates == null)
+ {
+ Console.debug(
+ "getMatchingVersionedFiles called with a null template array");
+ List<File> files = new ArrayList<File>();
+ files.addAll(matchingFiles);
+ return files;
+ }
+
+ Console.debug("Looking for all versions with a blacklist of size "
+ + versionBlacklist.length);
+ for (String template : templates)
+ {
+ Console.debug("Using template '" + template + "'");
+ for (String root : roots)
+ {
+ Console.debug("Using root '" + root + "'");
+ if (versionBlacklist != null)
+ {
+ String globMatch = String.format(template, root, "*");
+ Console.debug("Using glob '" + globMatch + "'");
+ List<File> foundFiles = FileUtils.getFilesFromGlob(globMatch,
+ false);
+ for (File found : foundFiles)
+ {
+ Console.debug("Checking " + found.getPath() + " is okay");
+ boolean add = true;
+ for (String notVersion : versionBlacklist)
+ {
+ StringBuilder vSB = new StringBuilder();
+ if (separator != null)
+ {
+ vSB.append(separator);
+ }
+ vSB.append(notVersion);
+ String versionString = vSB.toString();
+ if (String.format(template, root, versionString)
+ .equals(found.getPath()))
+ {
+ add = false;
+ Console.debug(
+ "Not adding " + found.getPath() + ": version '"
+ + notVersion + "' is in the blacklist");
+ break;
+ }
+ }
+ if (add)
+ {
+ Console.debug("Adding " + found.getPath() + " to list");
+ matchingFiles.add(found);
+ }
+ }
+
+ if (separator != null)
+ {
+ // try without a version number too
+ String nonVersioned = String.format(template, root, "");
+ matchingFiles.addAll(
+ FileUtils.getFilesFromGlob(nonVersioned, false));
+ }
+ }
+
+ if (versionWhitelist != null)
+ {
+ Console.debug("Adding " + versionWhitelist.length
+ + " whitelist versions");
+ for (String addVersion : versionWhitelist)
+ {
+ StringBuilder vSB = new StringBuilder();
+ if (separator != null)
+ {
+ vSB.append(separator);
+ }
+ vSB.append(addVersion);
+ String versionString = vSB.toString();
+ String versionPath = String.format(template, root,
+ versionString);
+ Console.debug("Adding whitelist path '" + versionPath + "'");
+ matchingFiles.add(new File(versionPath));
+ }
+
+ if (separator != null)
+ {
+ // try without a version number too
+ String nonVersioned = String.format(template, root, "");
+ matchingFiles.add(new File(nonVersioned));
+ }
+ }
+ }
+ }
+
+ List<File> files = new ArrayList<File>();
+ files.addAll(matchingFiles);
+ return files;
+ }
+
}