Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / io / BackupFilenameParts.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
23 import java.io.File;
24
25 public class BackupFilenameParts
26 {
27   private String base;
28
29   private String templateStart;
30
31   private int num;
32
33   private int digits;
34
35   private String templateEnd;
36
37   private boolean isBackupFile;
38
39   private BackupFilenameParts()
40   {
41     this.isBackupFile = false;
42   }
43
44   public BackupFilenameParts(File file, String base, String template,
45           int digits)
46   {
47     this(file.getName(), base, template, digits);
48   }
49
50   public BackupFilenameParts(String filename, String base, String template,
51           int suggesteddigits)
52   {
53     this(filename, base, template, suggesteddigits, false);
54   }
55
56   public BackupFilenameParts(String filename, String base, String template,
57           int suggesteddigits, boolean extensionMatch)
58   {
59     this.isBackupFile = false;
60
61     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
62     int digits = 0;
63     String templateStart = template;
64     String templateEnd = "";
65     if (numcharstart > -1)
66     {
67       templateStart = template.substring(0, numcharstart);
68       templateEnd = template.substring(
69               numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
70       digits = suggesteddigits;
71     }
72
73     String savedFilename = "";
74     // if extensionOnly is set then reset the filename to the last occurrence of the extension+templateStart and try the match
75     if (extensionMatch)
76     {
77       // only trying to match from extension onwards
78
79       int extensioncharstart = filename
80               .lastIndexOf('.' + base + templateStart);
81       if (extensioncharstart == -1)
82       {
83         return;
84       }
85
86       savedFilename = filename.substring(0, extensioncharstart + 1); // include
87                                                                      // the "."
88       filename = filename.substring(extensioncharstart + 1);
89     }
90
91     // full filename match
92
93     // calculate minimum length of a backup filename
94     int minlength = base.length() + template.length()
95             - BackupFiles.NUM_PLACEHOLDER.length() + digits;
96
97     if (!(filename.startsWith(base + templateStart)
98             && filename.endsWith(templateEnd)
99             && filename.length() >= minlength))
100     {
101       // non-starter
102       return;
103     }
104
105     int startLength = base.length() + templateStart.length();
106     int endLength = templateEnd.length();
107     String numString = numcharstart > -1
108             ? filename.substring(startLength, filename.length() - endLength)
109             : "";
110
111     if (filename.length() >= startLength + digits + endLength
112             && filename.startsWith(base + templateStart)
113             && filename.endsWith(templateEnd)
114             // match exactly digits number of number-characters (numString
115             // should be all digits and at least the right length), or more than
116             // digits long with proviso it's not zero-leading.
117             && (numString.matches("[0-9]{" + digits + "}")
118                     || numString.matches("[1-9][0-9]{" + digits + ",}")))
119     {
120       this.base = extensionMatch ? savedFilename + base : base;
121       this.templateStart = templateStart;
122       this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0;
123       this.digits = digits;
124       this.templateEnd = templateEnd;
125       this.isBackupFile = true;
126     }
127
128   }
129
130   public static BackupFilenameParts currentBackupFilenameParts(
131           String filename, String base, boolean extensionMatch)
132   {
133     BackupFilenameParts bfp = new BackupFilenameParts();
134     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
135             .getSavedBackupEntry();
136     String template = bfpe.suffix;
137     if (template == null)
138     {
139       return bfp;
140     }
141     int digits;
142     try
143     {
144       digits = bfpe.digits;
145     } catch (Exception e)
146     {
147       return bfp;
148     }
149     return new BackupFilenameParts(filename, base, template, digits,
150             extensionMatch);
151   }
152
153   public boolean isBackupFile()
154   {
155     return this.isBackupFile;
156   }
157
158   public int indexNum()
159   {
160     return this.num;
161   }
162
163   public static String getBackupFilename(int index, String base,
164           String template, int digits)
165   {
166     String numString = String.format("%0" + digits + "d", index);
167     String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER,
168             numString);
169     String backupfilename = base + backupSuffix;
170     return backupfilename;
171   }
172 }