1 16 package org.apache.cocoon.woody.datatype.validationruleimpl; 17 18 import org.apache.cocoon.woody.datatype.ValidationError; 19 import org.apache.cocoon.woody.util.I18nMessage; 20 import org.apache.cocoon.woody.Constants; 21 import org.apache.oro.text.regex.Pattern; 22 import org.apache.oro.text.regex.PatternMatcher; 23 import org.apache.oro.text.regex.Perl5Matcher; 24 import org.outerj.expression.ExpressionContext; 25 26 27 35 public class RegExpValidationRule extends AbstractValidationRule { 36 37 private Pattern pattern; 38 39 private String regexp; 40 41 public ValidationError validate(Object value, ExpressionContext expressionContext) { 42 String string = (String )value; 43 44 if(matchesRegExp(string)) 45 return null; 46 else 47 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.regexp", new String [] {regexp}, Constants.I18N_CATALOGUE)); 48 } 49 50 private boolean matchesRegExp(String string) { 51 PatternMatcher matcher = new Perl5Matcher(); 52 return matcher.matches(string, pattern); 53 } 54 55 void setPattern(String regexp, Pattern pattern) { 56 this.regexp = regexp; 57 this.pattern = pattern; 58 } 59 60 61 public boolean supportsType(Class clazz, boolean arrayType) { 62 return clazz.isAssignableFrom(String .class) && !arrayType; 63 } 64 } 65 | Popular Tags |