2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
25 public class BackupFilenameParts
29 private String templateStart;
35 private String templateEnd;
37 private boolean isBackupFile;
39 private BackupFilenameParts()
41 this.isBackupFile = false;
44 public BackupFilenameParts(File file, String base, String template,
47 this(file.getName(), base, template, digits);
50 public BackupFilenameParts(String filename, String base, String template,
53 this(filename, base, template, suggesteddigits, false);
56 public BackupFilenameParts(String filename, String base, String template,
57 int suggesteddigits, boolean extensionMatch)
59 this.isBackupFile = false;
61 int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
63 String templateStart = template;
64 String templateEnd = "";
65 if (numcharstart > -1)
67 templateStart = template.substring(0, numcharstart);
68 templateEnd = template.substring(
69 numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
70 digits = suggesteddigits;
73 String savedFilename = "";
74 // if extensionOnly is set then reset the filename to the last occurrence of
75 // the extension+templateStart and try the match
78 // only trying to match from extension onwards
80 int extensioncharstart = filename
81 .lastIndexOf('.' + base + templateStart);
82 if (extensioncharstart == -1)
87 savedFilename = filename.substring(0, extensioncharstart + 1); // include
89 filename = filename.substring(extensioncharstart + 1);
92 // full filename match
94 // calculate minimum length of a backup filename
95 int minlength = base.length() + template.length()
96 - BackupFiles.NUM_PLACEHOLDER.length() + digits;
98 if (!(filename.startsWith(base + templateStart)
99 && filename.endsWith(templateEnd)
100 && filename.length() >= minlength))
106 int startLength = base.length() + templateStart.length();
107 int endLength = templateEnd.length();
108 String numString = numcharstart > -1
109 ? filename.substring(startLength, filename.length() - endLength)
112 if (filename.length() >= startLength + digits + endLength
113 && filename.startsWith(base + templateStart)
114 && filename.endsWith(templateEnd)
115 // match exactly digits number of number-characters (numString
116 // should be all digits and at least the right length), or more than
117 // digits long with proviso it's not zero-leading.
118 && (numString.matches("[0-9]{" + digits + "}")
119 || numString.matches("[1-9][0-9]{" + digits + ",}")))
121 this.base = extensionMatch ? savedFilename + base : base;
122 this.templateStart = templateStart;
123 this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0;
124 this.digits = digits;
125 this.templateEnd = templateEnd;
126 this.isBackupFile = true;
131 public static BackupFilenameParts currentBackupFilenameParts(
132 String filename, String base, boolean extensionMatch)
134 BackupFilenameParts bfp = new BackupFilenameParts();
135 BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
136 .getSavedBackupEntry();
137 String template = bfpe.suffix;
138 if (template == null)
145 digits = bfpe.digits;
146 } catch (Exception e)
150 return new BackupFilenameParts(filename, base, template, digits,
154 public boolean isBackupFile()
156 return this.isBackupFile;
159 public int indexNum()
164 public static String getBackupFilename(int index, String base,
165 String template, int digits)
167 String numString = String.format("%0" + digits + "d", index);
168 String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER,
170 String backupfilename = base + backupSuffix;
171 return backupfilename;