X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFilenameParts.java;h=339cefa6e10b52a91705e7d1fee3879024e27604;hb=b1f3e3bac931d7f309f60e50b18c435c65586de8;hp=6f7da93b02895e23544b482a1fa0f99369c5de1c;hpb=656e362ece73c630f032d8bf30322143a297d92e;p=jalview.git diff --git a/src/jalview/io/BackupFilenameParts.java b/src/jalview/io/BackupFilenameParts.java index 6f7da93..339cefa 100644 --- a/src/jalview/io/BackupFilenameParts.java +++ b/src/jalview/io/BackupFilenameParts.java @@ -1,20 +1,45 @@ +/* + * 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; - boolean isBackupFile; + private boolean isBackupFile; + + private BackupFilenameParts() + { + this.isBackupFile = false; + } public BackupFilenameParts(File file, String base, String template, int digits) @@ -25,6 +50,12 @@ public class BackupFilenameParts public BackupFilenameParts(String filename, String base, String template, int suggesteddigits) { + this(filename, base, template, suggesteddigits, false); + } + + public BackupFilenameParts(String filename, String base, String template, + int suggesteddigits, boolean extensionMatch) + { this.isBackupFile = false; int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER); @@ -39,11 +70,33 @@ public class BackupFilenameParts 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) && filename.length() >= minlength)) + if (!(filename.startsWith(base + templateStart) + && filename.endsWith(templateEnd) + && filename.length() >= minlength)) { // non-starter return; @@ -58,9 +111,13 @@ public class BackupFilenameParts if (filename.length() >= startLength + digits + endLength && filename.startsWith(base + templateStart) && filename.endsWith(templateEnd) - && numString.matches("[0-9]*")) + // 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 = numString.length() > 0 ? Integer.parseInt(numString) : 0; this.digits = digits; @@ -70,6 +127,29 @@ public class BackupFilenameParts } + 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() { return this.isBackupFile; @@ -79,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; + } }