1 /*
2 * Fsgrep is a simple Java application which allows a user to
3 * search all files in a directory structure for lines matching
4 * a given pattern. Its functionality is a combination of the
5 * Unix 'find' and 'grep' utilities.
6 * Visit [http://fsgrep.sourceforge.net/] for more information.
7 *
8 * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9 *
10 * Fsgrep is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * Fsgrep is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Fsgrep (see the file named LICENSE.txt); if not, write
21 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22 * Boston, MA 02111-1307 USA
23 */
24
25 package mk.fsgrep.find;
26
27 import java.io.*;
28 import java.util.regex.*;
29
30 /***
31 * Filter directory contents based on the file suffix.
32 *
33 * @author Murali Krishnan
34 *
35 */
36 public class PatternFilter implements FilenameFilter {
37
38 //------------------------------------------------------------
39 //- Class Variables
40
41
42
43 //------------------------------------------------------------
44 //- Class Functions
45
46
47
48 //------------------------------------------------------------
49 //- Instance Variables
50
51 private Pattern _re = null;
52 private String _pattern = null;
53
54
55 //------------------------------------------------------------
56 //- Constructors
57
58 public PatternFilter(String pPattern) {
59 _pattern = pPattern;
60 try {
61 _re = Pattern.compile(pPattern, Pattern.CASE_INSENSITIVE);
62 } catch (PatternSyntaxException pse) {
63 System.err.println("Syntax error creating a regexp from '" +
64 pPattern + "'.");
65 pse.printStackTrace();
66 }
67 }
68
69
70 //------------------------------------------------------------
71 //- Accessors
72
73 public Pattern getRE() {return _re;}
74 protected String getPattern() {return _pattern;}
75
76
77
78 //------------------------------------------------------------
79 //- Settors
80
81
82
83 //------------------------------------------------------------
84 //- Private/Protected Utility Functions
85
86
87
88 //------------------------------------------------------------
89 //- Public Interface Functions
90
91 public String toString() {
92 String result = "PatternFilter[" + getPattern() + "]";
93
94 return result;
95 }
96
97
98
99 //------------------------------------------------------------
100 //- Class Interface Functions
101
102 public boolean accept(File dir, String name) {
103 boolean result = false; // Pessimistic.
104
105 if (getRE() != null) {
106 result = getRE().matcher(name).find();
107 }
108
109 return result;
110 }
111
112 //------------------------------------------------------------
113 //- Inner Classes
114
115
116
117 //------------------------------------------------------------
118 //- Main
119
120
121
122 }