View Javadoc
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 java.io.Serializable;
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Locale;
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.validator.routines.DateValidator;
26  import org.apache.commons.validator.routines.EmailValidator;
27  
28  /**
29   * This class contains basic methods for performing validations.
30   */
31  public class GenericValidator implements Serializable {
32  
33      private static final long serialVersionUID = -7212095066891517618L;
34  
35      /**
36       * Calculate an adjustment amount for line endings.
37       *
38       * See Bug 37962 for the rationale behind this.
39       *
40       * @param value The value validation is being performed on.
41       * @param lineEndLength The length to use for line endings.
42       * @return the adjustment amount.
43       */
44      private static int adjustForLineEnding(final String value, final int lineEndLength) {
45          int nCount = 0;
46          int rCount = 0;
47          for (int i = 0; i < value.length(); i++) {
48              if (value.charAt(i) == '\n') {
49                  nCount++;
50              }
51              if (value.charAt(i) == '\r') {
52                  rCount++;
53              }
54          }
55          final int rnCount = rCount + nCount;
56          return nCount * lineEndLength - rnCount;
57      }
58  
59      /**
60       * <p>Checks if the field isn't null and length of the field is greater
61       * than zero not including whitespace.</p>
62       *
63       * @param value The value validation is being performed on.
64       * @return true if blank or null.
65       */
66      public static boolean isBlankOrNull(final String value) {
67          // Don't trim is already empty.
68          return value == null || value.isEmpty() || value.trim().isEmpty();
69      }
70  
71      /**
72       * <p>Checks if the value can safely be converted to a byte primitive.</p>
73       *
74       * @param value The value validation is being performed on.
75       * @return true if the value can be converted to a Byte.
76       */
77      public static boolean isByte(final String value) {
78          return GenericTypeValidator.formatByte(value) != null;
79      }
80  
81      /**
82       * Checks if the field is a valid credit card number.
83       * @param value The value validation is being performed on.
84       * @return true if the value is valid Credit Card Number.
85       */
86      public static boolean isCreditCard(final String value) {
87          return Constants.CREDIT_CARD_VALIDATOR.isValid(value);
88      }
89  
90      /**
91       * <p>Checks if the field is a valid date.  The {@link Locale} is
92       * used with {@link DateFormat}.  The setLenient method
93       * is set to {@code false} for all.</p>
94       *
95       * @param value The value validation is being performed on.
96       * @param locale The locale to use for the date format, defaults to the
97       * system default if null.
98       * @return true if the value can be converted to a Date.
99       */
100     public static boolean isDate(final String value, final Locale locale) {
101         return DateValidator.getInstance().isValid(value, locale);
102     }
103 
104     /**
105      * <p>Checks if the field is a valid date.  The pattern is used with
106      * {@link SimpleDateFormat}.  If strict is true, then the
107      * length will be checked so '2/12/1999' will not pass validation with
108      * the format 'MM/dd/yyyy' because the month isn't two digits.
109      * The setLenient method is set to {@code false} for all.</p>
110      *
111      * @param value The value validation is being performed on.
112      * @param datePattern The pattern passed to {@link SimpleDateFormat}.
113      * @param strict Whether or not to have an exact match of the datePattern.
114      * @return true if the value can be converted to a Date.
115      */
116     public static boolean isDate(final String value, final String datePattern, final boolean strict) {
117         // TODO method isValid() not yet supported in routines version
118         return org.apache.commons.validator.DateValidator.getInstance().isValid(value, datePattern, strict);
119     }
120 
121     /**
122      * <p>Checks if the value can safely be converted to a double primitive.</p>
123      *
124      * @param value The value validation is being performed on.
125      * @return true if the value can be converted to a Double.
126      */
127     public static boolean isDouble(final String value) {
128         return GenericTypeValidator.formatDouble(value) != null;
129     }
130 
131     /**
132      * <p>Checks if a field has a valid e-mail address.</p>
133      *
134      * @param value The value validation is being performed on.
135      * @return true if the value is valid Email Address.
136      */
137     public static boolean isEmail(final String value) {
138         return EmailValidator.getInstance().isValid(value);
139     }
140 
141     /**
142      * <p>Checks if the value can safely be converted to a float primitive.</p>
143      *
144      * @param value The value validation is being performed on.
145      * @return true if the value can be converted to a Float.
146      */
147     public static boolean isFloat(final String value) {
148         return GenericTypeValidator.formatFloat(value) != null;
149     }
150 
151     /**
152     * <p>Checks if a value is within a range (min &amp; max specified
153     * in the vars attribute).</p>
154     *
155     * @param value The value validation is being performed on.
156     * @param min The minimum value of the range.
157     * @param max The maximum value of the range.
158      * @return true if the value is in the specified range.
159     */
160     public static boolean isInRange(final byte value, final byte min, final byte max) {
161         return value >= min && value <= max;
162     }
163 
164     /**
165      * <p>Checks if a value is within a range (min &amp; max specified
166      * in the vars attribute).</p>
167      *
168      * @param value The value validation is being performed on.
169      * @param min The minimum value of the range.
170      * @param max The maximum value of the range.
171      * @return true if the value is in the specified range.
172      */
173     public static boolean isInRange(final double value, final double min, final double max) {
174         return value >= min && value <= max;
175     }
176 
177     /**
178      * <p>Checks if a value is within a range (min &amp; max specified
179      * in the vars attribute).</p>
180      *
181      * @param value The value validation is being performed on.
182      * @param min The minimum value of the range.
183      * @param max The maximum value of the range.
184      * @return true if the value is in the specified range.
185      */
186     public static boolean isInRange(final float value, final float min, final float max) {
187         return value >= min && value <= max;
188     }
189 
190     /**
191      * <p>Checks if a value is within a range (min &amp; max specified
192      * in the vars attribute).</p>
193      *
194      * @param value The value validation is being performed on.
195      * @param min The minimum value of the range.
196      * @param max The maximum value of the range.
197      * @return true if the value is in the specified range.
198      */
199     public static boolean isInRange(final int value, final int min, final int max) {
200         return value >= min && value <= max;
201     }
202 
203     /**
204      * <p>Checks if a value is within a range (min &amp; max specified
205      * in the vars attribute).</p>
206      *
207      * @param value The value validation is being performed on.
208      * @param min The minimum value of the range.
209      * @param max The maximum value of the range.
210      * @return true if the value is in the specified range.
211      */
212     public static boolean isInRange(final long value, final long min, final long max) {
213         return value >= min && value <= max;
214     }
215 
216     /**
217      * <p>Checks if a value is within a range (min &amp; max specified
218      * in the vars attribute).</p>
219      *
220      * @param value The value validation is being performed on.
221      * @param min The minimum value of the range.
222      * @param max The maximum value of the range.
223      * @return true if the value is in the specified range.
224      */
225     public static boolean isInRange(final short value, final short min, final short max) {
226         return value >= min && value <= max;
227     }
228 
229     /**
230      * <p>Checks if the value can safely be converted to an int primitive.</p>
231      *
232      * @param value The value validation is being performed on.
233      * @return true if the value can be converted to an Integer.
234      */
235     public static boolean isInt(final String value) {
236         return GenericTypeValidator.formatInt(value) != null;
237     }
238 
239     /**
240      * <p>Checks if the value can safely be converted to a long primitive.</p>
241      *
242      * @param value The value validation is being performed on.
243      * @return true if the value can be converted to a Long.
244      */
245     public static boolean isLong(final String value) {
246         return GenericTypeValidator.formatLong(value) != null;
247     }
248 
249     /**
250      * <p>Checks if the value can safely be converted to a short primitive.</p>
251      *
252      * @param value The value validation is being performed on.
253      * @return true if the value can be converted to a Short.
254      */
255     public static boolean isShort(final String value) {
256         return GenericTypeValidator.formatShort(value) != null;
257     }
258 
259     /**
260      * <p>Checks if a field is a valid URL address.</p>
261      * If you need to modify what is considered valid then
262      * consider using the UrlValidator directly.
263      *
264      * @param value The value validation is being performed on.
265      * @return true if the value is valid Url.
266      */
267     public static boolean isUrl(final String value) {
268         return Constants.URL_VALIDATOR.isValid(value);
269     }
270 
271     /**
272      * <p>Checks if the value matches the regular expression.</p>
273      *
274      * @param value The value validation is being performed on.
275      * @param regexp The regular expression.
276      * @return true if matches the regular expression.
277      */
278     public static boolean matchRegexp(final String value, final String regexp) {
279         if (regexp == null || regexp.isEmpty()) {
280             return false;
281         }
282 
283         return Pattern.matches(regexp, value);
284     }
285 
286     /**
287      * <p>Checks if the value's length is less than or equal to the max.</p>
288      *
289      * @param value The value validation is being performed on.
290      * @param max The maximum length.
291      * @return true if the value's length is less than the specified maximum.
292      */
293     public static boolean maxLength(final String value, final int max) {
294         return value.length() <= max;
295     }
296 
297     /**
298      * <p>Checks if the value's adjusted length is less than or equal to the max.</p>
299      *
300      * @param value The value validation is being performed on.
301      * @param max The maximum length.
302      * @param lineEndLength The length to use for line endings.
303      * @return true if the value's length is less than the specified maximum.
304      */
305     public static boolean maxLength(final String value, final int max, final int lineEndLength) {
306         final int adjustAmount = adjustForLineEnding(value, lineEndLength);
307         return value.length() + adjustAmount <= max;
308     }
309 
310     /**
311      * <p>Checks if the value is less than or equal to the max.</p>
312      *
313      * @param value The value validation is being performed on.
314      * @param max The maximum numeric value.
315      * @return true if the value is &lt;= the specified maximum.
316      */
317     public static boolean maxValue(final double value, final double max) {
318         return value <= max;
319     }
320 
321     /**
322      * <p>Checks if the value is less than or equal to the max.</p>
323      *
324      * @param value The value validation is being performed on.
325      * @param max The maximum numeric value.
326      * @return true if the value is &lt;= the specified maximum.
327      */
328     public static boolean maxValue(final float value, final float max) {
329         return value <= max;
330     }
331 
332     /**
333      * <p>Checks if the value is less than or equal to the max.</p>
334      *
335      * @param value The value validation is being performed on.
336      * @param max The maximum numeric value.
337      * @return true if the value is &lt;= the specified maximum.
338      */
339     public static boolean maxValue(final int value, final int max) {
340         return value <= max;
341     }
342 
343     // See https://issues.apache.org/bugzilla/show_bug.cgi?id=29015 regarding the "value" methods.
344 
345     /**
346      * <p>Checks if the value is less than or equal to the max.</p>
347      *
348      * @param value The value validation is being performed on.
349      * @param max The maximum numeric value.
350      * @return true if the value is &lt;= the specified maximum.
351      */
352     public static boolean maxValue(final long value, final long max) {
353         return value <= max;
354     }
355 
356     /**
357      * <p>Checks if the value's length is greater than or equal to the min.</p>
358      *
359      * @param value The value validation is being performed on.
360      * @param min The minimum length.
361      * @return true if the value's length is more than the specified minimum.
362      */
363     public static boolean minLength(final String value, final int min) {
364         return value.length() >= min;
365     }
366 
367     /**
368      * <p>Checks if the value's adjusted length is greater than or equal to the min.</p>
369      *
370      * @param value The value validation is being performed on.
371      * @param min The minimum length.
372      * @param lineEndLength The length to use for line endings.
373      * @return true if the value's length is more than the specified minimum.
374      */
375     public static boolean minLength(final String value, final int min, final int lineEndLength) {
376         final int adjustAmount = adjustForLineEnding(value, lineEndLength);
377         return value.length() + adjustAmount >= min;
378     }
379 
380     /**
381      * <p>Checks if the value is greater than or equal to the min.</p>
382      *
383      * @param value The value validation is being performed on.
384      * @param min The minimum numeric value.
385      * @return true if the value is &gt;= the specified minimum.
386      */
387     public static boolean minValue(final double value, final double min) {
388         return value >= min;
389     }
390 
391     /**
392      * <p>Checks if the value is greater than or equal to the min.</p>
393      *
394      * @param value The value validation is being performed on.
395      * @param min The minimum numeric value.
396      * @return true if the value is &gt;= the specified minimum.
397      */
398     public static boolean minValue(final float value, final float min) {
399         return value >= min;
400     }
401 
402     /**
403      * <p>Checks if the value is greater than or equal to the min.</p>
404      *
405      * @param value The value validation is being performed on.
406      * @param min The minimum numeric value.
407      * @return true if the value is &gt;= the specified minimum.
408      */
409     public static boolean minValue(final int value, final int min) {
410         return value >= min;
411     }
412 
413     /**
414      * <p>Checks if the value is greater than or equal to the min.</p>
415      *
416      * @param value The value validation is being performed on.
417      * @param min The minimum numeric value.
418      * @return true if the value is &gt;= the specified minimum.
419      */
420     public static boolean minValue(final long value, final long min) {
421         return value >= min;
422     }
423 
424     /**
425      * Constructs a new instance.
426      *
427      * @deprecated Will be private in the next major version.
428      */
429     @Deprecated
430     public GenericValidator() {
431         // empty
432     }
433 
434 }