001/* 002 * This file was downloaded on 2/21/2011 from http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java?view=co 003 * 004 * It is subject to the ASL 2.0 (see below). 005 * 006 * The following changes were made from the original file: 007 * - the package name was changed 008 * - a default serialVersionUID was added 009 * - the accept matches returns the value of matcher.find() rather than matcher.matches() 010 */ 011 012/* 013 * Licensed to the Apache Software Foundation (ASF) under one or more 014 * contributor license agreements. See the NOTICE file distributed with 015 * this work for additional information regarding copyright ownership. 016 * The ASF licenses this file to You under the Apache License, Version 2.0 017 * (the "License"); you may not use this file except in compliance with 018 * the License. You may obtain a copy of the License at 019 * 020 * http://www.apache.org/licenses/LICENSE-2.0 021 * 022 * Unless required by applicable law or agreed to in writing, software 023 * distributed under the License is distributed on an "AS IS" BASIS, 024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 025 * See the License for the specific language governing permissions and 026 * limitations under the License. 027 */ 028package org.cleartk.util.cr; 029 030import java.io.File; 031import java.io.Serializable; 032import java.util.regex.Pattern; 033 034import org.apache.commons.io.IOCase; 035import org.apache.commons.io.filefilter.AbstractFileFilter; 036 037/** 038 * Filters files using supplied regular expression(s). 039 * <p/> 040 * See java.util.regex.Pattern for regex matching rules 041 * <p/> 042 * 043 * <p/> 044 * e.g. 045 * 046 * <pre> 047 * File dir = new File("."); 048 * FileFilter fileFilter = new RegexFileFilter("ˆ.*[tT]est(-\\d+)?\\.java$"); 049 * File[] files = dir.listFiles(fileFilter); 050 * for (int i = 0; i < files.length; i++) { 051 * System.out.println(files[i]); 052 * } 053 * </pre> 054 * 055 * @author Oliver Siegmar 056 * @version $Revision$ 057 * @since Commons IO 1.4 058 */ 059public class RegexFileFilter extends AbstractFileFilter implements Serializable { 060 061 /** 062 * 063 */ 064 private static final long serialVersionUID = 1L; 065 066 /** The regular expression pattern that will be used to match filenames */ 067 private final Pattern pattern; 068 069 /** 070 * Construct a new regular expression filter. 071 * 072 * @param pattern 073 * regular string expression to match 074 * @throws IllegalArgumentException 075 * if the pattern is null 076 */ 077 public RegexFileFilter(String pattern) { 078 if (pattern == null) { 079 throw new IllegalArgumentException("Pattern is missing"); 080 } 081 082 this.pattern = Pattern.compile(pattern); 083 } 084 085 /** 086 * Construct a new regular expression filter with the specified flags case sensitivity. 087 * 088 * @param pattern 089 * regular string expression to match 090 * @param caseSensitivity 091 * how to handle case sensitivity, null means case-sensitive 092 * @throws IllegalArgumentException 093 * if the pattern is null 094 */ 095 public RegexFileFilter(String pattern, IOCase caseSensitivity) { 096 if (pattern == null) { 097 throw new IllegalArgumentException("Pattern is missing"); 098 } 099 int flags = 0; 100 if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) { 101 flags = Pattern.CASE_INSENSITIVE; 102 } 103 this.pattern = Pattern.compile(pattern, flags); 104 } 105 106 /** 107 * Construct a new regular expression filter with the specified flags. 108 * 109 * @param pattern 110 * regular string expression to match 111 * @param flags 112 * pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE} 113 * @throws IllegalArgumentException 114 * if the pattern is null 115 */ 116 public RegexFileFilter(String pattern, int flags) { 117 if (pattern == null) { 118 throw new IllegalArgumentException("Pattern is missing"); 119 } 120 this.pattern = Pattern.compile(pattern, flags); 121 } 122 123 /** 124 * Construct a new regular expression filter for a compiled regular expression 125 * 126 * @param pattern 127 * regular expression to match 128 * @throws IllegalArgumentException 129 * if the pattern is null 130 */ 131 public RegexFileFilter(Pattern pattern) { 132 if (pattern == null) { 133 throw new IllegalArgumentException("Pattern is missing"); 134 } 135 136 this.pattern = pattern; 137 } 138 139 /** 140 * Checks to see if the filename matches one of the regular expressions. 141 * 142 * @param dir 143 * the file directory 144 * @param name 145 * the filename 146 * @return true if the filename matches one of the regular expressions 147 */ 148 @Override 149 public boolean accept(File dir, String name) { 150 return (pattern.matcher(name).find()); 151 } 152 153}