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