Back to index

wims  3.65+svn20090927
FileList.java
Go to the documentation of this file.
00001 package rene.util;
00002 
00003 import java.io.File;
00004 import java.util.Enumeration;
00005 import java.util.StringTokenizer;
00006 import java.util.Vector;
00007 
00008 import rene.util.sort.SortObject;
00009 import rene.util.sort.Sorter;
00010 
00011 class SortFile extends File
00012        implements SortObject
00013 {      String S;
00014        static int SortBy=0;
00015        final public static int NAME=0,DATE=1;
00016        public SortFile (File dir, String name)
00017        {      super(dir,name);
00018               try
00019               {      S=getCanonicalPath().toUpperCase();
00020               }
00021               catch (Exception e)
00022               { S=""; }
00023        }
00024        public int compare (SortObject o)
00025        {      SortFile f=(SortFile)o;
00026               if (SortBy==DATE)
00027               {      long n=f.lastModified();
00028                      long m=lastModified();
00029                      if (n<m) return -1;
00030                      if (n>m) return 1;
00031                      return 0;
00032               }
00033               return -f.S.compareTo(S);
00034        }
00035 }
00036 
00037 class FileFilter
00038 {      char F[][];
00039        public FileFilter (String s)
00040        {      StringTokenizer t=new StringTokenizer(s);
00041               int n=t.countTokens();
00042               F=new char[n][];
00043               for (int i=0; i<n; i++)
00044               {      F[i]=t.nextToken().toCharArray();
00045               }
00046        }
00047        public char[] filter (int i)
00048        {      return F[i];
00049        }
00050        public int filterCount ()
00051        {      return F.length;
00052        }
00053 }
00054 
00063 public class FileList
00064 {      Vector V=new Vector(),Vdir=new Vector();
00065        boolean Stop;
00066        boolean Recurse;
00067        String Dir,Filter;
00068        boolean UseCase=false;
00069        public FileList (String dir, String filter, boolean recurse)
00070        {      Stop=false;
00071               Recurse=recurse;
00072               Dir=dir;
00073               Filter=filter;
00074               if (Dir.equals("-"))
00075               {      Dir=".";
00076                      Recurse=false;
00077               }
00078               else if (Dir.startsWith("-"))
00079               {      Dir=Dir.substring(1);
00080                      Recurse=false;
00081               }
00082        }
00083        public FileList (String dir, String filter)
00084        {      this(dir,filter,true);
00085        }
00086        public FileList (String dir)
00087        {      this(dir,"*",true);
00088        }
00089        public void setCase (boolean usecase)
00090        {      UseCase=usecase;
00091        }
00092        public void search ()
00093        {      Stop=false;   
00094               File file=new File(Dir);
00095               if (!UseCase) Filter=Filter.toLowerCase();
00096               if (file.isDirectory()) find(file,new FileFilter(Filter));
00097        }
00098        void find (File dir, FileFilter filter)
00099        {      if (!directory(dir)) return;
00100               String list[]=dir.list();
00101               loop : for (int i=0; i<list.length; i++)
00102               {      SortFile file=new SortFile(dir,list[i]);
00103                      if (file.isDirectory()) 
00104                      {      Vdir.addElement(file);
00105                             if (Recurse) find(file,filter);
00106                      }
00107                      else 
00108                      {      String filename=file.getName();
00109                             if (!UseCase) filename=filename.toLowerCase();
00110                             char fn[]=filename.toCharArray();
00111                             for (int j=0; j<filter.filterCount(); j++)
00112                             {      if (match(fn,0,filter.filter(j),0))
00113                                    {      Stop=!file(file);
00114                                           if (Stop) break loop;
00115                                           V.addElement(file);
00116                                    }
00117                             }
00118                      }
00119                      if (Stop) break;
00120               }
00121               parsed(dir);
00122        }
00123        boolean match (char filename[], int n, char filter[], int m)
00124        {      if (filter==null) return true;
00125               if (m>=filter.length) return n>=filename.length;
00126               if (n>=filename.length) return m==filter.length-1 && filter[m]=='*';
00127               if (filter[m]=='?')
00128               {      return match(filename,n+1,filter,m+1);
00129               }
00130               if (filter[m]=='*')
00131               {      if (m==filter.length-1) return true;
00132                      for (int i=n; i<filename.length; i++)
00133                      {      if (match(filename,i,filter,m+1)) return true;
00134                      }
00135                      return false;
00136               }
00137               if (filter[m]==filename[n]) return match(filename,n+1,filter,m+1);
00138               return false;
00139        }
00143        public Enumeration files ()
00144        {      return V.elements();
00145        }
00149        public Enumeration dirs ()
00150        {      return Vdir.elements();
00151        }
00155        public int size ()
00156        {      return V.size();
00157        }
00161        public void sort ()
00162        {      int i,n=V.size();
00163               SortObject v[]=new SortObject[n];
00164               for (i=0; i<n; i++) v[i]=(SortFile)V.elementAt(i);
00165               Sorter.sort(v);
00166               for (i=0; i<n; i++) V.setElementAt(v[i],i);
00167               n=Vdir.size();
00168               v=new SortObject[n];
00169               for (i=0; i<n; i++) v[i]=(SortFile)Vdir.elementAt(i);
00170               Sorter.sort(v);
00171               for (i=0; i<n; i++) Vdir.setElementAt(v[i],i);
00172        }
00173        public void sort (int type)
00174        {      SortFile.SortBy=type;
00175               sort();
00176               SortFile.SortBy=SortFile.NAME;
00177        }
00183        protected boolean directory (File dir)
00184        {      return true;
00185        }
00190        protected boolean file (File file)
00191        {      return true;
00192        }
00196        protected void parsed (File dir)
00197        {
00198        }
00202        public void stopIt ()
00203        {      Stop=true;
00204        }
00208        public String getDir ()
00209        {      File dir=new File(Dir);
00210               try
00211               {      return (dir.getCanonicalPath());
00212               }
00213               catch (Exception e)
00214               {      return "Dir does not exist!";
00215               }
00216        }
00217 }