|
1 |
| -package nnwl.jduplicatefinder.engine; |
2 |
| - |
3 |
| -import org.apache.log4j.Logger; |
4 |
| - |
5 |
| -import java.nio.file.Path; |
6 |
| -import java.util.regex.Pattern; |
7 |
| - |
8 |
| -/** |
9 |
| - * JDuplicateFinder |
10 |
| - * |
11 |
| - * @author Anael Ollier <nanawel NOSPAM [at] gmail [dot] com> |
12 |
| - * @license GPLv3 - See LICENSE |
13 |
| - */ |
14 |
| -public class FileFilter { |
15 |
| - |
16 |
| - private static final Logger logger = Logger.getLogger(FileFilter.class); |
17 |
| - |
18 |
| - public static final String MATCH_FILENAME = "Filename"; |
19 |
| - public static final String MATCH_PATH = "Path"; |
20 |
| - public static final String[] MATCH_CHOICES = {MATCH_FILENAME, MATCH_PATH}; |
21 |
| - |
22 |
| - public static final String TYPE_SIMPLE = "Simple"; |
23 |
| - public static final String TYPE_REGEXP = "Regexp"; |
24 |
| - public static final String[] TYPE_CHOICES = {TYPE_SIMPLE, TYPE_REGEXP}; |
25 |
| - public static final String[] TYPE_TOOLTIPS = { |
26 |
| - "<html>Literal filter, but you can use \"*\" as a wildcard.<br/>Ex: \"*.jpg\" will find all filenames ending in \".jpg\"</html>", |
27 |
| - "Classic regexp"}; |
28 |
| - |
29 |
| - protected String title; |
30 |
| - |
31 |
| - protected String userPattern; |
32 |
| - |
33 |
| - protected String matches; |
34 |
| - |
35 |
| - protected String type; |
36 |
| - |
37 |
| - protected Pattern pattern; |
38 |
| - |
39 |
| - public FileFilter() { |
40 |
| - this("", MATCH_FILENAME, TYPE_SIMPLE); |
41 |
| - } |
42 |
| - |
43 |
| - public FileFilter(String pattern, String matches, String type) { |
44 |
| - this.setType(type); |
45 |
| - this.setPattern(pattern); |
46 |
| - this.setMatches(matches); |
47 |
| - } |
48 |
| - |
49 |
| - public Pattern compilePattern() { |
50 |
| - String finalPattern; |
51 |
| - switch (this.type) { |
52 |
| - case TYPE_SIMPLE: |
53 |
| - finalPattern = "^" + escapeSimplePattern(this.userPattern) + "$"; |
54 |
| - break; |
55 |
| - case TYPE_REGEXP: |
56 |
| - finalPattern = userPattern; |
57 |
| - break; |
58 |
| - default: |
59 |
| - throw new IllegalArgumentException(this.type + " is not a valid type of filter."); |
60 |
| - } |
61 |
| - return Pattern.compile(finalPattern); |
62 |
| - } |
63 |
| - |
64 |
| - protected static String escapeSimplePattern(String p) { |
65 |
| - String[] parts = p.split("\\*", -1); |
66 |
| - |
67 |
| - StringBuffer sb = new StringBuffer(); |
68 |
| - for (int i = 0; i < parts.length; i++) { |
69 |
| - String part = parts[i]; |
70 |
| - sb.append(Pattern.quote(part)); |
71 |
| - if (i < parts.length - 1) { |
72 |
| - sb.append(".*"); |
73 |
| - } |
74 |
| - } |
75 |
| - return sb.toString(); |
76 |
| - } |
77 |
| - |
78 |
| - public boolean matches(Path p) { |
79 |
| - String subject; |
80 |
| - switch (this.matches) { |
81 |
| - case MATCH_FILENAME: |
82 |
| - subject = p.getFileName().toString(); |
83 |
| - break; |
84 |
| - case MATCH_PATH: |
85 |
| - subject = p.toString(); |
86 |
| - break; |
87 |
| - default: |
88 |
| - throw new IllegalArgumentException(this.matches + " is not a valid value for matches."); |
89 |
| - } |
90 |
| - boolean matches = this.getRegexpPattern().matcher(subject).find(); |
91 |
| - if (logger.isDebugEnabled()) { |
92 |
| - if (matches) { |
93 |
| - logger.debug(this.toString() + " MATCHES " + subject); |
94 |
| - } else { |
95 |
| - logger.debug(this.toString() + " does NOT MATCH " + subject); |
96 |
| - } |
97 |
| - } |
98 |
| - return matches; |
99 |
| - } |
100 |
| - |
101 |
| - public String getTitle() { |
102 |
| - return title; |
103 |
| - } |
104 |
| - |
105 |
| - public void setTitle(String title) { |
106 |
| - this.title = title; |
107 |
| - } |
108 |
| - |
109 |
| - public String getPattern() { |
110 |
| - return userPattern; |
111 |
| - } |
112 |
| - |
113 |
| - public void setPattern(String pattern) { |
114 |
| - this.userPattern = pattern; |
115 |
| - this.pattern = null; |
116 |
| - } |
117 |
| - |
118 |
| - protected Pattern getRegexpPattern() { |
119 |
| - if (this.pattern == null) { |
120 |
| - this.pattern = this.compilePattern(); |
121 |
| - } |
122 |
| - return this.pattern; |
123 |
| - } |
124 |
| - |
125 |
| - public String getMatches() { |
126 |
| - return matches; |
127 |
| - } |
128 |
| - |
129 |
| - public void setMatches(String matches) { |
130 |
| - this.matches = matches; |
131 |
| - } |
132 |
| - |
133 |
| - public String getType() { |
134 |
| - return type; |
135 |
| - } |
136 |
| - |
137 |
| - public void setType(String type) { |
138 |
| - this.type = type; |
139 |
| - } |
140 |
| - |
141 |
| - public String toString() { |
142 |
| - 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("]"); |
145 |
| - return sb.toString(); |
146 |
| - } |
147 |
| -} |
| 1 | +package nnwl.jduplicatefinder.engine; |
| 2 | + |
| 3 | +import org.apache.log4j.Logger; |
| 4 | + |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +/** |
| 9 | + * JDuplicateFinder |
| 10 | + * |
| 11 | + * @author Anael Ollier <nanawel NOSPAM [at] gmail [dot] com> |
| 12 | + * @license GPLv3 - See LICENSE |
| 13 | + */ |
| 14 | +public class FileFilter { |
| 15 | + |
| 16 | + private static final Logger logger = Logger.getLogger(FileFilter.class); |
| 17 | + |
| 18 | + public static final String MATCH_FILENAME = "Filename"; |
| 19 | + public static final String MATCH_PATH = "Path"; |
| 20 | + public static final String[] MATCH_CHOICES = {MATCH_FILENAME, MATCH_PATH}; |
| 21 | + |
| 22 | + public static final String TYPE_SIMPLE = "Simple"; |
| 23 | + public static final String TYPE_REGEXP = "Regexp"; |
| 24 | + public static final String[] TYPE_CHOICES = {TYPE_SIMPLE, TYPE_REGEXP}; |
| 25 | + public static final String[] TYPE_TOOLTIPS = { |
| 26 | + "<html>Literal filter, but you can use \"*\" as a wildcard.<br/>Ex: \"*.jpg\" will find all filenames ending in \".jpg\"</html>", |
| 27 | + "Classic regexp"}; |
| 28 | + |
| 29 | + protected String title; |
| 30 | + |
| 31 | + protected String userPattern; |
| 32 | + |
| 33 | + protected String matches; |
| 34 | + |
| 35 | + protected String type; |
| 36 | + |
| 37 | + protected Pattern pattern; |
| 38 | + |
| 39 | + public FileFilter() { |
| 40 | + this("", MATCH_FILENAME, TYPE_SIMPLE); |
| 41 | + } |
| 42 | + |
| 43 | + public FileFilter(String pattern, String matches, String type) { |
| 44 | + this.setType(type); |
| 45 | + this.setPattern(pattern); |
| 46 | + this.setMatches(matches); |
| 47 | + } |
| 48 | + |
| 49 | + public Pattern compilePattern() { |
| 50 | + String finalPattern; |
| 51 | + switch (this.type) { |
| 52 | + case TYPE_SIMPLE: |
| 53 | + finalPattern = "^" + escapeSimplePattern(this.userPattern) + "$"; |
| 54 | + break; |
| 55 | + case TYPE_REGEXP: |
| 56 | + finalPattern = userPattern; |
| 57 | + break; |
| 58 | + default: |
| 59 | + throw new IllegalArgumentException(this.type + " is not a valid type of filter."); |
| 60 | + } |
| 61 | + return Pattern.compile(finalPattern); |
| 62 | + } |
| 63 | + |
| 64 | + protected static String escapeSimplePattern(String p) { |
| 65 | + String[] parts = p.split("\\*", -1); |
| 66 | + |
| 67 | + StringBuffer sb = new StringBuffer(); |
| 68 | + for (int i = 0; i < parts.length; i++) { |
| 69 | + String part = parts[i]; |
| 70 | + sb.append(Pattern.quote(part)); |
| 71 | + if (i < parts.length - 1) { |
| 72 | + sb.append(".*"); |
| 73 | + } |
| 74 | + } |
| 75 | + return sb.toString(); |
| 76 | + } |
| 77 | + |
| 78 | + public boolean matches(Path p) { |
| 79 | + String subject; |
| 80 | + switch (this.matches) { |
| 81 | + case MATCH_FILENAME: |
| 82 | + subject = p.getFileName().toString(); |
| 83 | + break; |
| 84 | + case MATCH_PATH: |
| 85 | + subject = p.toString(); |
| 86 | + break; |
| 87 | + default: |
| 88 | + throw new IllegalArgumentException(this.matches + " is not a valid value for matches."); |
| 89 | + } |
| 90 | + boolean matches = this.getRegexpPattern().matcher(subject).find(); |
| 91 | + if (logger.isDebugEnabled()) { |
| 92 | + if (matches) { |
| 93 | + logger.debug(this.toString() + " MATCHES " + subject); |
| 94 | + } else { |
| 95 | + logger.debug(this.toString() + " does NOT MATCH " + subject); |
| 96 | + } |
| 97 | + } |
| 98 | + return matches; |
| 99 | + } |
| 100 | + |
| 101 | + public String getTitle() { |
| 102 | + return title; |
| 103 | + } |
| 104 | + |
| 105 | + public void setTitle(String title) { |
| 106 | + this.title = title; |
| 107 | + } |
| 108 | + |
| 109 | + public String getPattern() { |
| 110 | + return userPattern; |
| 111 | + } |
| 112 | + |
| 113 | + public void setPattern(String pattern) { |
| 114 | + this.userPattern = pattern; |
| 115 | + this.pattern = null; |
| 116 | + } |
| 117 | + |
| 118 | + protected Pattern getRegexpPattern() { |
| 119 | + if (this.pattern == null) { |
| 120 | + this.pattern = this.compilePattern(); |
| 121 | + } |
| 122 | + return this.pattern; |
| 123 | + } |
| 124 | + |
| 125 | + public String getMatches() { |
| 126 | + return matches; |
| 127 | + } |
| 128 | + |
| 129 | + public void setMatches(String matches) { |
| 130 | + this.matches = matches; |
| 131 | + } |
| 132 | + |
| 133 | + public String getType() { |
| 134 | + return type; |
| 135 | + } |
| 136 | + |
| 137 | + public void setType(String type) { |
| 138 | + this.type = type; |
| 139 | + } |
| 140 | + |
| 141 | + public String toString() { |
| 142 | + 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("]"); |
| 145 | + return sb.toString(); |
| 146 | + } |
| 147 | +} |
0 commit comments