--- /dev/null
+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;
+ }
+
+}
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;
// 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;
// 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
return tempFile.renameTo(file);
}
- protected BackupFiles(File file, boolean defaultEnabled,
+ public BackupFiles(File file, boolean defaultEnabled,
String defaultSuffix,
int defaultMax, int defaultDigits, boolean defaultReverseOrder)
{
// 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)
{
{
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);
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;
}
}
+