Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

CustomFileFilter.java

00001 package edu.ksu.cis.bandera.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000  Roby Joehanes (robbyjo@cis.ksu.edu)           *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 
00036 /**
00037  * <p>A powerful file filter for Java. Accepts UNIX style wildcards.
00038  * <p>Usage :<BR>
00039  * The method <tt>setFileList(List l)</tt> is to set a list of wildcard
00040  * strings for a template. Then you can use it in file dialogs and
00041  * file listing statements.
00042  * <p>It accepts * and ? as the usual wildcards. The question marks ('?')
00043  * requires exactly one character (not zero or one as it is in DOS). The
00044  * star ('*') denotes zero or more character.
00045  * <p>The inner working is when the list is passed, the strings in the
00046  * list are tokenized in <tt>parseRE(String)</tt> method and placed into
00047  * the hashtable <tt>RETable</tt>. Then, whenever
00048  * <tt>accept(File, String)</tt> is called by file listing statements,
00049  * it calls <tt>isMatched(String, String)</tt> for each string token in
00050  * the table.
00051  * <p>The method <tt>includeDir(boolean)</tt> determines whether we should
00052  * filter directories too or not. If it is set to <tt>true</tt>, then
00053  * the directories will <b>NOT</b> be filtered. It is filtered otherwise.
00054  * <p>An example on how using it:
00055  <pre>  CustomFileFilter ff = new CustomFileFilter();
00056 
00057     LinkedList l = new LinkedList();
00058     l.addLast("*.jpg");
00059     l.addLast("*.png");
00060     l.addLast("*.gif");
00061     l.addLast("*.bmp");
00062     ff.setFileList(l);
00063 </pre>
00064 <p>Now, suppose we have a method called <tt>dirLister</tt> as follows:
00065 
00066 <pre>   void dirLister(File fn, FileFilter filter)
00067     {
00068         File[] files = fn.listFiles(filter);
00069         for (int i=0; i < files.length; i++)
00070         {
00071             if (files[i].isDirectory())
00072             {
00073                 if (recurseDir) dirLister(files[i], filter);
00074             } else
00075             {
00076                 analyzeFile(files[i]); // Or do anything with it
00077             }
00078         }
00079     }
00080 </pre>
00081 <p>In the main method, we simply call:<br>
00082     <tt>dirLister(new File("."),ff);</tt>
00083 
00084 <p>Alternatively, we can put the file filter as a field and
00085     modify the <tt>dirLister</tt> method into accepting only <tt>File fn</tt>.
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  * CustomFileFilter constructor comment.
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     // An example on how to use it:
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     Now, suppose we have a method called dirLister as follows:
00180 
00181     void dirLister(File fn, FileFilter filter)
00182     {
00183         File[] files = fn.listFiles(filter);
00184         for (int i=0; i < files.length; i++)
00185         {
00186             if (files[i].isDirectory())
00187             {
00188                 if (recurseDir) dirLister(files[i], filter);
00189             } else
00190             {
00191                 analyzeFile(files[i]); // Or do anything with it
00192             }
00193         }
00194     }
00195 
00196     In the main method, we simply call:
00197     dirLister(new File("."),ff);
00198 
00199     Alternatively, we can put the file filter as a field and
00200     modify the dirLister method into accepting only File fn.
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             // Accept * or ?
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 }

Generated at Thu Feb 7 06:43:15 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001