001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)InputFileFilter.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.file.util;
030:
031: import java.io.File;
032: import java.io.FilenameFilter;
033:
034: import java.util.regex.Pattern;
035:
036: /**
037: * Filter class to select input files.
038: *
039: * @author Sun Microsystems, Inc.
040: */
041: public class InputFileFilter implements FilenameFilter {
042: /**
043: * Creates a new instance of InputFileFilter.
044: */
045: private Pattern mRegex = null;
046:
047: /**
048: * Creates a new InputFileFilter object.
049: */
050: public InputFileFilter() {
051: }
052:
053: /**
054: * Sets the filter expression to be used. This is nothing but the regex
055: * present in the config file.
056: *
057: * @param regex regular expression
058: */
059: public void setFilterexpression(String regex) {
060: if ((regex == null) || regex.trim().equals("")) {
061: mRegex = Pattern.compile(".*");
062: } else {
063: try {
064: mRegex = Pattern.compile(regex);
065: } catch (Exception e) {
066: e.printStackTrace();
067: mRegex = Pattern.compile(".*");
068: }
069: }
070: }
071:
072: /**
073: * Returns the filter expression.
074: *
075: * @return filter expression
076: */
077: public String getFilterexpression() {
078: return mRegex.pattern();
079: }
080:
081: /**
082: * Returns true if the file needs to be included in the file list for the
083: * directory dir.
084: *
085: * @param dir directory / file
086: * @param name pattern
087: *
088: * @return true if included false otherwise
089: */
090: public boolean accept(File dir, String name) {
091: File tmpFile = new File(name);
092: String f = tmpFile.getName();
093:
094: /* Check if the file name matches the pattern specified
095: in the configuration file. Also check if the file is
096: is a file or a folder.
097: */
098: return (mRegex.matcher(f).matches());
099: }
100: }
|