01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.constraint;
13:
14: import java.util.Map;
15: import java.util.regex.Pattern;
16:
17: import net.sf.oval.Validator;
18: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
19: import net.sf.oval.context.OValContext;
20: import net.sf.oval.internal.CollectionFactoryHolder;
21:
22: /**
23: * @author Sebastian Thomschke
24: */
25: public class MatchPatternCheck extends
26: AbstractAnnotationCheck<MatchPattern> {
27: private static final long serialVersionUID = 1L;
28:
29: private Pattern pattern;
30:
31: @Override
32: public void configure(final MatchPattern constraintAnnotation) {
33: super .configure(constraintAnnotation);
34: setPattern(constraintAnnotation.pattern(), constraintAnnotation
35: .flags());
36: }
37:
38: @Override
39: public Map<String, String> getMessageVariables() {
40: final Map<String, String> messageVariables = CollectionFactoryHolder
41: .getFactory().createMap(2);
42: messageVariables.put("pattern", pattern.pattern());
43: return messageVariables;
44: }
45:
46: /**
47: * @return the pattern
48: */
49: public Pattern getPattern() {
50: return pattern;
51: }
52:
53: public boolean isSatisfied(final Object validatedObject,
54: final Object value, final OValContext context,
55: final Validator validator) {
56: if (value == null)
57: return true;
58:
59: return pattern.matcher(value.toString()).matches();
60: }
61:
62: /**
63: * @param pattern the pattern to set
64: */
65: public void setPattern(final Pattern pattern) {
66: this .pattern = pattern;
67: }
68:
69: /**
70: * @param pattern the pattern to set
71: */
72: public void setPattern(final String pattern, final int flags) {
73: this.pattern = Pattern.compile(pattern, flags);
74: }
75: }
|