View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.boolex;
11  
12  import java.util.regex.Pattern;
13  import java.util.regex.PatternSyntaxException;
14  
15  import ch.qos.logback.core.spi.ContextAwareBase;
16  import ch.qos.logback.core.spi.LifeCycle;
17  
18  public class Matcher extends ContextAwareBase implements LifeCycle {
19  
20    private String regex;
21    private String name;
22    private boolean caseSensitive = true;
23    private boolean canonEq = false;
24    private boolean unicodeCase = false;
25  
26    private boolean start = false;
27    private Pattern pattern;
28  
29    public String getRegex() {
30      return regex;
31    }
32  
33    public void setRegex(String regex) {
34      this.regex = regex;
35    }
36  
37    public void start() {
38      if(name ==  null) {
39        addError("All Matcher objects must be named");
40        return;
41      }
42      try {
43        int code = 0;
44        if(!caseSensitive) {
45          code |= Pattern.CASE_INSENSITIVE; 
46        }
47        if(canonEq) {
48          code |= Pattern.CANON_EQ;
49        }
50        if(unicodeCase) {
51          code |= Pattern.UNICODE_CASE; 
52        }
53        
54        //code |= Pattern.DOTALL;
55        
56        pattern = Pattern.compile(regex, code);
57        start = true;
58      } catch (PatternSyntaxException pse) {
59        addError("Failed to compile regex [" + regex + "]", pse);
60      }
61    }
62  
63    public void stop() {
64      start = false;
65    }
66  
67    public boolean isStarted() {
68      return start;
69    }
70  
71    //However, this method does 
72    //not require that the entire region (of the input) be matched.
73    
74    /**
75     * Checks whether the input matches the regular expression. 
76     * 
77     * @param input
78     * @return
79     * @throws EvaluationException
80     */
81    public boolean matches(String input) throws EvaluationException {
82      if(start) {
83        java.util.regex.Matcher matcher = pattern.matcher(input);
84        return matcher.find();
85      } else {
86        throw new EvaluationException("Matcher ["+regex+"] not started");
87      }
88    }
89  
90    public boolean isCanonEq() {
91      return canonEq;
92    }
93  
94    public void setCanonEq(boolean canonEq) {
95      this.canonEq = canonEq;
96    }
97  
98    public boolean isCaseSensitive() {
99      return caseSensitive;
100   }
101 
102   public void setCaseSensitive(boolean caseSensitive) {
103     this.caseSensitive = caseSensitive;
104   }
105 
106   public boolean isUnicodeCase() {
107     return unicodeCase;
108   }
109 
110   public void setUnicodeCase(boolean unicodeCase) {
111     this.unicodeCase = unicodeCase;
112   }
113 
114   public String getName() {
115     return name;
116   }
117 
118   public void setName(String name) {
119     this.name = name;
120   }
121 }