1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.settings4j.settings;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.regex.Pattern;
22
23 import org.settings4j.Filter;
24
25
26
27
28
29
30 public class DefaultFilter implements Filter {
31
32
33 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultFilter.class);
34
35 private final List<Pattern> includePatterns = new ArrayList<Pattern>();
36 private final List<Pattern> excludePatterns = new ArrayList<Pattern>();
37
38
39 public void addExclude(final String pattern) {
40 try {
41 final Pattern p = Pattern.compile(pattern);
42 this.excludePatterns.add(p);
43 } catch (final Exception e) {
44 LOG.warn("cannnot compile Pattern-String '" + pattern + "'" + e.getMessage(), e);
45 }
46 }
47
48
49 public void addInclude(final String pattern) {
50 try {
51 final Pattern p = Pattern.compile(pattern);
52 this.includePatterns.add(p);
53 } catch (final Exception e) {
54 LOG.warn("cannnot compile Pattern-String '" + pattern + "'" + e.getMessage(), e);
55 }
56 }
57
58
59 public boolean isValid(final String key) {
60
61 for (final Pattern pattern : this.excludePatterns) {
62 if (pattern.matcher(key).matches()) {
63 return false;
64 }
65 }
66
67
68 if (this.includePatterns.isEmpty()) {
69 return true;
70 }
71
72
73 for (final Pattern pattern : this.includePatterns) {
74 if (pattern.matcher(key).matches()) {
75 return true;
76 }
77 }
78
79
80 return false;
81 }
82
83 }