X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFilenameParts.java;h=339cefa6e10b52a91705e7d1fee3879024e27604;hb=121a4b153bdd56b15ff2c528e6b84219affc09d5;hp=b9f766e658c4fadf1a8bef97b0c71afad7a337bb;hpb=05c12ef9626200844bf5067056f03e84090565b3;p=jalview.git diff --git a/src/jalview/io/BackupFilenameParts.java b/src/jalview/io/BackupFilenameParts.java index b9f766e..339cefa 100644 --- a/src/jalview/io/BackupFilenameParts.java +++ b/src/jalview/io/BackupFilenameParts.java @@ -1,47 +1,65 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.io; import java.io.File; public class BackupFilenameParts { - String base; + private String base; - String templateStart; + private String templateStart; - int num; + private int num; - int digits; + private int digits; - String templateEnd; + private String templateEnd; - String extension; + private boolean isBackupFile; - boolean isBackupFile; + private BackupFilenameParts() + { + this.isBackupFile = false; + } - public BackupFilenameParts(File file, String base, String template, int digits, - String extension) + public BackupFilenameParts(File file, String base, String template, + int digits) { - this(file.getName(), base, template, digits, extension); + this(file.getName(), base, template, digits); } public BackupFilenameParts(String filename, String base, String template, - int digits, String extension) + int suggesteddigits) { - this.isBackupFile = false; - - // calculate minimum length of a backup filename - int minlength = base.length() + template.length() - - BackupFiles.NUM_PLACEHOLDER.length() + digits - + extension.length(); + this(filename, base, template, suggesteddigits, false); + } - if (!(filename.startsWith(base) && filename.endsWith(extension) - && filename.length() >= minlength)) - { - // non-starter - return; - } + public BackupFilenameParts(String filename, String base, String template, + int suggesteddigits, boolean extensionMatch) + { + this.isBackupFile = false; int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER); + int digits = 0; String templateStart = template; String templateEnd = ""; if (numcharstart > -1) @@ -49,27 +67,87 @@ public class BackupFilenameParts templateStart = template.substring(0, numcharstart); templateEnd = template.substring( numcharstart + BackupFiles.NUM_PLACEHOLDER.length()); + digits = suggesteddigits; } - + + String savedFilename = ""; + // if extensionOnly is set then reset the filename to the last occurrence of the extension+templateStart and try the match + if (extensionMatch) + { + // only trying to match from extension onwards + + int extensioncharstart = filename + .lastIndexOf('.' + base + templateStart); + if (extensioncharstart == -1) + { + return; + } + + savedFilename = filename.substring(0, extensioncharstart + 1); // include + // the "." + filename = filename.substring(extensioncharstart + 1); + } + + // full filename match + + // calculate minimum length of a backup filename + int minlength = base.length() + template.length() + - BackupFiles.NUM_PLACEHOLDER.length() + digits; + + if (!(filename.startsWith(base + templateStart) + && filename.endsWith(templateEnd) + && filename.length() >= minlength)) + { + // non-starter + return; + } + int startLength = base.length() + templateStart.length(); - int endLength = templateEnd.length() + extension.length(); - String numString = filename.substring(startLength, - filename.length() - endLength); + int endLength = templateEnd.length(); + String numString = numcharstart > -1 + ? filename.substring(startLength, filename.length() - endLength) + : ""; if (filename.length() >= startLength + digits + endLength && filename.startsWith(base + templateStart) - && filename.endsWith(templateEnd + extension) - && numString.matches("[0-9]+")) + && filename.endsWith(templateEnd) + // match exactly digits number of number-characters (numString + // should be all digits and at least the right length), or more than + // digits long with proviso it's not zero-leading. + && (numString.matches("[0-9]{" + digits + "}") + || numString.matches("[1-9][0-9]{" + digits + ",}"))) { - this.base = base; + this.base = extensionMatch ? savedFilename + base : base; this.templateStart = templateStart; - this.num = Integer.parseInt(numString); + this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0; this.digits = digits; - this.templateStart = templateStart; this.templateEnd = templateEnd; this.isBackupFile = true; } - + + } + + public static BackupFilenameParts currentBackupFilenameParts( + String filename, String base, boolean extensionMatch) + { + BackupFilenameParts bfp = new BackupFilenameParts(); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry + .getSavedBackupEntry(); + String template = bfpe.suffix; + if (template == null) + { + return bfp; + } + int digits; + try + { + digits = bfpe.digits; + } catch (Exception e) + { + return bfp; + } + return new BackupFilenameParts(filename, base, template, digits, + extensionMatch); } public boolean isBackupFile() @@ -81,4 +159,14 @@ public class BackupFilenameParts { return this.num; } + + public static String getBackupFilename(int index, String base, + String template, int digits) + { + String numString = String.format("%0" + digits + "d", index); + String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER, + numString); + String backupfilename = base + backupSuffix; + return backupfilename; + } }