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:
29: import org.apache.commons.logging.Log;
30: import org.apache.commons.logging.LogFactory;
31: import org.apache.oro.text.GlobCompiler;
32: import org.apache.oro.text.regex.MalformedPatternException;
33: import org.apache.oro.text.regex.Pattern;
34: import org.apache.oro.text.regex.PatternMatcher;
35: import org.apache.oro.text.regex.Perl5Matcher;
36:
37: public class GlobFilenameFilter implements FilenameFilter {
38:
39: private final Log log = LogFactory.getLog(this .getClass());
40:
41: private String pattern = ".*";
42: private Pattern globPattern;
43: private PatternMatcher matcher;
44:
45: public GlobFilenameFilter() {
46: this .setPattern(pattern);
47: matcher = new Perl5Matcher();
48: }
49:
50: public String getPattern() {
51: return pattern;
52: }
53:
54: public void setPattern(String pattern) {
55: this .pattern = pattern;
56: GlobCompiler compiler = new GlobCompiler();
57: try {
58: globPattern = compiler.compile(pattern);
59: } catch (MalformedPatternException e) {
60: log.error("Invalid File Pattern specified. ", e);
61: }
62: }
63:
64: /* (non-Javadoc)
65: * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
66: */
67: public boolean accept(File dir, String name) {
68: boolean matches = matcher.matches(name, globPattern);
69: if (log.isDebugEnabled())
70: log
71: .debug("File: " + name + " matches pattern = "
72: + matches);
73: return matches;
74: }
75:
76: }
|