View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util.filter;
5   
6   import java.io.File;
7   
8   public class FileExtensionFilter implements Filter<File> {
9   	protected final String[] extensions;
10  	protected final boolean ignoreCase;
11  
12  	/**
13  	 * Matches any files with the given extensions, ignoring case
14  	 */
15  	public FileExtensionFilter(String... extensions) {
16  		this(true, extensions);
17  	}
18  
19  	/**
20  	 * Matches any files with the given extensions, optionally ignoring case.
21  	 */
22  	public FileExtensionFilter(boolean ignoreCase, String... extensions) {
23  		this.extensions = extensions;
24  		this.ignoreCase = ignoreCase;
25  		if (ignoreCase) {
26  			for (int i = 0; i < this.extensions.length; i++) {
27  				this.extensions[i] = this.extensions[i].toUpperCase();
28  			}
29  		}
30  	}
31  
32  	public boolean filter(File file) {
33  		boolean accept = extensions == null;
34  		if (!accept) {
35  			for (String extension : extensions) {
36  				String name = file.getName();
37  				if (ignoreCase ? name.toUpperCase().endsWith(extension) : name.endsWith(extension)) {
38  					accept = true;
39  					break;
40  				}
41  			}
42  		}
43  		return accept;
44  	}
45  }