From d0410b0ca6504922a43f7b550424bff655a62682 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 23 Oct 2018 18:03:52 +0100 Subject: [PATCH] BackupFilter and other unfinished business --- src/jalview/io/BackupFileFilter.java | 60 ++++++++++++++++++++++++++++++++++ src/jalview/io/BackupFiles.java | 52 +++++++++++++---------------- 2 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 src/jalview/io/BackupFileFilter.java diff --git a/src/jalview/io/BackupFileFilter.java b/src/jalview/io/BackupFileFilter.java new file mode 100644 index 0000000..647e250 --- /dev/null +++ b/src/jalview/io/BackupFileFilter.java @@ -0,0 +1,60 @@ +package jalview.io; + +import java.io.File; +import java.io.FilenameFilter; + +public class BackupFileFilter implements FilenameFilter +{ + + public String base; + + public String template; + + public int digits; + + public String extension; + + public BackupFileFilter(String base, String template, int digits, + String extension) + { + this.base = base; + this.template = template; + this.digits = digits; + this.extension = extension; + } + + @Override + public boolean accept(File file, String filename) + { + if (file.isDirectory()) + { + return true; + } + else + { + int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER); + String templateStart = template; + String templateEnd = ""; + if (numcharstart > -1) + { + templateStart = template.substring(0, numcharstart); + templateEnd = template.substring(numcharstart + BackupFiles.NUM_PLACEHOLDER.length()); + } + int startLength = base.length() + templateStart.length(); + int endLength = templateEnd.length() + extension.length(); + if (filename.length() == startLength + digits + endLength + && filename.startsWith(base + templateStart) + && filename.endsWith(templateEnd + extension) + && filename + .substring(startLength, + filename.length() - endLength + 1) + .matches("[0-9]+")) + { + return true; + } + + } + return false; + } + +} diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 2d75746..da5209b 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -19,6 +19,8 @@ public class BackupFiles public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS"; + protected static String NUM_PLACEHOLDER = "%n"; + public static String REVERSE_ORDER = NS + "_REVERSE_ORDER"; private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS; @@ -28,20 +30,20 @@ public class BackupFiles // enabled - default flag as to whether to do the backup file roll (if not // defined in preferences) - private boolean enabled = true; + private boolean enabled; // defaultSuffix - default template to use to append to basename of file - private String suffix = "-v%n"; + private String suffix; // defaultMax - default max number of backup files - private int max = 4; + private int max; // defaultDigits - number of zero-led digits to use in the filename - private int digits = 2; + private int digits; // reverseOrder - set to true to make newest (latest) files lowest number // (like rolled log files) - private boolean reverseOrder = false; + private boolean reverseOrder; // temp saved file to become new saved file private File tempFile; @@ -55,7 +57,7 @@ public class BackupFiles // REVERSE_ORDER public BackupFiles(File file) { - this(file, true, "-v%n", 4, 2, false); + this(file, true, "-v" + NUM_PLACEHOLDER, 4, 3, false); } // set, get and rename temp file into place @@ -74,7 +76,7 @@ public class BackupFiles return tempFile.renameTo(file); } - protected BackupFiles(File file, boolean defaultEnabled, + public BackupFiles(File file, boolean defaultEnabled, String defaultSuffix, int defaultMax, int defaultDigits, boolean defaultReverseOrder) { @@ -126,9 +128,10 @@ public class BackupFiles // split filename up to insert suffix template in the right place. template // and backupMax can be set in .jalview_properties String dir = ""; + File dirFile; try { - File dirFile = file.getParentFile(); + dirFile = file.getParentFile(); dir = dirFile.getCanonicalPath(); } catch (Exception e) { @@ -160,7 +163,7 @@ public class BackupFiles { int n = max - m; numString = String.format("%0" + digits + "d", n); - String backupSuffix = suffix.replaceAll("%n", numString); + String backupSuffix = suffix.replaceAll(NUM_PLACEHOLDER, numString); String backupfilename = dir + File.separatorChar + basename + backupSuffix + extension; File backupfile_n = new File(backupfilename); @@ -197,33 +200,24 @@ public class BackupFiles lastfile = backupfile_n; } + // now actually backup the important file! + ret = ret && file.renameTo(lastfile); } else { - // version style numbering (with file rolling though) + // version style numbering (with earliest file deletion if max files + // reached) - // check if all backup files exist - int largest = 0; - for (int m = 0; m < max; m++) - { - int n = m + 1; - numString = String.format("%0" + digits + "d", n); - String backupSuffix = suffix.replaceAll("%n", numString); - String backupfilename = dir + File.separatorChar + basename - + backupSuffix + extension; - File backupfile_n = new File(backupfilename); - if (backupfile_n.exists()) - { - largest = n; - } - } - // MORE CODE HERE BEN! - } + // find existing backup files + BackupFileFilter bff = new BackupFileFilter(basename, suffix, digits, + extension); + File[] backupFiles = dirFile.listFiles(bff); - // now actually backup the important file! - ret = ret && file.renameTo(lastfile); + + } return ret; } } + -- 1.7.10.2