Updated copyright dates.
[trakem2.git] / ini / trakem2 / io / ImageFileFilter.java
blob495f57c7d7055d99bd7840e0e40b95d5ae086c10
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005-2009 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.io;
25 import ini.trakem2.utils.Utils;
26 import ini.trakem2.utils.IJError;
28 import java.io.File;
29 import java.io.FilenameFilter;
30 import java.util.regex.*;
32 /** Filters out non-accepted image formats.
35 public class ImageFileFilter implements FilenameFilter {
37 /* // not checking for extension anymore, so the opener can open strange file formats through HandleExtraFileTypes plugin
38 final String JPG = ".jpg";
39 final String TIF = ".tif";
40 final String PNG = ".png";
41 final String GIF = ".gif";
42 final String JPEG = ".jpeg";
43 final String TIFF = ".tiff";
44 final String TIFZIP = ".tif.zip";
46 //static final String EXTENSIONS = ".dat.tif.tiff.jpg.jpeg.png.gif.tif.zip";
47 private String code = null;
48 private Pattern pattern = null;
50 public ImageFileFilter() {}
52 public ImageFileFilter(String regex, String code) {
53 this.code = code;
54 if (null == regex || 0 == regex.length()) return;
55 // create pattern
56 final StringBuffer sb = new StringBuffer(); // I hate java, all these loops just to avoid parsing the backslashes
57 if (!regex.startsWith("^")) sb.append("^.*");
58 for (int i=0; i<regex.length(); i++) {
59 sb.append(regex.charAt(i));
61 if (!regex.endsWith("$")) sb.append(".*$");
62 this.pattern = Pattern.compile(sb.toString()); // case-sensitive
65 public boolean accept(File dir, String name) {
66 boolean b = accept0(dir, name);
67 if (b) System.out.println("accepting " + name);
68 return b;
71 private boolean accept0(File dir, String name) {
72 // skip directories
73 try {
74 final File file = new File(dir.getCanonicalPath().replace('\\', '/') + "/" + name);
75 if (file.isDirectory() || file.isHidden() || name.equals(".") || name.equals("..")) {
76 return false;
78 } catch (Exception e) {
79 IJError.print(e);
82 if (null != code) {
83 if (startsWithCode(name)) return true;
84 } else if (null == pattern || pattern.matcher(name).matches()) return true;
86 return false;
89 //static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
90 //static final String NUMBERS = "0123456789";
92 boolean startsWithCode(String name) {
93 // code is of type 'ccxdd' (for example). 'x' is any, 'c' char, 'd' digit
94 for (int i=0; i<code.length(); i++) {
95 char c = name.charAt(i);
96 switch (code.charAt(i)) {
97 case 'c':
98 //if (-1 == LETTERS.indexOf(c)) return false;
99 if (!Character.isLetter(c)) return false;
100 break;
101 case 'd':
102 //if (-1 == NUMBERS.indexOf(c)) return false;
103 if (!Character.isDigit(c)) return false;
104 break;
105 case 'x': // any character
106 continue;
107 //if (-1 == LETTERS.indexOf(c)) return false;
108 // break;
111 return true;