1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.find;
26
27
28 import java.io.*;
29 import java.util.*;
30
31
32 /***
33 * The representation of the set of files to search. It encapsulates the
34 * starting directory, and the set of suffixes of files to include.
35 *
36 * @author Murali Krishnan
37 *
38 */
39 public class ScanProfile {
40
41
42
43
44
45
46
47
48
49 protected static String makeRegexpFromSuffix(String pSuffix) {
50 String result = pSuffix.trim();
51
52 while (result.startsWith(".")) {
53 result = result.substring(1);
54 }
55
56 result = "//." + result + "$";
57
58 return result;
59 }
60
61
62 protected static String makeRegexpFromSuffixList(String pSuffixes) {
63 String result;
64
65 if (pSuffixes.indexOf(",") == -1) {
66 result = makeRegexpFromSuffix(pSuffixes);
67 } else {
68 boolean first = true;
69 StringBuffer buffer = new StringBuffer("//.(");
70 StringTokenizer tokenizer = new StringTokenizer(pSuffixes, ",");
71 while (tokenizer.hasMoreTokens()) {
72 if (first) {
73 first = false;
74 } else {
75 buffer.append("|");
76 }
77 String token = tokenizer.nextToken().trim();
78 buffer.append(token);
79 }
80 buffer.append(")$");
81
82 result = buffer.toString();
83 }
84
85 return result;
86 }
87
88
89
90
91
92 private File _root = null;
93 private String _filenameSpec = null;
94 private FilenameFilter _filter = null;
95
96
97
98
99
100
101 public ScanProfile(String pFullPattern) {
102 _root = new File(pFullPattern.substring(0, pFullPattern.indexOf("**")));
103 {
104 int dot = pFullPattern.lastIndexOf(".")+1;
105 String pPattern = pFullPattern.substring(dot);
106 if (pPattern.startsWith("{")) {
107 pPattern = pPattern.substring(1);
108 }
109 if (pPattern.endsWith("}")) {
110 pPattern = pPattern.substring(0, (pPattern.length()-1));
111 }
112
113 _filenameSpec = pPattern;
114 }
115
116 setFilter(_filenameSpec);
117 }
118
119 public ScanProfile(File pFile, String pPattern) {
120 _root = pFile;
121 _filenameSpec = pPattern;
122
123 setFilter(pPattern);
124 }
125
126 public ScanProfile(String pFile, String pPattern) {
127 this(new File(pFile), pPattern);
128 }
129
130
131
132
133
134
135 public File getRoot() {return _root;}
136 public String getFilenameSpec() {return _filenameSpec;}
137 public FilenameFilter getFilter() {return _filter;}
138
139
140
141
142
143
144 protected void setFilenameSpec(String val) {_filenameSpec=val;}
145
146
147
148
149
150 protected void setFilter(String pPattern) {
151 if ("*".equals(pPattern)) {
152 _filter = new TextFileFilter();
153 } else {
154 _filter = new PatternFilter(makeRegexpFromSuffixList(pPattern));
155 }
156 }
157
158
159
160
161
162
163 public String toString() {
164 String result = "ScanProfile[" + createSpecification() + "]";
165
166 return result;
167 }
168
169
170 public String createSpecification() {
171 StringBuffer buffer = new StringBuffer();
172
173 try {
174 buffer.append(getRoot().getCanonicalPath());
175 } catch (IOException ioe) {
176 buffer.append(getRoot().getAbsolutePath());
177 }
178
179 if (!buffer.toString().endsWith(File.separator)) {
180 buffer.append(File.separator);
181 }
182
183 buffer.append("**").append(File.separator).append("*.");
184 if (getFilenameSpec().indexOf(",") == -1) {
185 buffer.append(getFilenameSpec());
186 } else {
187 buffer.append("{").append(getFilenameSpec()).append("}");
188 }
189
190 return buffer.toString();
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 }