1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.boolex;
15
16 import java.util.regex.Pattern;
17 import java.util.regex.PatternSyntaxException;
18
19 import ch.qos.logback.core.spi.ContextAwareBase;
20 import ch.qos.logback.core.spi.LifeCycle;
21
22 public class Matcher extends ContextAwareBase implements LifeCycle {
23
24 private String regex;
25 private String name;
26 private boolean caseSensitive = true;
27 private boolean canonEq = false;
28 private boolean unicodeCase = false;
29
30 private boolean start = false;
31 private Pattern pattern;
32
33 public String getRegex() {
34 return regex;
35 }
36
37 public void setRegex(String regex) {
38 this.regex = regex;
39 }
40
41 public void start() {
42 if (name == null) {
43 addError("All Matcher objects must be named");
44 return;
45 }
46 try {
47 int code = 0;
48 if (!caseSensitive) {
49 code |= Pattern.CASE_INSENSITIVE;
50 }
51 if (canonEq) {
52 code |= Pattern.CANON_EQ;
53 }
54 if (unicodeCase) {
55 code |= Pattern.UNICODE_CASE;
56 }
57
58
59
60 pattern = Pattern.compile(regex, code);
61 start = true;
62 } catch (PatternSyntaxException pse) {
63 addError("Failed to compile regex [" + regex + "]", pse);
64 }
65 }
66
67 public void stop() {
68 start = false;
69 }
70
71 public boolean isStarted() {
72 return start;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public boolean matches(String input) throws EvaluationException {
86 if (start) {
87 java.util.regex.Matcher matcher = pattern.matcher(input);
88 return matcher.find();
89 } else {
90 throw new EvaluationException("Matcher [" + regex + "] not started");
91 }
92 }
93
94 public boolean isCanonEq() {
95 return canonEq;
96 }
97
98 public void setCanonEq(boolean canonEq) {
99 this.canonEq = canonEq;
100 }
101
102 public boolean isCaseSensitive() {
103 return caseSensitive;
104 }
105
106 public void setCaseSensitive(boolean caseSensitive) {
107 this.caseSensitive = caseSensitive;
108 }
109
110 public boolean isUnicodeCase() {
111 return unicodeCase;
112 }
113
114 public void setUnicodeCase(boolean unicodeCase) {
115 this.unicodeCase = unicodeCase;
116 }
117
118 public String getName() {
119 return name;
120 }
121
122 public void setName(String name) {
123 this.name = name;
124 }
125 }