Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / io / BackupFilenameParts.java
1 package jalview.io;
2
3 import java.io.File;
4
5 public class BackupFilenameParts
6 {
7   private String base;
8
9   private String templateStart;
10
11   private int num;
12
13   private int digits;
14
15   private String templateEnd;
16
17   private boolean isBackupFile;
18
19   private BackupFilenameParts()
20   {
21     this.isBackupFile = false;
22   }
23
24   public BackupFilenameParts(File file, String base, String template,
25           int digits)
26   {
27     this(file.getName(), base, template, digits);
28   }
29
30   public BackupFilenameParts(String filename, String base, String template,
31           int suggesteddigits)
32   {
33     this(filename, base, template, suggesteddigits, false);
34   }
35
36   public BackupFilenameParts(String filename, String base, String template,
37           int suggesteddigits, boolean extensionMatch)
38   {
39     this.isBackupFile = false;
40
41     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
42     int digits = 0;
43     String templateStart = template;
44     String templateEnd = "";
45     if (numcharstart > -1)
46     {
47       templateStart = template.substring(0, numcharstart);
48       templateEnd = template.substring(
49               numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
50       digits = suggesteddigits;
51     }
52
53     String savedFilename = "";
54     // if extensionOnly is set then reset the filename to the last occurrence of the extension+templateStart and try the match
55     if (extensionMatch)
56     {
57       // only trying to match from extension onwards
58
59       int extensioncharstart = filename
60               .lastIndexOf('.' + base + templateStart);
61       if (extensioncharstart == -1)
62       {
63         return;
64       }
65
66       savedFilename = filename.substring(0, extensioncharstart + 1); // include
67                                                                      // the "."
68       filename = filename.substring(extensioncharstart + 1);
69     }
70
71     // full filename match
72
73     // calculate minimum length of a backup filename
74     int minlength = base.length() + template.length()
75             - BackupFiles.NUM_PLACEHOLDER.length() + digits;
76
77     if (!(filename.startsWith(base + templateStart)
78             && filename.endsWith(templateEnd)
79             && filename.length() >= minlength))
80     {
81       // non-starter
82       return;
83     }
84
85     int startLength = base.length() + templateStart.length();
86     int endLength = templateEnd.length();
87     String numString = numcharstart > -1
88             ? filename.substring(startLength, filename.length() - endLength)
89             : "";
90
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 + ",}")))
99     {
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;
106     }
107
108   }
109
110   public static BackupFilenameParts currentBackupFilenameParts(
111           String filename, String base, boolean extensionMatch)
112   {
113     BackupFilenameParts bfp = new BackupFilenameParts();
114     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
115             .getSavedBackupEntry();
116     String template = bfpe.suffix;
117     if (template == null)
118     {
119       return bfp;
120     }
121     int digits;
122     try
123     {
124       digits = bfpe.digits;
125     } catch (Exception e)
126     {
127       return bfp;
128     }
129     return new BackupFilenameParts(filename, base, template, digits,
130             extensionMatch);
131   }
132
133   public boolean isBackupFile()
134   {
135     return this.isBackupFile;
136   }
137
138   public int indexNum()
139   {
140     return this.num;
141   }
142
143   public static String getBackupFilename(int index, String base,
144           String template, int digits)
145   {
146     String numString = String.format("%0" + digits + "d", index);
147     String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER,
148             numString);
149     String backupfilename = base + backupSuffix;
150     return backupfilename;
151   }
152 }