From 5c49150fcafd9726b4814557332ae2f60c0c46a2 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Sun, 14 May 2023 12:22:02 +0100 Subject: [PATCH] JAL-629 add an {extension} substitution --- src/jalview/bin/argparser/ArgParser.java | 9 +++++++++ src/jalview/bin/argparser/ArgValuesMap.java | 22 ++++++++++++++++++---- src/jalview/util/FileUtils.java | 25 +++++++++++++++++++------ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/jalview/bin/argparser/ArgParser.java b/src/jalview/bin/argparser/ArgParser.java index 7132e89..2a3272c 100644 --- a/src/jalview/bin/argparser/ArgParser.java +++ b/src/jalview/bin/argparser/ArgParser.java @@ -81,6 +81,11 @@ public class ArgParser // the linked id substitution string used to use the idCounter private static final String LINKEDIDAUTOCOUNTER = "{n}"; + // the linked id substitution string used to use the filename extension of + // --append + // or --open + private static final String LINKEDIDEXTENSION = "{extension}"; + // the linked id substitution string used to use the base filename of --append // or --open private static final String LINKEDIDBASENAME = "{basename}"; @@ -637,6 +642,10 @@ public class ArgParser { rest = rest.replace(LINKEDIDBASENAME, avm.getBasename()); } + if (rest.contains(LINKEDIDEXTENSION)) + { + rest = rest.replace(LINKEDIDEXTENSION, avm.getExtension()); + } if (rest.contains(LINKEDIDDIRNAME)) { rest = rest.replace(LINKEDIDDIRNAME, avm.getDirname()); diff --git a/src/jalview/bin/argparser/ArgValuesMap.java b/src/jalview/bin/argparser/ArgValuesMap.java index 158041b..99a1ef6 100644 --- a/src/jalview/bin/argparser/ArgValuesMap.java +++ b/src/jalview/bin/argparser/ArgValuesMap.java @@ -209,7 +209,16 @@ public class ArgValuesMap */ public String getBasename() { - return getDirOrBasename(false); + return getDirBasenameOrExtension(false, false); + } + + /* + * This method returns the basename of the first --append or --open value. + * Used primarily for substitutions in output filenames. + */ + public String getExtension() + { + return getDirBasenameOrExtension(false, true); } /* @@ -218,10 +227,11 @@ public class ArgValuesMap */ public String getDirname() { - return getDirOrBasename(true); + return getDirBasenameOrExtension(true, false); } - public String getDirOrBasename(boolean dirname) + public String getDirBasenameOrExtension(boolean dirname, + boolean extension) { String filename = null; String appendVal = getValue(Arg.APPEND); @@ -234,7 +244,11 @@ public class ArgValuesMap return null; File file = new File(filename); - return dirname ? FileUtils.getDirname(file) + if (dirname) + { + return FileUtils.getDirname(file); + } + return extension ? FileUtils.getExtension(file) : FileUtils.getBasename(file); } diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 4d35cd2..e62a7d6 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -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; } /* -- 1.7.10.2