1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.validation.impl;
32
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 import nl.openedge.baritus.FormBeanContext;
37 import nl.openedge.baritus.util.ValueUtils;
38 import nl.openedge.baritus.validation.AbstractFieldValidator;
39 import nl.openedge.baritus.validation.ValidationActivationRule;
40
41 import org.infohazard.maverick.flow.ControllerContext;
42
43 /***
44 * Tests for a match against a regex pattern.
45 * if property 'mode' is MODE_VALID_IF_MATCHES (which is the default), isValid returns
46 * true if the input matches the pattern. If property mode is MODE_INVALID_IF_MATCHES
47 * (i.e. else), isValid returns false if the input matches the pattern.
48 *
49 * @author Eelco Hillenius
50 */
51 public class RegexValidator extends AbstractFieldValidator
52 {
53 /*** when in this mode, isValid will return true if the input matches the pattern */
54 public final static int MODE_VALID_IF_MATCHES = 0;
55
56 /*** when in this mode, isValid will return false if the input matches the pattern */
57 public final static int MODE_INVALID_IF_MATCHES = 1;
58
59
60 private int mode = MODE_VALID_IF_MATCHES;
61
62
63 private Pattern pattern = null;
64
65 private String errorMessageKey = "invalid.input";
66
67 /***
68 * construct without parameters
69 */
70 public RegexValidator()
71 {
72
73 }
74
75 /***
76 * construct with pattern
77 * @param pattern
78 */
79 public RegexValidator(Pattern pattern)
80 {
81 setPattern(pattern);
82 }
83
84 /***
85 * construct with errorMessageKey and pattern
86 * @param errorMessageKey
87 * @param pattern
88 */
89 public RegexValidator(String errorMessageKey, Pattern pattern)
90 {
91 setErrorMessageKey(errorMessageKey);
92 setPattern(pattern);
93 }
94
95 /***
96 * construct with errorMessageKey, rule and pattern
97 * @param errorMessageKey
98 * @param rule
99 * @param pattern
100 */
101 public RegexValidator(String errorMessageKey, ValidationActivationRule rule, Pattern pattern)
102 {
103 setErrorMessageKey(errorMessageKey);
104 setValidationRule(rule);
105 setPattern(pattern);
106 }
107
108 /***
109 * construct with pattern and mode
110 * @param pattern
111 * @param mode
112 */
113 public RegexValidator(Pattern pattern, int mode)
114 {
115 setPattern(pattern);
116 setMode(mode);
117 }
118
119 /***
120 * construct with message prefix, pattern and mode
121 * @param errorMessageKey
122 * @param pattern
123 * @param mode
124 */
125 public RegexValidator(String errorMessageKey, Pattern pattern, int mode)
126 {
127 setErrorMessageKey(errorMessageKey);
128 setPattern(pattern);
129 setMode(mode);
130 }
131
132 /***
133 * construct with errorMessageKey, rule, pattern and mode
134 * @param errorMessageKey
135 * @param rule
136 * @param pattern
137 * @param mode
138 */
139 public RegexValidator(
140 String errorMessageKey, ValidationActivationRule rule, Pattern pattern, int mode)
141 {
142 setErrorMessageKey(errorMessageKey);
143 setValidationRule(rule);
144 setPattern(pattern);
145 setMode(mode);
146 }
147
148 /***
149 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
150 */
151 public boolean isValid(
152 ControllerContext cctx,
153 FormBeanContext formBeanContext,
154 String fieldName,
155 Object value)
156 {
157 if(pattern == null) throw new RuntimeException("pattern is not provided!");
158
159 String toMatch = ValueUtils.convertToString(value);
160 Matcher matcher = pattern.matcher(toMatch);
161
162 boolean valid = (mode == MODE_VALID_IF_MATCHES) ? matcher.matches() : !matcher.matches();
163
164 if(!valid)
165 {
166 setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(),
167 new Object[]{getFieldName(formBeanContext, fieldName), value});
168 }
169
170 return valid;
171 }
172
173
174
175 /***
176 * get mode
177 * @return int
178 */
179 public int getMode()
180 {
181 return mode;
182 }
183
184 /***
185 * get pattern
186 * @return Pattern
187 */
188 public Pattern getPattern()
189 {
190 return pattern;
191 }
192
193 /***
194 * set mode
195 * @param i mode
196 */
197 public void setMode(int i)
198 {
199 mode = i;
200 }
201
202 /***
203 * set pattern
204 * @param pattern regex pattern
205 */
206 public void setPattern(Pattern pattern)
207 {
208 this.pattern = pattern;
209 }
210
211 /***
212 * Get key of error message.
213 * @return String key of error message
214 */
215 public String getErrorMessageKey()
216 {
217 return errorMessageKey;
218 }
219
220 /***
221 * Set key of error message.
222 * @param string key of error message
223 */
224 public void setErrorMessageKey(String string)
225 {
226 errorMessageKey = string;
227 }
228
229 }