5 public class BackupFilenameParts
9 private String templateStart;
15 private String templateEnd;
17 private boolean isBackupFile;
19 private BackupFilenameParts()
21 this.isBackupFile = false;
24 public BackupFilenameParts(File file, String base, String template,
27 this(file.getName(), base, template, digits);
30 public BackupFilenameParts(String filename, String base, String template,
33 this(filename, base, template, suggesteddigits, false);
36 public BackupFilenameParts(String filename, String base, String template,
37 int suggesteddigits, boolean extensionMatch)
39 this.isBackupFile = false;
41 int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
43 String templateStart = template;
44 String templateEnd = "";
45 if (numcharstart > -1)
47 templateStart = template.substring(0, numcharstart);
48 templateEnd = template.substring(
49 numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
50 digits = suggesteddigits;
53 String savedFilename = "";
54 // if extensionOnly is set then reset the filename to the last occurrence of the extension+templateStart and try the match
57 // only trying to match from extension onwards
59 int extensioncharstart = filename
60 .lastIndexOf('.' + base + templateStart);
61 if (extensioncharstart == -1)
66 savedFilename = filename.substring(0, extensioncharstart + 1); // include
68 filename = filename.substring(extensioncharstart + 1);
71 // full filename match
73 // calculate minimum length of a backup filename
74 int minlength = base.length() + template.length()
75 - BackupFiles.NUM_PLACEHOLDER.length() + digits;
77 if (!(filename.startsWith(base + templateStart)
78 && filename.endsWith(templateEnd)
79 && filename.length() >= minlength))
85 int startLength = base.length() + templateStart.length();
86 int endLength = templateEnd.length();
87 String numString = numcharstart > -1
88 ? filename.substring(startLength, filename.length() - endLength)
91 if (filename.length() >= startLength + digits + endLength
92 && filename.startsWith(base + templateStart)
93 && filename.endsWith(templateEnd)
94 // match exactly digits number of number-characters (numString
95 // should be all digits and at least the right length), or more than
96 // digits long with proviso it's not zero-leading.
97 && (numString.matches("[0-9]{" + digits + "}")
98 || numString.matches("[1-9][0-9]{" + digits + ",}")))
100 this.base = extensionMatch ? savedFilename + base : base;
101 this.templateStart = templateStart;
102 this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0;
103 this.digits = digits;
104 this.templateEnd = templateEnd;
105 this.isBackupFile = true;
110 public static BackupFilenameParts currentBackupFilenameParts(
111 String filename, String base, boolean extensionMatch)
113 BackupFilenameParts bfp = new BackupFilenameParts();
114 BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
115 .getSavedBackupEntry();
116 String template = bfpe.suffix;
117 if (template == null)
124 digits = bfpe.digits;
125 } catch (Exception e)
129 return new BackupFilenameParts(filename, base, template, digits,
133 public boolean isBackupFile()
135 return this.isBackupFile;
138 public int indexNum()
143 public static String getBackupFilename(int index, String base,
144 String template, int digits)
146 String numString = String.format("%0" + digits + "d", index);
147 String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER,
149 String backupfilename = base + backupSuffix;
150 return backupfilename;