Skip to content

Commit 6296c49

Browse files
committed
Add basic support for include/exclude + files/dirs filters
1 parent 0add7fc commit 6296c49

File tree

6 files changed

+216
-60
lines changed

6 files changed

+216
-60
lines changed

config/config.xml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,43 @@
33
<filters>
44
<filter>
55
<title>Windows Thumbnails DB Files (Thumbs.db)</title>
6-
<type>Simple</type>
7-
<match>Filename</match>
6+
<policy>Exclude</policy>
7+
<filetype>Files</filetype>
88
<pattern>Thumbs.db</pattern>
9+
<match>Filename</match>
10+
<type>Simple</type>
911
</filter>
1012
<filter>
1113
<title>DVD Information Files (*.IFO, *.BUP)</title>
12-
<type>Regexp</type>
13-
<match>Filename</match>
14+
<policy>Exclude</policy>
15+
<filetype>Files</filetype>
1416
<pattern><![CDATA[.*\.(IFO|BUP)]]></pattern>
17+
<match>Filename</match>
18+
<type>Regexp</type>
1519
</filter>
1620
<filter>
17-
<title>(UNIX) Files under hidden directory</title>
18-
<type>Regexp</type>
19-
<match>Path</match>
20-
<pattern><![CDATA[.*/\..*/.*$]]></pattern>
21+
<title>(UNIX) Hidden directories content</title>
22+
<policy>Exclude</policy>
23+
<filetype>Directories</filetype>
24+
<pattern><![CDATA[.*]]></pattern>
25+
<match>Filename</match>
26+
<type>Simple</type>
2127
</filter>
2228
<filter>
2329
<title>Audio files</title>
24-
<type>Regexp</type>
25-
<match>Filename</match>
30+
<policy>Include</policy>
31+
<filetype>Files</filetype>
2632
<pattern><![CDATA[\.(mp3|wav|ogg|flac|m4a|aac|ra|rm|mpc|m4p|3gp|wma)$]]></pattern>
33+
<match>Filename</match>
34+
<type>Regexp</type>
2735
</filter>
2836
<filter>
2937
<title>Video files</title>
30-
<type>Regexp</type>
31-
<match>Filename</match>
38+
<policy>Include</policy>
39+
<filetype>Files</filetype>
3240
<pattern><![CDATA[\.(avi|mpg|divx|mp4|mkv|wmv|flv|vob|ogv|mov|rm)$]]></pattern>
41+
<match>Filename</match>
42+
<type>Regexp</type>
3343
</filter>
3444
</filters>
3545
</jduplicatefinder>

src/nnwl/jduplicatefinder/engine/FileFilter.java

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.log4j.Logger;
44

5+
import java.io.File;
56
import java.nio.file.Path;
67
import java.util.regex.Pattern;
78

