JAL-629 add an {extension} substitution
[jalview.git] / src / jalview / util / FileUtils.java
index 4d35cd2..e62a7d6 100644 (file)
@@ -130,26 +130,39 @@ public class FileUtils
   }
 
   /*
-   * This method returns the basename of the first --append or --open value. 
-   * Used primarily for substitutions in output filenames.
+   * This method returns the basename of File file
    */
   public static String getBasename(File file)
   {
+    return getBasenameOrExtension(file, false);
+  }
+
+  /*
+   * This method returns the extension of File file.
+   */
+  public static String getExtension(File file)
+  {
+    return getBasenameOrExtension(file, true);
+  }
+
+  public static String getBasenameOrExtension(File file, boolean extension)
+  {
     if (file == null)
       return null;
 
-    String basename = null;
+    String value = null;
     String filename = file.getName();
     int lastDot = filename.lastIndexOf('.');
     if (lastDot > 0) // don't truncate if starts with '.'
     {
-      basename = filename.substring(0, lastDot);
+      value = extension ? filename.substring(lastDot + 1)
+              : filename.substring(0, lastDot);
     }
     else
     {
-      basename = filename;
+      value = extension ? "" : filename;
     }
-    return basename;
+    return value;
   }
 
   /*