1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.file.filefilter;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.apache.commons.io.filefilter.AbstractFileFilter;
24 import org.apache.commons.io.FilenameUtils;
25
26 /***
27 * Filters files using supplied wildcard(s). Based on the Apache
28 * WildcardFilter class in the Commons IO package. Difference is
29 * that in this class, it uses the
30 * org.apache.commons.io.FilenameUtils.wildcardMatchOnSystem()
31 * for its matching rules, which means that pattern matching using
32 * this class is OS dependent (case-insensitive on Windows and
33 * case-sensitive on Unix, Linux, MAC)
34 *
35 * @author mcayanan
36 * @version $Revision: 3461 $
37 *
38 */
39
40 public class WildcardOSFilter extends AbstractFileFilter {
41
42 private List wildcards = null;
43
44 /***
45 * Constructor for a single wildcard
46 *
47 * @param wc a single filter to set
48 * @throws NullPointerException if the pattern is null
49 */
50 public WildcardOSFilter(String wc) {
51 if(wc == null) {
52 throw new NullPointerException();
53 }
54
55 this.wildcards = new ArrayList();
56 this.wildcards.add(wc);
57 }
58
59 /***
60 * Returns list of filters that were set
61 * @return a list of filters
62 */
63 public List getWildcards() {
64 return wildcards;
65 }
66
67 /***
68 * Constructor for a list of wildcards
69 *
70 * @param wc a list of filters to set
71 * @throws NullPointerException if the pattern list is null
72 */
73 public WildcardOSFilter(List wc) {
74 if(wc == null) {
75 throw new NullPointerException();
76 }
77
78 this.wildcards = new ArrayList();
79 this.wildcards.addAll(wc);
80 }
81
82 /***
83 * Checks to see if the filename matches one of the wildcards. Matching is
84 * case-insensitive for Windows and case-sensitive for Unix.
85 *
86 * @param file the file to check
87 * @return true if the filename matches one of the wildcards
88 * @throws NullPointerException if the file is null
89 */
90
91 public boolean accept(File file) {
92
93 if(file == null)
94 throw new NullPointerException("No file specified");
95
96 for(Iterator i = wildcards.iterator(); i.hasNext(); ) {
97 if(FilenameUtils.wildcardMatchOnSystem(file.getName(), i.next().toString())) {
98 return true;
99 }
100 }
101
102 return false;
103 }
104
105 /***
106 * Checks to see if the filename matches one of the wildcards. Matching is
107 * case-insensitive for Windows and case-sensitive for Unix.
108 *
109 * @param dir the directory to check
110 * @param name the file name within the directory to check
111 * @return true if the filename matches one of the wildcards, false otherwise
112 * @throws NullPointerException if the file is null
113 */
114 public boolean accept(File dir, String name) {
115 return accept(new File(dir, name));
116 }
117
118 }