@@ -15,6 +16,15 @@ public class FileFilter {
1516

1617
private static final Logger logger = Logger.getLogger(FileFilter.class);
1718

19+
public static final String POLICY_EXCLUDE = "Exclude";
20+
public static final String POLICY_INCLUDE = "Include";
21+
public static final String[] POLICY_CHOICES = {POLICY_EXCLUDE, POLICY_INCLUDE};
22+
23+
public static final String FILETYPE_FILES = "Files";
24+
public static final String FILETYPE_DIRS = "Directories";
25+
public static final String FILETYPE_ALL = "All";
26+
public static final String[] FILETYPE_CHOICES = {FILETYPE_FILES, FILETYPE_DIRS, FILETYPE_ALL};
27+
1828
public static final String MATCH_FILENAME = "Filename";
1929
public static final String MATCH_PATH = "Path";
2030
public static final String[] MATCH_CHOICES = {MATCH_FILENAME, MATCH_PATH};
@@ -28,6 +38,10 @@ public class FileFilter {
2838

2939
protected String title;
3040

41+
protected String policy;
42+
43+
protected String filetype;
44+
3145
protected String userPattern;
3246

3347
protected String matches;
@@ -41,9 +55,19 @@ public FileFilter() {
4155
}
4256

4357
public FileFilter(String pattern, String matches, String type) {
58+
this(pattern, matches, type, POLICY_EXCLUDE);
59+
}
60+
61+
public FileFilter(String pattern, String matches, String type, String policy) {
62+
this(pattern, matches, type, FILETYPE_FILES, policy);
63+
}
64+
65+
public FileFilter(String pattern, String matches, String type, String filetype, String policy) {
4466
this.setType(type);
4567
this.setPattern(pattern);
4668
this.setMatches(matches);
69+
this.setFiletype(filetype);
70+
this.setPolicy(policy);
4771
}
4872

4973
public Pattern compilePattern() {
@@ -75,7 +99,19 @@ protected static String escapeSimplePattern(String p) {
7599
return sb.toString();
76100
}
77101

78-
public boolean matches(Path p) {
102+
public boolean shouldInclude(File f) {
103+
// Preliminary filter on filetype
104+
if (this.filetype.equals(FILETYPE_FILES) && f.isDirectory()) {
105+
return true;
106+
}
107+
if (this.filetype.equals(FILETYPE_DIRS) && !f.isDirectory()) {
108+
return true;
109+
}
110+
111+
return this.shouldInclude(f.toPath());
112+
}
113+
114+
public boolean shouldInclude(Path p) {
79115
String subject;
80116
switch (this.matches) {
81117
case MATCH_FILENAME:
@@ -87,15 +123,31 @@ public boolean matches(Path p) {
87123
default:
88124
throw new IllegalArgumentException(this.matches + " is not a valid value for matches.");
89125
}
126+
90127
boolean matches = this.getRegexpPattern().matcher(subject).find();
128+
boolean shouldInclude;
129+
130+
// Invert result depending on policy
131+
switch (this.policy) {
132+
case POLICY_EXCLUDE:
133+
shouldInclude = !matches;
134+
break;
135+
case POLICY_INCLUDE:
136+
shouldInclude = matches;
137+
break;
138+
default:
139+
throw new IllegalArgumentException(this.policy + " is not a valid value for policy.");
140+
}
141+
91142
if (logger.isDebugEnabled()) {
92-
if (matches) {
93-
logger.debug(this.toString() + " MATCHES " + subject);
143+
if (shouldInclude) {
144+
logger.debug(this.toString() + " SHOULD include " + subject);
94145
} else {
95-
logger.debug(this.toString() + " does NOT MATCH " + subject);
146+
logger.debug(this.toString() + " should NOT include " + subject);
96147
}
97148
}
98-
return matches;
149+
150+
return shouldInclude;
99151
}
100152

101153
public String getTitle() {
@@ -106,6 +158,22 @@ public void setTitle(String title) {
106158
this.title = title;
107159
}
108160

161+
public String getPolicy() {
162+
return policy;
163+
}
164+
165+
public void setPolicy(String policy) {
166+
this.policy = policy;
167+
}
168+
169+
public String getFiletype() {
170+
return filetype;
171+
}
172+
173+
public void setFiletype(String filetype) {
174+
this.filetype = filetype;
175+
}
176+
109177
public String getPattern() {
110178
return userPattern;
111179
}
@@ -140,8 +208,17 @@ public void setType(String type) {
140208

141209
public String toString() {
142210
StringBuffer sb = new StringBuffer(FileFilter.class.toString());
143-
sb.append(" [Pattern=\"").append(this.userPattern).append("\", Matches=").append(this.matches)
144-
.append(", Type=").append(this.type).append("]");
211+
sb.append("[")
212+
.append(this.policy.toUpperCase())
213+
.append(" Filetype=")
214+
.append(this.filetype)
215+
.append(", Pattern=\"")
216+
.append(this.userPattern)
217+
.append("\", Matches=")
218+
.append(this.matches)
219+
.append(", Type=")
220+
.append(this.type)
221+
.append("]");
145222
return sb.toString();
146223
}
147224
}

src/nnwl/jduplicatefinder/engine/Runner.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ public Set<Path> listFilesRecursively(Set<Path> folders) throws Exception {
172172
}
173173

174174
if (f.isDirectory() && this.recurseSubdirectories) {
175-
if (this.matchesFilter(f.toPath())) {
175+
if (!this.shouldAnalyze(f)) {
176176
logger.debug("Skipping filtered directory: " + f.getAbsolutePath());
177177
} else {
178178
dirs.add(f.toPath());
179179
}
180180
} else if (f.isFile()) {
181181
totalFiles++;
182-
if (this.matchesFilter(f.toPath())) {
182+
if (!this.shouldAnalyze(f)) {
183183
logger.debug("Skipping filtered path: " + f.getAbsolutePath());
184184
this.filteredFiles.add(f.toPath());
185185
} else {
@@ -318,15 +318,15 @@ public void exceptionCaught(ComparatorExceptionEvent ex) {
318318
return results;
319319
}
320320

321-
protected boolean matchesFilter(Path p) {
321+
protected boolean shouldAnalyze(File f) {
322322
if (this.fileFilters != null) {
323323
for (FileFilter ff : this.fileFilters) {
324-
if (ff.matches(p)) {
325-
return true;
324+
if (!ff.shouldInclude(f)) {
325+
return false;
326326
}
327327
}
328328
}
329-
return false;
329+
return true;
330330
}
331331

332332
protected RunnerEvent getNewEvent() {

src/nnwl/jduplicatefinder/ui/App.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
public class App {
5353
public static String APP_NAME = "JDuplicateFinder";
5454

55-
public static String APP_VERSION = "1.5";
55+
public static String APP_VERSION = "1.6";
5656

5757
private static final Logger logger = Logger.getLogger(App.class);
5858

@@ -147,8 +147,13 @@ protected void loadConfiguration() {
147147
List<HierarchicalConfiguration> filterNodes = config.configurationsAt("filters.filter");
148148
List<FileFilter> filters = new ArrayList<>();
149149
for (HierarchicalConfiguration filterNode : filterNodes) {
150-
FileFilter ff = new FileFilter(filterNode.getString("pattern"), filterNode.getString("match"),
151-
filterNode.getString("type"));
150+
FileFilter ff = new FileFilter(
151+
filterNode.getString("pattern"),
152+
filterNode.getString("match"),
153+
filterNode.getString("type"),
154+
filterNode.getString("filetype"),
155+
filterNode.getString("policy")
156+
);
152157
ff.setTitle(filterNode.getString("title"));
153158
filters.add(ff);
154159
}
@@ -207,7 +212,7 @@ private void initialize() {
207212
@Override
208213
public void mouseClicked(MouseEvent e) {
209214
try {
210-
Desktop.getDesktop().browse(new URI("https://www.lanterne-rouge.info/"));
215+
Desktop.getDesktop().browse(new URI("https://github.com/nanawel/jduplicatefinder"));
211216
} catch (Exception e1) {
212217
}
213218
}
@@ -224,7 +229,7 @@ public void mouseExited(MouseEvent e) {
224229
lblAbout.setCursor(Cursor.getDefaultCursor());
225230
}
226231
});
227-
lblAbout.setToolTipText("Go to author's blog");
232+
lblAbout.setToolTipText("Go to Git repository");
228233
toolBar.add(lblAbout);
229234

230235
// //////////

0 commit comments

Comments
 (0)