001 package jp.osdl.jbento2; 002 003 import java.io.File; 004 import java.util.ArrayList; 005 import java.util.Iterator; 006 import java.util.List; 007 import java.util.regex.Pattern; 008 009 import org.apache.commons.io.filefilter.AbstractFileFilter; 010 011 public class RegexNameFileFilter extends AbstractFileFilter { 012 013 List patterns = new ArrayList(); 014 015 public RegexNameFileFilter() { 016 } 017 018 public RegexNameFileFilter(String pattern) { 019 patterns.add(Pattern.compile(pattern)); 020 } 021 022 public RegexNameFileFilter(String[] patterns) { 023 for (int i = 0; i < patterns.length; i++) { 024 this.patterns.add(Pattern.compile(patterns[i])); 025 } 026 } 027 028 public void addPattern(String pattern) { 029 patterns.add(Pattern.compile(pattern)); 030 } 031 032 public boolean accept(File file) { 033 for (Iterator ite = patterns.iterator(); ite.hasNext();) { 034 Pattern pattern = (Pattern) ite.next(); 035 if (pattern.matcher(file.getName()).matches()) { 036 return true; 037 } 038 } 039 return false; 040 } 041 042 public boolean accept(File dir, String name) { 043 for (Iterator ite = patterns.iterator(); ite.hasNext();) { 044 Pattern pattern = (Pattern) ite.next(); 045 if (pattern.matcher(name).matches()) { 046 return true; 047 } 048 } 049 return false; 050 } 051 }