001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.boolex; 015 016import java.util.regex.Pattern; 017import java.util.regex.PatternSyntaxException; 018 019import ch.qos.logback.core.spi.ContextAwareBase; 020import ch.qos.logback.core.spi.LifeCycle; 021 022public class Matcher extends ContextAwareBase implements LifeCycle { 023 024 private String regex; 025 private String name; 026 private boolean caseSensitive = true; 027 private boolean canonEq = false; 028 private boolean unicodeCase = false; 029 030 private boolean start = false; 031 private Pattern pattern; 032 033 public String getRegex() { 034 return regex; 035 } 036 037 public void setRegex(String regex) { 038 this.regex = regex; 039 } 040 041 public void start() { 042 if (name == null) { 043 addError("All Matcher objects must be named"); 044 return; 045 } 046 try { 047 int code = 0; 048 if (!caseSensitive) { 049 code |= Pattern.CASE_INSENSITIVE; 050 } 051 if (canonEq) { 052 code |= Pattern.CANON_EQ; 053 } 054 if (unicodeCase) { 055 code |= Pattern.UNICODE_CASE; 056 } 057 058 // code |= Pattern.DOTALL; 059 060 pattern = Pattern.compile(regex, code); 061 start = true; 062 } catch (PatternSyntaxException pse) { 063 addError("Failed to compile regex [" + regex + "]", pse); 064 } 065 } 066 067 public void stop() { 068 start = false; 069 } 070 071 public boolean isStarted() { 072 return start; 073 } 074 075 // However, this method does 076 // not require that the entire region (of the input) be matched. 077 078 /** 079 * Checks whether the input matches the regular expression. 080 * 081 * @param input 082 * @return 083 * @throws EvaluationException 084 */ 085 public boolean matches(String input) throws EvaluationException { 086 if (start) { 087 java.util.regex.Matcher matcher = pattern.matcher(input); 088 return matcher.find(); 089 } else { 090 throw new EvaluationException("Matcher [" + regex + "] not started"); 091 } 092 } 093 094 public boolean isCanonEq() { 095 return canonEq; 096 } 097 098 public void setCanonEq(boolean canonEq) { 099 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}