View Javadoc

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