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