|
| 1 | +package org.ocpsoft.prettyfaces.annotation.handlers; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | + |
| 5 | +import org.ocpsoft.logging.Logger; |
| 6 | +import org.ocpsoft.prettyfaces.annotation.ValidateRegExp; |
| 7 | +import org.ocpsoft.rewrite.annotation.api.FieldContext; |
| 8 | +import org.ocpsoft.rewrite.annotation.spi.FieldAnnotationHandler; |
| 9 | +import org.ocpsoft.rewrite.param.Parameter; |
| 10 | +import org.ocpsoft.rewrite.param.RegexConstraint; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Christian Kaltepoth |
| 14 | + */ |
| 15 | +public class ValidateRegExpHandler extends FieldAnnotationHandler<ValidateRegExp> |
| 16 | +{ |
| 17 | + private final Logger log = Logger.getLogger(ValidateRegExpHandler.class); |
| 18 | + |
| 19 | + @Override |
| 20 | + public Class<ValidateRegExp> handles() |
| 21 | + { |
| 22 | + return ValidateRegExp.class; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public int priority() |
| 27 | + { |
| 28 | + return HandlerConstants.WEIGHT_TYPE_ENRICHING; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 33 | + public void process(FieldContext context, Field field, ValidateRegExp annotation) |
| 34 | + { |
| 35 | + |
| 36 | + // obtain the parameter for the current field |
| 37 | + Parameter parameter = (Parameter) context.get(Parameter.class); |
| 38 | + if (parameter == null) { |
| 39 | + throw new IllegalStateException("Cound not find any binding for field: " + field.getName()); |
| 40 | + } |
| 41 | + |
| 42 | + // add a corresponding RegexConstraint |
| 43 | + String expr = annotation.value(); |
| 44 | + parameter.constrainedBy(new RegexConstraint(expr)); |
| 45 | + |
| 46 | + if (log.isTraceEnabled()) { |
| 47 | + log.trace("Parameter [{}] has been constrained by [{}]", parameter.getName(), expr); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments