1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.validator;
18
19 import org.apache.commons.validator.util.ValidatorUtils;
20
21 /**
22 * Contains validation methods for different unit tests.
23 */
24 public class GenericValidatorImpl {
25
26 public static final String FIELD_TEST_NULL = "NULL";
27
28 public static final String FIELD_TEST_NOTNULL = "NOTNULL";
29
30 public static final String FIELD_TEST_EQUAL = "EQUAL";
31
32 private static boolean isStringOrNull(final Object o) {
33 if (o == null) {
34 return true; // TODO this condition is not exercised by any tests currently
35 }
36 return o instanceof String;
37 }
38
39 /**
40 * Checks if the field can be successfully converted to a {@code byte}.
41 *
42 * @param bean The value validation is being performed on.
43 * @param field the field to use
44 * @return boolean If the field can be successfully converted to a {@code byte} {@code true} is returned. Otherwise {@code false}.
45 */
46 public static boolean validateByte(final Object bean, final Field field) {
47 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
48
49 return GenericValidator.isByte(value);
50 }
51
52 /**
53 * Checks if the field can be successfully converted to a {@code double}.
54 *
55 * @param bean The value validation is being performed on.
56 * @param field the field to use
57 * @return boolean If the field can be successfully converted to a {@code double} {@code true} is returned. Otherwise {@code false}.
58 */
59 public static boolean validateDouble(final Object bean, final Field field) {
60 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
61
62 return GenericValidator.isDouble(value);
63 }
64
65 /**
66 * Checks if the field is an e-mail address.
67 *
68 * @param bean The value validation is being performed on.
69 * @param field the field to use
70 * @return boolean If the field is an e-mail address {@code true} is returned. Otherwise {@code false}.
71 */
72 public static boolean validateEmail(final Object bean, final Field field) {
73 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
74
75 return GenericValidator.isEmail(value);
76 }
77
78 /**
79 * Checks if the field can be successfully converted to a {@code float}.
80 *
81 * @param bean The value validation is being performed on.
82 * @param field the field to use
83 * @return boolean If the field can be successfully converted to a {@code float} {@code true} is returned. Otherwise {@code false}.
84 */
85 public static boolean validateFloat(final Object bean, final Field field) {
86 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
87
88 return GenericValidator.isFloat(value);
89 }
90
91 /**
92 * Checks if the field can be successfully converted to a {@code int}.
93 *
94 * @param bean The value validation is being performed on.
95 * @param field the field to use
96 * @return boolean If the field can be successfully converted to a {@code int} {@code true} is returned. Otherwise {@code false}.
97 */
98 public static boolean validateInt(final Object bean, final Field field) {
99 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
100
101 return GenericValidator.isInt(value);
102 }
103
104 /**
105 * Checks if the field can be successfully converted to a {@code long}.
106 *
107 * @param bean The value validation is being performed on.
108 * @param field the field to use
109 * @return boolean If the field can be successfully converted to a {@code long} {@code true} is returned. Otherwise {@code false}.
110 */
111 public static boolean validateLong(final Object bean, final Field field) {
112 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
113
114 return GenericValidator.isLong(value);
115 }
116
117 /**
118 * Checks if field is positive assuming it is an integer
119 *
120 * @param bean The value validation is being performed on.
121 * @param field Description of the field to be evaluated
122 * @return boolean If the integer field is greater than zero, returns true, otherwise returns false.
123 */
124 public static boolean validatePositive(final Object bean, final Field field) {
125 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
126
127 return GenericTypeValidator.formatInt(value).intValue() > 0;
128 }
129
130 /**
131 * Throws a runtime exception if the value of the argument is "RUNTIME", an exception if the value of the argument is "CHECKED", and a ValidatorException
132 * otherwise.
133 *
134 * @throws RuntimeException with "RUNTIME-EXCEPTION as message" if value is "RUNTIME"
135 * @throws Exception with "CHECKED-EXCEPTION" as message if value is "CHECKED"
136 * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message otherwise
137 */
138 public static boolean validateRaiseException(final Object bean, final Field field) throws Exception {
139
140 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
141
142 if ("RUNTIME".equals(value)) {
143 throw new RuntimeException("RUNTIME-EXCEPTION");
144
145 }
146 if ("CHECKED".equals(value)) {
147 throw new Exception("CHECKED-EXCEPTION");
148
149 }
150 throw new ValidatorException("VALIDATOR-EXCEPTION");
151 }
152
153 /**
154 * Checks if the field is required.
155 *
156 * @return boolean If the field isn't {@code null} and has a length greater than zero, {@code true} is returned. Otherwise {@code false}.
157 */
158 public static boolean validateRequired(final Object bean, final Field field) {
159 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
160
161 return !GenericValidator.isBlankOrNull(value);
162 }
163
164 public static boolean validateRequiredIf(final Object bean, final Field field, final Validator validator) {
165
166 final Object form = validator.getParameterValue(Validator.BEAN_PARAM);
167 String value = null;
168 boolean required = false;
169 if (isStringOrNull(bean)) {
170 value = (String) bean;
171 } else {
172 value = ValidatorUtils.getValueAsString(bean, field.getProperty());
173 }
174 int i = 0;
175 String fieldJoin = "AND";
176 if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
177 fieldJoin = field.getVarValue("fieldJoin");
178 }
179 if (fieldJoin.equalsIgnoreCase("AND")) {
180 required = true;
181 }
182 while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
183 String dependProp = field.getVarValue("field[" + i + "]");
184 final String dependTest = field.getVarValue("fieldTest[" + i + "]");
185 final String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
186 String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");
187 if (dependIndexed == null) {
188 dependIndexed = "false";
189 }
190 boolean thisRequired = false;
191 if (field.isIndexed() && Boolean.parseBoolean(dependIndexed)) {
192 final String key = field.getKey();
193 if (key.contains("[") && key.contains("]")) {
194 final String ind = key.substring(0, key.indexOf(".") + 1);
195 dependProp = ind + dependProp;
196 }
197 }
198 final String dependVal = ValidatorUtils.getValueAsString(form, dependProp);
199 if (dependTest.equals(FIELD_TEST_NULL)) {
200 if (dependVal != null && !dependVal.isEmpty()) {
201 thisRequired = false;
202 } else {
203 thisRequired = true;
204 }
205 }
206 if (dependTest.equals(FIELD_TEST_NOTNULL)) {
207 if (dependVal != null && !dependVal.isEmpty()) {
208 thisRequired = true;
209 } else {
210 thisRequired = false;
211 }
212 }
213 if (dependTest.equals(FIELD_TEST_EQUAL)) {
214 thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
215 }
216 if (fieldJoin.equalsIgnoreCase("AND")) {
217 required = required && thisRequired;
218 } else {
219 required = required || thisRequired;
220 }
221 i++;
222 }
223 if (required) {
224 if (value != null && !value.isEmpty()) {
225 return true;
226 }
227 return false;
228 }
229 return true;
230 }
231
232 /**
233 * Checks if the field can be successfully converted to a {@code short}.
234 *
235 * @param bean The value validation is being performed on.
236 * @param field the field to use
237 * @return boolean If the field can be successfully converted to a {@code short} {@code true} is returned. Otherwise {@code false}.
238 */
239 public static boolean validateShort(final Object bean, final Field field) {
240 final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
241
242 return GenericValidator.isShort(value);
243 }
244
245 }