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:
16: import net.sf.oval.Validator;
17: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
18: import net.sf.oval.context.OValContext;
19: import net.sf.oval.internal.CollectionFactoryHolder;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class HasSubstringCheck extends
25: AbstractAnnotationCheck<HasSubstring> {
26: private static final long serialVersionUID = 1L;
27:
28: private boolean ignoreCase;
29:
30: private String substring;
31: private transient String substringLowerCase;
32:
33: @Override
34: public void configure(final HasSubstring constraintAnnotation) {
35: super .configure(constraintAnnotation);
36: setIgnoreCase(constraintAnnotation.ignoreCase());
37: setSubstring(constraintAnnotation.substring());
38: }
39:
40: @Override
41: public Map<String, String> getMessageVariables() {
42: final Map<String, String> messageVariables = CollectionFactoryHolder
43: .getFactory().createMap(2);
44: messageVariables
45: .put("ignoreCase", Boolean.toString(ignoreCase));
46: messageVariables.put("substring", substring);
47: return messageVariables;
48: }
49:
50: /**
51: * @return the substring
52: */
53: public String getSubstring() {
54: return substring;
55: }
56:
57: private String getSubstringLowerCase() {
58: if (substringLowerCase == null && substring != null)
59: substringLowerCase = substring.toLowerCase();
60: return substringLowerCase;
61: }
62:
63: /**
64: * @return the ignoreCase
65: */
66: public boolean isIgnoreCase() {
67: return ignoreCase;
68: }
69:
70: public boolean isSatisfied(final Object validatedObject,
71: final Object value, final OValContext context,
72: final Validator validator) {
73: if (value == null)
74: return true;
75:
76: if (ignoreCase)
77: return value.toString().toLowerCase().indexOf(
78: getSubstringLowerCase()) > -1;
79:
80: return value.toString().indexOf(substring) > -1;
81: }
82:
83: /**
84: * @param ignoreCase the ignoreCase to set
85: */
86: public void setIgnoreCase(final boolean ignoreCase) {
87: this .ignoreCase = ignoreCase;
88: }
89:
90: /**
91: * @param substring the substring to set
92: */
93: public void setSubstring(final String substring) {
94: this.substring = substring;
95: }
96: }
|