|
| 1 | +package org.apache.commons.io.filefilter; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.commons.io.FilenameUtils; |
| 7 | +import org.apache.commons.io.IOCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Filters files using the supplied wildcards. |
| 11 | + * <p> |
| 12 | + * This filter selects files and directories based on one or more wildcards. |
| 13 | + * Testing is case-sensitive by default, but this can be configured. |
| 14 | + * <p> |
| 15 | + * The wildcard matcher uses the characters '?' and '*' to represent a |
| 16 | + * single or multiple wildcard characters. |
| 17 | + * This is the same as often found on Dos/Unix command lines. |
| 18 | + * The extension check is case-sensitive by . |
| 19 | + * See {@link FilenameUtils#wildcardMatchOnSystem} for more information. |
| 20 | + * <p> |
| 21 | + * For example: |
| 22 | + * <pre> |
| 23 | + * File dir = new File("."); |
| 24 | + * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~"); |
| 25 | + * File[] files = dir.listFiles(fileFilter); |
| 26 | + * for (int i = 0; i < files.length; i++) { |
| 27 | + * System.out.println(files[i]); |
| 28 | + * } |
| 29 | + * </pre> |
| 30 | + * |
| 31 | + * @author Jason Anderson |
| 32 | + * @version $Revision: 155419 $ $Date$ |
| 33 | + * @since Commons IO 1.3 |
| 34 | + */ |
| 35 | +public class WildcardFileFilter extends AbstractFileFilter { |
| 36 | + |
| 37 | + /** The wildcards that will be used to match filenames. */ |
| 38 | + private String[] wildcards; |
| 39 | + /** Whether the comparison is case sensitive. */ |
| 40 | + private IOCase caseSensitivity; |
| 41 | + |
| 42 | + /** |
| 43 | + * Construct a new case-sensitive wildcard filter for a single wildcard. |
| 44 | + * |
| 45 | + * @param wildcard the wildcard to match |
| 46 | + * @throws IllegalArgumentException if the pattern is null |
| 47 | + */ |
| 48 | + public WildcardFileFilter(String wildcard) { |
| 49 | + this(wildcard, null); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct a new wildcard filter for a single wildcard specifying case-sensitivity. |
| 54 | + * |
| 55 | + * @param wildcard the wildcard to match, not null |
| 56 | + * @param caseSensitivity how to handle case sensitivity, null means case-sensitive |
| 57 | + * @throws IllegalArgumentException if the pattern is null |
| 58 | + */ |
| 59 | + public WildcardFileFilter(String wildcard, IOCase caseSensitivity) { |
| 60 | + if (wildcard == null) { |
| 61 | + throw new IllegalArgumentException("The wildcard must not be null"); |
| 62 | + } |
| 63 | + this.wildcards = new String[] { wildcard }; |
| 64 | + this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Construct a new case-sensitive wildcard filter for an array of wildcards. |
| 69 | + * |
| 70 | + * @param wildcards the array of wildcards to match |
| 71 | + * @throws IllegalArgumentException if the pattern array is null |
| 72 | + */ |
| 73 | + public WildcardFileFilter(String[] wildcards) { |
| 74 | + this(wildcards, null); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity. |
| 79 | + * |
| 80 | + * @param wildcards the array of wildcards to match, not null |
| 81 | + * @param caseSensitivity how to handle case sensitivity, null means case-sensitive |
| 82 | + * @throws IllegalArgumentException if the pattern array is null |
| 83 | + */ |
| 84 | + public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) { |
| 85 | + if (wildcards == null) { |
| 86 | + throw new IllegalArgumentException("The wildcard array must not be null"); |
| 87 | + } |
| 88 | + this.wildcards = wildcards; |
| 89 | + this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Construct a new case-sensitive wildcard filter for a list of wildcards. |
| 94 | + * |
| 95 | + * @param wildcards the list of wildcards to match, not null |
| 96 | + * @throws IllegalArgumentException if the pattern list is null |
| 97 | + * @throws ClassCastException if the list does not contain Strings |
| 98 | + */ |
| 99 | + public WildcardFileFilter(List wildcards) { |
| 100 | + this(wildcards, null); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity. |
| 105 | + * |
| 106 | + * @param wildcards the list of wildcards to match, not null |
| 107 | + * @param caseSensitivity how to handle case sensitivity, null means case-sensitive |
| 108 | + * @throws IllegalArgumentException if the pattern list is null |
| 109 | + * @throws ClassCastException if the list does not contain Strings |
| 110 | + */ |
| 111 | + public WildcardFileFilter(List wildcards, IOCase caseSensitivity) { |
| 112 | + if (wildcards == null) { |
| 113 | + throw new IllegalArgumentException("The wildcard list must not be null"); |
| 114 | + } |
| 115 | + this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]); |
| 116 | + this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); |
| 117 | + } |
| 118 | + |
| 119 | + //----------------------------------------------------------------------- |
| 120 | + /** |
| 121 | + * Checks to see if the filename matches one of the wildcards. |
| 122 | + * |
| 123 | + * @param dir the file directory |
| 124 | + * @param name the filename |
| 125 | + * @return true if the filename matches one of the wildcards |
| 126 | + */ |
| 127 | + public boolean accept(File dir, String name) { |
| 128 | + for (int i = 0; i < wildcards.length; i++) { |
| 129 | + if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Checks to see if the filename matches one of the wildcards. |
| 138 | + * |
| 139 | + * @param file the file to check |
| 140 | + * @return true if the filename matches one of the wildcards |
| 141 | + */ |
| 142 | + public boolean accept(File file) { |
| 143 | + String name = file.getName(); |
| 144 | + for (int i = 0; i < wildcards.length; i++) { |
| 145 | + if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) { |
| 146 | + return true; |
| 147 | + } |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments