KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > RegexFilter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: RegexFilter.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 /**
27  * filter out all but files that match a given Regex Pattern,
28  */

29 public class RegexFilter implements FilenameFilter JavaDoc {
30
31     private Pattern JavaDoc pattern;
32
33    /**
34     * constructor
35     *
36     * @param pattern Regex pattern applied to simple filename
37     */

38    public RegexFilter(String JavaDoc pattern)
39       {
40       if (pattern != null)
41          {
42          this.pattern = Pattern.compile(pattern);
43          }
44       }
45
46
47    /**
48     * Select only files that match the Regex Pattern.
49     *
50     * @param dir the directory in which the file was found.
51     * @param name the simple name of the file
52     * @return true if and only if the name should be included in the file list; false otherwise.
53     */

54    public boolean accept(File JavaDoc dir, String JavaDoc name)
55       {
56       if (!(new File JavaDoc(dir, name).isFile())) return false;
57
58       if (pattern != null && !(pattern.matcher(name).matches())) return false;
59       
60       return true;
61       }
62       
63 }
64
65
Popular Tags