JAL-629 added setprop. More filename based substitutions enabled. Test for these.
[jalview.git] / src / jalview / util / FileUtils.java
index 63af0bc..b6f4e90 100644 (file)
@@ -16,6 +16,8 @@ import java.util.EnumSet;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import jalview.bin.Console;
+
 public class FileUtils
 {
   /*
@@ -123,4 +125,50 @@ public class FileUtils
             ? 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;
+  }
 }