00001 package edu.ksu.cis.bandera.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 import java.io.*;
00088 import java.util.*;
00089
00090 public class CustomFileFilter implements FilenameFilter {
00091 private boolean dirtoo = true;
00092 private Hashtable RETable = new Hashtable();
00093
00094
00095
00096 public CustomFileFilter() {
00097 super();
00098 }
00099 public boolean accept(File dir, String name)
00100 {
00101 if (dirtoo)
00102 {
00103 try {
00104 if (new File (dir.getCanonicalPath()+File.separator+name).isDirectory())
00105 return true;
00106 } catch (Exception e) { return false; }
00107 }
00108 for (Enumeration e = RETable.keys(); e.hasMoreElements(); )
00109 {
00110 String s = (String) e.nextElement();
00111 if (isMatched(s,name)) return true;
00112 }
00113 return false;
00114 }
00115 public void includeDir(boolean b)
00116 {
00117 dirtoo = b;
00118 }
00119 public boolean isMatched(String RE, String s)
00120 {
00121 List l = (List) RETable.get(RE);
00122 if (l == null) l = parseRE(RE);
00123
00124 int curpos = 0, requires = 0;
00125 boolean more = false;
00126 String token="";
00127 for (Iterator i = l.iterator(); i.hasNext(); )
00128 {
00129 token = (String) i.next();
00130 if (token.indexOf("?") != -1)
00131 {
00132 if (token.indexOf("*") != -1)
00133 {
00134 more = true; requires = token.length()-1;
00135 } else
00136 {
00137 more = false; requires = token.length();
00138 }
00139 } else
00140 {
00141 if (token.indexOf("*") != -1)
00142 {
00143 if (token.length() > 1) throw new RuntimeException("Bug!");
00144 more = true;
00145 } else
00146 {
00147 int pos = s.indexOf(token);
00148 if (pos < (requires+curpos)) return false;
00149 if ((pos > (requires+curpos)) && !more) return false;
00150 curpos = pos+token.length();
00151 }
00152 }
00153 }
00154 if (token.indexOf("?") != -1)
00155 {
00156 if (token.indexOf("*") != -1)
00157 return (s.length()-curpos) >= (token.length());
00158 else
00159 return (s.length()-curpos-1) > (token.length());
00160 } else
00161 {
00162 if (token.indexOf("*") == -1 && s.length() > curpos) return false;
00163 }
00164 return true;
00165 }
00166 public static void main(String[] args)
00167 {
00168
00169 CustomFileFilter ff = new CustomFileFilter();
00170
00171 LinkedList l = new LinkedList();
00172 l.addLast("*.jpg");
00173 l.addLast("*.png");
00174 l.addLast("*.gif");
00175 l.addLast("*.bmp");
00176 ff.setFileList(l);
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 }
00203 private List parseRE(String RE)
00204 {
00205 LinkedList l = new LinkedList();
00206 int state = 0;
00207 String s = "";
00208
00209 for (int i = 0; i < RE.length(); i++)
00210 {
00211 char c = RE.charAt(i);
00212 if (state == 0)
00213 {
00214 if (c == '*' || c == '?')
00215 {
00216 if (!s.equals("")) l.addLast(s);
00217 s = "" + c; state = 1;
00218 } else
00219 {
00220 s += c;
00221 }
00222 } else
00223 {
00224
00225 if (c != '?' && c != '*')
00226 {
00227 if (!s.equals("")) l.addLast(s);
00228 s = "" + c; state = 0;
00229 } else
00230 {
00231 if (c == '?') s = c + s;
00232 if (c == '*' && s.indexOf("*") == -1) s += "*";
00233 }
00234 }
00235 }
00236 if (!s.equals("")) l.addLast(s);
00237 return l;
00238 }
00239 public void setFileList(List l) {
00240 if (l == null) throw new RuntimeException("List is null");
00241 if (l.isEmpty()) throw new RuntimeException("List is empty");
00242
00243 try
00244 {
00245 for (Iterator i=l.iterator(); i.hasNext(); )
00246 {
00247 String s = (String) i.next();
00248 if (s == null) throw new RuntimeException("List contains null");
00249 if (RETable.get(s) == null) RETable.put(s,parseRE(s));
00250 }
00251 } catch (Exception e)
00252 {
00253 throw new RuntimeException("List is not properly set up");
00254 }
00255 }
00256 }