01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2008 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id$
23: */
24: package com.bostechcorp.cbesb.runtime.file;
25:
26: import java.io.File;
27: import java.io.FilenameFilter;
28: import java.util.regex.Matcher;
29: import java.util.regex.Pattern;
30: import java.util.regex.PatternSyntaxException;
31:
32: import org.apache.commons.logging.Log;
33: import org.apache.commons.logging.LogFactory;
34:
35: public class RegexFilenameFilter implements FilenameFilter {
36:
37: private final Log log = LogFactory.getLog(this .getClass());
38:
39: private String pattern = ".*";
40: private Pattern regexPattern = null;
41:
42: public RegexFilenameFilter() {
43: regexPattern = Pattern.compile(pattern);
44: }
45:
46: public String getPattern() {
47: return pattern;
48: }
49:
50: public void setPattern(String pattern) {
51: this .pattern = pattern;
52: try {
53: this .regexPattern = Pattern.compile(pattern);
54: } catch (PatternSyntaxException e) {
55: log.error("Invalid File Pattern specified. ", e);
56: }
57: }
58:
59: /* (non-Javadoc)
60: * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
61: */
62: public boolean accept(File dir, String name) {
63: Matcher matcher = regexPattern.matcher(name);
64: boolean matches = matcher.matches();
65: if (log.isDebugEnabled())
66: log
67: .debug("File: " + name + " matches pattern = "
68: + matches);
69: return matches;
70: }
71:
72: }
|