View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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              // code |= Pattern.DOTALL;
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      // However, this method does
76      // not require that the entire region (of the input) be matched.
77  
78      /**
79       * Checks whether the input matches the regular expression. 
80       * 
81       * @param input
82       * @return
83       * @throws EvaluationException
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 }