1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import org.apache.commons.validator.util.ValidatorUtils;
20
21
22
23
24
25
26 public class GenericValidatorImpl {
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public static boolean validateRaiseException(
41 final Object bean,
42 final Field field)
43 throws Exception {
44
45 final String value =
46 ValidatorUtils.getValueAsString(bean, field.getProperty());
47
48 if ("RUNTIME".equals(value)) {
49 throw new RuntimeException("RUNTIME-EXCEPTION");
50
51 } else if ("CHECKED".equals(value)) {
52 throw new Exception("CHECKED-EXCEPTION");
53
54 } else {
55 throw new ValidatorException("VALIDATOR-EXCEPTION");
56 }
57 }
58
59
60
61
62
63
64
65
66 public static boolean validateRequired(Object bean, Field field) {
67 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
68
69 return !GenericValidator.isBlankOrNull(value);
70 }
71
72
73
74
75
76
77
78
79
80
81 public static boolean validateByte(Object bean, Field field) {
82 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
83
84 return GenericValidator.isByte(value);
85 }
86
87
88
89
90
91
92
93
94
95
96 public static boolean validateShort(Object bean, Field field) {
97 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
98
99 return GenericValidator.isShort(value);
100 }
101
102
103
104
105
106
107
108
109
110
111 public static boolean validateInt(Object bean, Field field) {
112 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
113
114 return GenericValidator.isInt(value);
115 }
116
117
118
119
120
121
122
123
124
125 public static boolean validatePositive(Object bean , Field field) {
126 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
127
128 return GenericTypeValidator.formatInt(value).intValue() > 0;
129 }
130
131
132
133
134
135
136
137
138
139
140 public static boolean validateLong(Object bean, Field field) {
141 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
142
143 return GenericValidator.isLong(value);
144 }
145
146
147
148
149
150
151
152
153
154
155 public static boolean validateFloat(Object bean, Field field) {
156 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
157
158 return GenericValidator.isFloat(value);
159 }
160
161
162
163
164
165
166
167
168
169
170 public static boolean validateDouble(Object bean, Field field) {
171 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
172
173 return GenericValidator.isDouble(value);
174 }
175
176
177
178
179
180
181
182
183
184
185 public static boolean validateEmail(Object bean, Field field) {
186 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
187
188 return GenericValidator.isEmail(value);
189 }
190
191 public final static String FIELD_TEST_NULL = "NULL";
192 public final static String FIELD_TEST_NOTNULL = "NOTNULL";
193 public final static String FIELD_TEST_EQUAL = "EQUAL";
194
195 public static boolean validateRequiredIf(
196 Object bean,
197 Field field,
198 Validator validator) {
199
200 Object form = validator.getParameterValue(Validator.BEAN_PARAM);
201 String value = null;
202 boolean required = false;
203 if (isStringOrNull(bean)) {
204 value = (String) bean;
205 } else {
206 value = ValidatorUtils.getValueAsString(bean, field.getProperty());
207 }
208 int i = 0;
209 String fieldJoin = "AND";
210 if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
211 fieldJoin = field.getVarValue("fieldJoin");
212 }
213 if (fieldJoin.equalsIgnoreCase("AND")) {
214 required = true;
215 }
216 while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
217 String dependProp = field.getVarValue("field[" + i + "]");
218 String dependTest = field.getVarValue("fieldTest[" + i + "]");
219 String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
220 String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");
221 if (dependIndexed == null) {
222 dependIndexed = "false";
223 }
224 String dependVal = null;
225 boolean this_required = false;
226 if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
227 String key = field.getKey();
228 if ((key.contains("[")) && (key.contains("]"))) {
229 String ind = key.substring(0, key.indexOf(".") + 1);
230 dependProp = ind + dependProp;
231 }
232 }
233 dependVal = ValidatorUtils.getValueAsString(form, dependProp);
234 if (dependTest.equals(FIELD_TEST_NULL)) {
235 if ((dependVal != null) && (dependVal.length() > 0)) {
236 this_required = false;
237 } else {
238 this_required = true;
239 }
240 }
241 if (dependTest.equals(FIELD_TEST_NOTNULL)) {
242 if ((dependVal != null) && (dependVal.length() > 0)) {
243 this_required = true;
244 } else {
245 this_required = false;
246 }
247 }
248 if (dependTest.equals(FIELD_TEST_EQUAL)) {
249 this_required = dependTestValue.equalsIgnoreCase(dependVal);
250 }
251 if (fieldJoin.equalsIgnoreCase("AND")) {
252 required = required && this_required;
253 } else {
254 required = required || this_required;
255 }
256 i++;
257 }
258 if (required) {
259 if ((value != null) && (value.length() > 0)) {
260 return true;
261 } else {
262 return false;
263 }
264 }
265 return true;
266 }
267
268 private static boolean isStringOrNull(Object o) {
269 if (o == null) {
270 return true;
271 }
272 return (o instanceof String);
273 }
274
275 }