/Users/lyon/j4p/src/classUtils/pack/util/ExtensionFileFilter.java

1    package classUtils.pack.util; 
2     
3    import java.io.File; 
4    import java.io.FileFilter; 
5    import java.util.ArrayList; 
6    import java.util.List; 
7    import java.util.StringTokenizer; 
8     
9    /** 
10    * A simple FileFilter based on file extensions. 
11    * 
12    * @author Cris Sadun 
13    * @version 1.0 
14    */ 
15   public class ExtensionFileFilter implements FileFilter { 
16    
17       private String [] ext; 
18       private String matchingExt; 
19       private boolean doAccept=true; 
20    
21       /** 
22        * Create a filter which will accept the given extension 
23        * (denoted by a comma-separated string) 
24        * @param extensions a comma-separated string with the extensions 
25        */ 
26       public ExtensionFileFilter(String extensions) { 
27           this(extensions,true); 
28       } 
29    
30       /** 
31        * Create a filter which will accept or refuse the given extension 
32        * (denoted by a comma-separated string) 
33        * @param extensions a comma-separated string with the extensions 
34        * @param doAccept if <b>true</b>, the filter will accept the files 
35        *                 with the given extensions, refuse otherwise. 
36        */ 
37       public ExtensionFileFilter(String extensions, boolean doAccept) { 
38           this(mkExtArray(extensions)); 
39       } 
40    
41       /** 
42        * Create a filter which will filter the given extension 
43        * (denoted by a string array) 
44        * @param extensions a string array with the extensions 
45        */ 
46       public ExtensionFileFilter(String []ext) { 
47           this(ext, true); 
48       } 
49    
50    
51       /** 
52        * Create a filter which will filter the given extension 
53        * (denoted by a string array) 
54        * @param extensions a string array with the extensions 
55        * @param doAccept if <b>true</b>, the filter will accept the files 
56        *                 with the given extensions, refuse otherwise. 
57        */ 
58       public ExtensionFileFilter(String []ext, boolean doAccept) { 
59           this.ext=ext; 
60           this.doAccept=doAccept; 
61       } 
62    
63       private static String [] mkExtArray(String extensions) { 
64           StringTokenizer st = new StringTokenizer(extensions,";,:"); 
65           List l = new ArrayList(); 
66           while(st.hasMoreTokens()) { 
67               l.add(st.nextToken()); 
68           } 
69           String res [] = new String[l.size()]; 
70           l.toArray(res); 
71           return res; 
72       } 
73    
74       /** 
75        * Accept or refuse a file basing on its extension and the 
76        * current working mode (see {@link ExtensionFileFilter#getAcceptsExtensions() 
77        * getAcceptsExtensions()}). 
78        */ 
79       public boolean accept(File path) { 
80           for(int i=0;i<ext.length;i++) { 
81               if (path.getName().endsWith(ext[i])) { 
82                   matchingExt=ext[i]; 
83                   return doAccept; 
84               } 
85           } 
86           matchingExt=null; 
87           return ! doAccept; 
88       } 
89    
90       /** 
91        * Invert the filter - after this call, files that were accepted 
92        * will be refused, and vice versa (see {@link 
93        * ExtensionFileFilter#getAcceptsExtensions() getAcceptsExtensions()}). 
94        */ 
95       public void invert() { doAccept=!doAccept; } 
96    
97       /** 
98        * Return <b>true</b> if the filter is currently set to accept 
99        * files whose extension matches the extensions provided at construction. 
100       * @return <b>true</b> if the filter is currently set to accept 
101       * files whose extension matches the extensions provided at construction. 
102       */ 
103      public boolean getAcceptsExtensions() { return doAccept; } 
104   
105   
106      /** 
107       * Returns the extension that has been last matched in a call 
108       * of {@link ExtensionFileFilter#accept(java.io.File) 
109       * accept()}, or <b>null</b> if accept has never been invoked or 
110       * no extension has been matched. 
111       * @return the last matched exception 
112       */ 
113      public String lastMatchingExtension() { return matchingExt; } 
114  } 
115