JAL-3141 Ostensibly all backups working for alignment and project saves. Need to...
[jalview.git] / src / jalview / io / BackupFilenameFilter.java
1 package jalview.io;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6
7 public class BackupFilenameFilter implements FilenameFilter
8 {
9
10   public String base;
11
12   public String template;
13
14   public int digits;
15
16   public String extension;
17
18   public BackupFilenameFilter(String base, String template, int digits,
19           String extension)
20   {
21     this.base = base;
22     this.template = template;
23     this.digits = digits;
24     this.extension = extension;
25   }
26
27   @Override
28   public boolean accept(File dir, String filename)
29   {
30     boolean ret = false;
31     try
32     {
33       File file = new File(
34               dir.getCanonicalPath() + File.separatorChar + filename);
35       if (file.isDirectory())
36       {
37         // backup files aren't dirs!
38         return false;
39       }
40     } catch (IOException e)
41     {
42       System.out.println("IOException when checking file '" + filename
43               + "' is a backupfile");
44     }
45
46     BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
47             template, digits, extension);
48     ret = bffp.isBackupFile();
49     return ret;
50   }
51
52 }
53
54 class BackupFilenameParts
55 {
56   String base;
57
58   String templateStart;
59
60   int num;
61
62   int digits;
63
64   String templateEnd;
65
66   String extension;
67
68   boolean isBackupFile;
69
70   public BackupFilenameParts(File file, String base, String template, int digits,
71           String extension)
72   {
73     this(file.getName(), base, template, digits, extension);
74   }
75
76   public BackupFilenameParts(String filename, String base, String template,
77           int digits, String extension)
78   {
79     this.isBackupFile = false;
80
81     // calculate minimum length of a backup filename
82     int minlength = base.length() + template.length()
83             - BackupFiles.NUM_PLACEHOLDER.length() + digits
84             + extension.length();
85
86     if (!(filename.startsWith(base) && filename.endsWith(extension)
87             && filename.length() >= minlength))
88     {
89       // non-starter
90       return;
91     }
92
93     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
94     String templateStart = template;
95     String templateEnd = "";
96     if (numcharstart > -1)
97     {
98       templateStart = template.substring(0, numcharstart);
99       templateEnd = template.substring(numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
100     }
101     
102     int startLength = base.length() + templateStart.length();
103     int endLength = templateEnd.length() + extension.length();
104     String numString = filename.substring(startLength,
105             filename.length() - endLength);
106
107     if (filename.length() >= startLength + digits + endLength
108             && filename.startsWith(base + templateStart)
109             && filename.endsWith(templateEnd + extension)
110             && numString.matches("[0-9]+"))
111     {
112       this.base = base;
113       this.templateStart = templateStart;
114       this.num = Integer.parseInt(numString);
115       this.digits = digits;
116       this.templateStart = templateStart;
117       this.templateEnd = templateEnd;
118       this.isBackupFile = true;
119     }
120     
121   }
122
123   public static String getBackupFilename(int index, String base,
124           String template,
125           int digits, String extension)
126   {
127     String numString = String.format("%0" + digits + "d", index);
128     String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
129             numString);
130     String backupfilename = base + backupSuffix + extension;
131     return backupfilename;
132   }
133
134   public boolean isBackupFile()
135   {
136     return this.isBackupFile;
137   }
138
139   public int indexNum()
140   {
141     return this.num;
142   }
143 }