View Javadoc

    1   package net.sourceforge.pmd.lang;
    2
    3   import java.io.File;
    4   import java.io.FilenameFilter;
    5   import java.util.Collections;
    6   import java.util.List;
    7   import java.util.Set;
    8
    9   /**
    10   * This is an implementation of the {@link FilenameFilter} interface which
    11   * compares a file against a collection of Languages to see if the any are
    12   * applicable.
    13   *
    14   * @author Pieter_Van_Raemdonck - Application Engineers NV/SA - www.ae.be
    15   */
    16  public class LanguageFilenameFilter implements FilenameFilter {
    17
    18  	private final Set<Language> languages;
    19
    20  	/**
    21  	 * Create a LanguageFilenameFilter for a single Language.
    22  	 * @param language The Language.
    23  	 */
    24  	public LanguageFilenameFilter(Language language) {
    25  		this(Collections.singleton(language));
    26  	}
    27
    28  	/**
    29  	 * Create a LanguageFilenameFilter for a List of Languages.
    30  	 * @param languages The List of Languages.
    31  	 */
    32  	public LanguageFilenameFilter(Set<Language> languages) {
    33  		this.languages = languages;
    34  	}
    35
    36  	/**
    37  	 * Check if a file should be checked by PMD.
    38  	 * {@inheritDoc}
    39  	 */
    40  	public boolean accept(File dir, String name) {
    41  		// Any source file should have a '.' in its name...
    42  		int lastDotIndex = name.lastIndexOf('.');
    43  		if (lastDotIndex < 0) {
    44  			return false;
    45  		}
    46
    47  		String extension = name.substring(1 + lastDotIndex).toUpperCase();
    48  		for (Language language : languages) {
    49  			for (String ext : language.getExtensions()) {
    50  				if (extension.equalsIgnoreCase(ext)) {
    51  					return true;
    52  				}
    53  			}
    54  		}
    55  		return false;
    56  	}
    57
    58  	public String toString() {
    59  		StringBuilder buffer = new StringBuilder("(Extension is one of: ");
    60  		for (Language language : languages) {
    61  			List<String> extensions = language.getExtensions();
    62  			for (int i = 0; i < extensions.size(); i++) {
    63  				if (i > 0) {
    64  					buffer.append(", ");
    65  				}
    66  				buffer.append(extensions.get(i));
    67  			}
    68  		}
    69  		buffer.append(')');
    70  		return buffer.toString();
    71  	}
    72  }