001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.io.fileselectors;
007
008import java.io.*;
009import java.util.*;
010
011/**
012Filters a list of files based on the specified prefix. Files <b>not</b>
013having the specifed suffix are chosen. An empty string will result in all
014files being rejected. Note, the <u>file name</u> not the entire path is
015looked at by this filter. For example, if a file has a path of
016<tt>/foo/bar/x.html</tt> then only <tt>x.html</tt> will be looked at by
017this filter. (to retrieve the filename, this filter calls
018<tt>getName()</tt> on the input file)
019<p>
020The specified pattern can be a single token or a a comma delimited set of 
021tokens. For example:
022<blockquote>
023<pre>
024.
025foo
026.,foo,bar
027</pre>
028</blockquote>
029The above will reject all files starting with '.', reject files starting
030with 'foo' and reject files starting with '.' or 'foo' or 'bar' respectively.
031
032@author hursh jain
033@date   3/24/2002
034**/   
035public class FilePrefixRejectSelector extends ChainedFileSelector 
036{
037final static boolean dbg = false;
038List matchlist;
039
040public FilePrefixRejectSelector(String pattern) {
041  this(pattern, null);
042  }
043  
044public FilePrefixRejectSelector(String pattern, ChainedFileSelector filter) {
045  super(filter);
046  matchlist = new ArrayList();
047  if (pattern.equals("")) { 
048    matchlist.add("");
049    }
050  else {
051    StringTokenizer tok = new StringTokenizer(pattern, ",");
052    while ( tok.hasMoreTokens()) {
053      matchlist.add(tok.nextToken());
054      }
055    }
056  if (dbg) System.out.println("token match list=" + matchlist);
057  }
058
059protected boolean thisfilter(File name) 
060  { 
061  String filename = name.getName();
062  
063  if (dbg) System.out.print("FilePrefixRejectSelector: matching: " + filename);
064  Iterator it = matchlist.listIterator(); 
065  while (it.hasNext()) { 
066    String pat = (String) it.next();
067    if (dbg) System.out.println("; startsWith(" + pat + ")=" + filename.startsWith(pat));
068    if (filename.startsWith(pat)) {
069      return false;
070      }
071    }
072  return true;
073  }
074
075public static void main(String[] args) throws Exception
076  {
077  new Test();
078  }
079
080/**
081Unit Test History   
082<pre>
083Class Version Tester  Status    Notes
0841.0       hj    passed  
085</pre>
086*/
087static private class Test {
088Test() throws Exception
089  {
090  File startdir = new File("/tmp");
091  //will match both files and directories
092  ChainedFileSelector sel = new FilePrefixRejectSelector(".");  
093  File[] files = startdir.listFiles(sel);
094  List list = Arrays.asList(files);     
095  System.out.println("total="+ list.size() + ", files=" + list);
096
097  //empty suffix
098  sel = new FilePrefixRejectSelector("");
099  files = startdir.listFiles(sel);
100  list = Arrays.asList(files);      
101  System.out.println("total="+ list.size() + ", files=" + list);
102  }
103} //~class Test
104
105}