1 /* 2 * Copyright 2010 Werner Guttmann 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.exolab.castor.util; 17 18 import java.util.regex.Pattern; 19 import java.util.regex.PatternSyntaxException; 20 21 /** 22 * An implementation of {@link RegExpEvaluator} that uses the Java Regular 23 * Expression library. 24 * 25 * @author <a href="mailto:george76@hotmail.com">George Varghese</a> 26 * @since 1.3.2 27 **/ 28 public class SunRegExpEvaluator implements RegExpEvaluator { 29 30 /** 31 * The Regular expression 32 **/ 33 private Pattern _pattern; 34 35 /** 36 * Sets the regular expression to match against during a call to #matches 37 * 38 * @param rexpr 39 * the regular expression 40 **/ 41 public void setExpression(String rexpr) { 42 43 if (rexpr != null) { 44 try { 45 // -- patch and compile expression 46 _pattern = Pattern.compile(rexpr); 47 } catch (PatternSyntaxException ex) { 48 String err = "RegExp Syntax error: "; 49 err += ex.getMessage(); 50 err += " ; error occured with the following " 51 + "regular expression: " + rexpr; 52 53 throw new IllegalArgumentException(err, ex); 54 } 55 } 56 } 57 58 /** 59 * Returns true if the given String is matched by the regular expression of 60 * this RegExpEvaluator 61 * 62 * @param value 63 * the String to check the production of 64 * @return true if the given string matches the regular expression of this 65 * RegExpEvaluator 66 * @see #setExpression 67 **/ 68 public boolean matches(String value) { 69 if (_pattern != null) { 70 return _pattern.matcher(value).matches(); 71 } 72 return true; 73 } 74 75 } 76