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 suffix. Files <b>not</b> having 013the specifed suffix are chosen. To choose all files, specify a empty string as 014the suffix. 015<p> 016The specified pattern should be an file extension, or a comma 017delimited set of file extensions. For example: 018<blockquote> 019gif <br> 020.gif <br> 021gif,.jpg,jpeg 022</blockquote> 023Note: The '.' preceding the extension is optional: both <tt>gif</tt> and 024<tt>.gif</tt> are allowed in the specified pattern. 025 026@author hursh jain 027@date 6/14/2002 028**/ 029public class SuffixRejectSelector extends ChainedFileSelector 030{ 031List matchlist; 032 033public SuffixRejectSelector(String pattern) { 034 this(pattern, null); 035 } 036 037public SuffixRejectSelector(String pattern, ChainedFileSelector filter) { 038 super(filter); 039 matchlist = new ArrayList(); 040 if (pattern.equals("")) { 041 //nothing 042 } 043 else { 044 StringTokenizer tok = new StringTokenizer(pattern, ","); 045 while ( tok.hasMoreTokens()) { 046 matchlist.add(tok.nextToken()); 047 } 048 } 049 //System.out.println("matchlist=" + matchlist); 050 } 051 052protected boolean thisfilter(File name) { 053 String filename = name.toString(); 054 Iterator it = matchlist.listIterator(); 055 while (it.hasNext()) { 056 String pat = (String) it.next(); 057 //we didn't add "" to our list (if we did, then endsWith("") 058 //would always return false 059 if (filename.endsWith(pat)) { 060 return false; 061 } 062 } 063 return true; 064 } 065 066public static void main(String[] args) throws Exception 067 { 068 new Test(); 069 } 070 071/** 072Unit Test History 073<pre> 074Class Version Tester Status Notes 0751.0 hj passed 076</pre> 077*/ 078static private class Test { 079Test() throws Exception 080 { 081 File startdir = new File("c:\\temp"); 082 ChainedFileSelector sel = new SuffixRejectSelector(".html", new NormalFileSelector()); 083 File[] files = startdir.listFiles(sel); 084 List list = Arrays.asList(files); 085 System.out.println("total="+ list.size() + ", files=" + list); 086 087 //empty suffix 088 sel = new SuffixRejectSelector("", new NormalFileSelector()); 089 files = startdir.listFiles(sel); 090 list = Arrays.asList(files); 091 System.out.println("total="+ list.size() + ", files=" + list); 092 } 093} //~class Test 094 095}