JAL-4277 Corrected choice of all or last opened linkedIds. Better wildcard syntactic...
[jalview.git] / src / jalview / util / FileUtils.java
index e62a7d6..79cac0a 100644 (file)
@@ -17,8 +17,6 @@ import java.util.EnumSet;
 import java.util.List;
 import java.util.stream.Collectors;
 
-import jalview.bin.Console;
-
 public class FileUtils
 {
   /*
@@ -36,6 +34,8 @@ public class FileUtils
           boolean allowSingleFilenameThatDoesNotExist)
   {
     pattern = substituteHomeDir(pattern);
+    String relativePattern = pattern.startsWith(File.separator) ? null
+            : pattern;
     List<File> files = new ArrayList<>();
     /*
      * For efficiency of the Files.walkFileTree(), let's find the longest path that doesn't need globbing.
@@ -60,7 +60,11 @@ public class FileUtils
     {
       String pS = pattern.substring(0, lastFS + 1);
       String rest = pattern.substring(lastFS + 1);
-      Path parentDir = Paths.get(pS).toAbsolutePath();
+      if ("".equals(pS))
+      {
+        pS = ".";
+      }
+      Path parentDir = Paths.get(pS);
       if (parentDir.toFile().exists())
       {
         try
@@ -175,18 +179,51 @@ public class FileUtils
       return null;
 
     String dirname = null;
-    try
+    File p = file.getParentFile();
+    if (p == null)
     {
-      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 = "";
+      p = new File(".");
     }
+    File d = new File(substituteHomeDir(p.getPath()));
+    dirname = d.getPath();
     return dirname;
   }
+
+  public static String convertWildcardsToPath(String value, String wildcard,
+          String dirname, String basename)
+  {
+    if (value == null)
+    {
+      return null;
+    }
+    StringBuilder path = new StringBuilder();
+    int lastFileSeparatorIndex = value.lastIndexOf(File.separatorChar);
+    int wildcardBeforeIndex = value.indexOf(wildcard);
+    if (lastFileSeparatorIndex > wildcard.length() - 1
+            && wildcardBeforeIndex < lastFileSeparatorIndex)
+    {
+      path.append(value.substring(0, wildcardBeforeIndex));
+      path.append(dirname);
+      path.append(value.substring(wildcardBeforeIndex + wildcard.length(),
+              lastFileSeparatorIndex + 1));
+    }
+    else
+    {
+      path.append(value.substring(0, lastFileSeparatorIndex + 1));
+    }
+    int wildcardAfterIndex = value.indexOf(wildcard,
+            lastFileSeparatorIndex);
+    if (wildcardAfterIndex > lastFileSeparatorIndex)
+    {
+      path.append(value.substring(lastFileSeparatorIndex + 1,
+              wildcardAfterIndex));
+      path.append(basename);
+      path.append(value.substring(wildcardAfterIndex + wildcard.length()));
+    }
+    else
+    {
+      path.append(value.substring(lastFileSeparatorIndex + 1));
+    }
+    return path.toString();
+  }
 }