| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| WildcardFileFilter |
|
| 3.0;3 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io.filefilter; | |
| 18 | ||
| 19 | import java.io.File; | |
| 20 | import java.io.Serializable; | |
| 21 | import java.util.List; | |
| 22 | ||
| 23 | import org.apache.commons.io.FilenameUtils; | |
| 24 | import org.apache.commons.io.IOCase; | |
| 25 | ||
| 26 | /** | |
| 27 | * Filters files using the supplied wildcards. | |
| 28 | * <p> | |
| 29 | * This filter selects files and directories based on one or more wildcards. | |
| 30 | * Testing is case-sensitive by default, but this can be configured. | |
| 31 | * <p> | |
| 32 | * The wildcard matcher uses the characters '?' and '*' to represent a | |
| 33 | * single or multiple wildcard characters. | |
| 34 | * This is the same as often found on Dos/Unix command lines. | |
| 35 | * The check is case-sensitive by default. | |
| 36 | * See {@link FilenameUtils#wildcardMatchOnSystem} for more information. | |
| 37 | * <p> | |
| 38 | * For example: | |
| 39 | * <pre> | |
| 40 | * File dir = new File("."); | |
| 41 | * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~"); | |
| 42 | * File[] files = dir.listFiles(fileFilter); | |
| 43 | * for (int i = 0; i < files.length; i++) { | |
| 44 | * System.out.println(files[i]); | |
| 45 | * } | |
| 46 | * </pre> | |
| 47 | * | |
| 48 | * @version $Id: WildcardFileFilter.java 1465476 2013-04-07 22:00:26Z sebb $ | |
| 49 | * @since 1.3 | |
| 50 | */ | |
| 51 | public class WildcardFileFilter extends AbstractFileFilter implements Serializable { | |
| 52 | ||
| 53 | /** The wildcards that will be used to match filenames. */ | |
| 54 | private final String[] wildcards; | |
| 55 | /** Whether the comparison is case sensitive. */ | |
| 56 | private final IOCase caseSensitivity; | |
| 57 | ||
| 58 | /** | |
| 59 | * Construct a new case-sensitive wildcard filter for a single wildcard. | |
| 60 | * | |
| 61 | * @param wildcard the wildcard to match | |
| 62 | * @throws IllegalArgumentException if the pattern is null | |
| 63 | */ | |
| 64 | public WildcardFileFilter(final String wildcard) { | |
| 65 | 10 | this(wildcard, IOCase.SENSITIVE); |
| 66 | 9 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Construct a new wildcard filter for a single wildcard specifying case-sensitivity. | |
| 70 | * | |
| 71 | * @param wildcard the wildcard to match, not null | |
| 72 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 73 | * @throws IllegalArgumentException if the pattern is null | |
| 74 | */ | |
| 75 | 14 | public WildcardFileFilter(final String wildcard, final IOCase caseSensitivity) { |
| 76 | 14 | if (wildcard == null) { |
| 77 | 1 | throw new IllegalArgumentException("The wildcard must not be null"); |
| 78 | } | |
| 79 | 13 | this.wildcards = new String[] { wildcard }; |
| 80 | 13 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 81 | 13 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Construct a new case-sensitive wildcard filter for an array of wildcards. | |
| 85 | * <p> | |
| 86 | * | |
| 87 | * @param wildcards the array of wildcards to match | |
| 88 | * @throws IllegalArgumentException if the pattern array is null | |
| 89 | */ | |
| 90 | public WildcardFileFilter(final String[] wildcards) { | |
| 91 | 2 | this(wildcards, IOCase.SENSITIVE); |
| 92 | 1 | } |
| 93 | ||
| 94 | /** | |
| 95 | * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity. | |
| 96 | * <p> | |
| 97 | * | |
| 98 | * @param wildcards the array of wildcards to match, not null | |
| 99 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 100 | * @throws IllegalArgumentException if the pattern array is null | |
| 101 | */ | |
| 102 | 6 | public WildcardFileFilter(final String[] wildcards, final IOCase caseSensitivity) { |
| 103 | 6 | if (wildcards == null) { |
| 104 | 1 | throw new IllegalArgumentException("The wildcard array must not be null"); |
| 105 | } | |
| 106 | 5 | this.wildcards = new String[wildcards.length]; |
| 107 | 5 | System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length); |
| 108 | 5 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 109 | 5 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Construct a new case-sensitive wildcard filter for a list of wildcards. | |
| 113 | * | |
| 114 | * @param wildcards the list of wildcards to match, not null | |
| 115 | * @throws IllegalArgumentException if the pattern list is null | |
| 116 | * @throws ClassCastException if the list does not contain Strings | |
| 117 | */ | |
| 118 | public WildcardFileFilter(final List<String> wildcards) { | |
| 119 | 2 | this(wildcards, IOCase.SENSITIVE); |
| 120 | 1 | } |
| 121 | ||
| 122 | /** | |
| 123 | * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity. | |
| 124 | * | |
| 125 | * @param wildcards the list of wildcards to match, not null | |
| 126 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 127 | * @throws IllegalArgumentException if the pattern list is null | |
| 128 | * @throws ClassCastException if the list does not contain Strings | |
| 129 | */ | |
| 130 | 2 | public WildcardFileFilter(final List<String> wildcards, final IOCase caseSensitivity) { |
| 131 | 2 | if (wildcards == null) { |
| 132 | 1 | throw new IllegalArgumentException("The wildcard list must not be null"); |
| 133 | } | |
| 134 | 1 | this.wildcards = wildcards.toArray(new String[wildcards.size()]); |
| 135 | 1 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 136 | 1 | } |
| 137 | ||
| 138 | //----------------------------------------------------------------------- | |
| 139 | /** | |
| 140 | * Checks to see if the filename matches one of the wildcards. | |
| 141 | * | |
| 142 | * @param dir the file directory (ignored) | |
| 143 | * @param name the filename | |
| 144 | * @return true if the filename matches one of the wildcards | |
| 145 | */ | |
| 146 | @Override | |
| 147 | public boolean accept(final File dir, final String name) { | |
| 148 | 9 | for (final String wildcard : wildcards) { |
| 149 | 7 | if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) { |
| 150 | 1 | return true; |
| 151 | } | |
| 152 | } | |
| 153 | 2 | return false; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Checks to see if the filename matches one of the wildcards. | |
| 158 | * | |
| 159 | * @param file the file to check | |
| 160 | * @return true if the filename matches one of the wildcards | |
| 161 | */ | |
| 162 | @Override | |
| 163 | public boolean accept(final File file) { | |
| 164 | 52 | final String name = file.getName(); |
| 165 | 79 | for (final String wildcard : wildcards) { |
| 166 | 65 | if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) { |
| 167 | 38 | return true; |
| 168 | } | |
| 169 | } | |
| 170 | 14 | return false; |
| 171 | } | |
| 172 | ||
| 173 | /** | |
| 174 | * Provide a String representation of this file filter. | |
| 175 | * | |
| 176 | * @return a String representation | |
| 177 | */ | |
| 178 | @Override | |
| 179 | public String toString() { | |
| 180 | 25 | final StringBuilder buffer = new StringBuilder(); |
| 181 | 25 | buffer.append(super.toString()); |
| 182 | 25 | buffer.append("("); |
| 183 | 25 | if (wildcards != null) { |
| 184 | 69 | for (int i = 0; i < wildcards.length; i++) { |
| 185 | 44 | if (i > 0) { |
| 186 | 19 | buffer.append(","); |
| 187 | } | |
| 188 | 44 | buffer.append(wildcards[i]); |
| 189 | } | |
| 190 | } | |
| 191 | 25 | buffer.append(")"); |
| 192 | 25 | return buffer.toString(); |
| 193 | } | |
| 194 | ||
| 195 | } |