Coverage Report - org.apache.commons.validator.GenericTypeValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericTypeValidator
83%
114/137
52%
55/104
6
 
 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  
  *      http://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.NumberFormat;
 22  
 import java.text.ParseException;
 23  
 import java.text.ParsePosition;
 24  
 import java.text.SimpleDateFormat;
 25  
 import java.util.Date;
 26  
 import java.util.Locale;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 /**
 32  
  * This class contains basic methods for performing validations that return the
 33  
  * correctly typed class based on the validation performed.
 34  
  *
 35  
  * @version $Revision: 1716212 $
 36  
  */
 37  0
 public class GenericTypeValidator implements Serializable {
 38  
 
 39  
     private static final long serialVersionUID = 5487162314134261703L;
 40  
 
 41  1
     private static final Log LOG = LogFactory.getLog(GenericTypeValidator.class);
 42  
 
 43  
     /**
 44  
      * Checks if the value can safely be converted to a byte primitive.
 45  
      *
 46  
      * @param value The value validation is being performed on.
 47  
      * @return the converted Byte value.
 48  
      */
 49  
     public static Byte formatByte(String value) {
 50  9
         if (value == null) {
 51  2
             return null;
 52  
         }
 53  
 
 54  
         try {
 55  7
             return Byte.valueOf(value);
 56  2
         } catch (NumberFormatException e) {
 57  2
             return null;
 58  
         }
 59  
 
 60  
     }
 61  
 
 62  
     /**
 63  
      * Checks if the value can safely be converted to a byte primitive.
 64  
      *
 65  
      * @param value  The value validation is being performed on.
 66  
      * @param locale The locale to use to parse the number (system default if
 67  
      *               null)
 68  
      * @return the converted Byte value.
 69  
      */
 70  
     public static Byte formatByte(String value, Locale locale) {
 71  2
         Byte result = null;
 72  
 
 73  2
         if (value != null) {
 74  2
             NumberFormat formatter = null;
 75  2
             if (locale != null) {
 76  2
                 formatter = NumberFormat.getNumberInstance(locale);
 77  
             } else {
 78  0
                 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
 79  
             }
 80  2
             formatter.setParseIntegerOnly(true);
 81  2
             ParsePosition pos = new ParsePosition(0);
 82  2
             Number num = formatter.parse(value, pos);
 83  
 
 84  
             // If there was no error      and we used the whole string
 85  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 86  
                     num.doubleValue() >= Byte.MIN_VALUE &&
 87  
                     num.doubleValue() <= Byte.MAX_VALUE) {
 88  2
                 result = Byte.valueOf(num.byteValue());
 89  
             }
 90  
         }
 91  
 
 92  2
         return result;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Checks if the value can safely be converted to a short primitive.
 97  
      *
 98  
      * @param value The value validation is being performed on.
 99  
      * @return the converted Short value.
 100  
      */
 101  
     public static Short formatShort(String value) {
 102  7
         if (value == null) {
 103  1
             return null;
 104  
         }
 105  
 
 106  
         try {
 107  6
             return Short.valueOf(value);
 108  2
         } catch (NumberFormatException e) {
 109  2
             return null;
 110  
         }
 111  
 
 112  
     }
 113  
 
 114  
     /**
 115  
      * Checks if the value can safely be converted to a short primitive.
 116  
      *
 117  
      * @param value  The value validation is being performed on.
 118  
      * @param locale The locale to use to parse the number (system default if
 119  
      *               null)
 120  
      * @return the converted Short value.
 121  
      */
 122  
     public static Short formatShort(String value, Locale locale) {
 123  2
         Short result = null;
 124  
 
 125  2
         if (value != null) {
 126  2
             NumberFormat formatter = null;
 127  2
             if (locale != null) {
 128  2
                 formatter = NumberFormat.getNumberInstance(locale);
 129  
             } else {
 130  0
                 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
 131  
             }
 132  2
             formatter.setParseIntegerOnly(true);
 133  2
             ParsePosition pos = new ParsePosition(0);
 134  2
             Number num = formatter.parse(value, pos);
 135  
 
 136  
             // If there was no error      and we used the whole string
 137  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 138  
                     num.doubleValue() >= Short.MIN_VALUE &&
 139  
                     num.doubleValue() <= Short.MAX_VALUE) {
 140  2
                 result = Short.valueOf(num.shortValue());
 141  
             }
 142  
         }
 143  
 
 144  2
         return result;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Checks if the value can safely be converted to a int primitive.
 149  
      *
 150  
      * @param value The value validation is being performed on.
 151  
      * @return the converted Integer value.
 152  
      */
 153  
     public static Integer formatInt(String value) {
 154  24
         if (value == null) {
 155  2
             return null;
 156  
         }
 157  
 
 158  
         try {
 159  22
             return Integer.valueOf(value);
 160  8
         } catch (NumberFormatException e) {
 161  8
             return null;
 162  
         }
 163  
 
 164  
     }
 165  
 
 166  
     /**
 167  
      * Checks if the value can safely be converted to an int primitive.
 168  
      *
 169  
      * @param value  The value validation is being performed on.
 170  
      * @param locale The locale to use to parse the number (system default if
 171  
      *               null)
 172  
      * @return the converted Integer value.
 173  
      */
 174  
     public static Integer formatInt(String value, Locale locale) {
 175  2
         Integer result = null;
 176  
 
 177  2
         if (value != null) {
 178  2
             NumberFormat formatter = null;
 179  2
             if (locale != null) {
 180  2
                 formatter = NumberFormat.getNumberInstance(locale);
 181  
             } else {
 182  0
                 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
 183  
             }
 184  2
             formatter.setParseIntegerOnly(true);
 185  2
             ParsePosition pos = new ParsePosition(0);
 186  2
             Number num = formatter.parse(value, pos);
 187  
 
 188  
             // If there was no error      and we used the whole string
 189  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 190  
                     num.doubleValue() >= Integer.MIN_VALUE &&
 191  
                     num.doubleValue() <= Integer.MAX_VALUE) {
 192  2
                 result = Integer.valueOf(num.intValue());
 193  
             }
 194  
         }
 195  
 
 196  2
         return result;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Checks if the value can safely be converted to a long primitive.
 201  
      *
 202  
      * @param value The value validation is being performed on.
 203  
      * @return the converted Long value.
 204  
      */
 205  
     public static Long formatLong(String value) {
 206  9
         if (value == null) {
 207  2
             return null;
 208  
         }
 209  
 
 210  
         try {
 211  7
             return Long.valueOf(value);
 212  2
         } catch (NumberFormatException e) {
 213  2
             return null;
 214  
         }
 215  
 
 216  
     }
 217  
 
 218  
     /**
 219  
      * Checks if the value can safely be converted to a long primitive.
 220  
      *
 221  
      * @param value  The value validation is being performed on.
 222  
      * @param locale The locale to use to parse the number (system default if
 223  
      *               null)
 224  
      * @return the converted Long value.
 225  
      */
 226  
     public static Long formatLong(String value, Locale locale) {
 227  2
         Long result = null;
 228  
 
 229  2
         if (value != null) {
 230  2
             NumberFormat formatter = null;
 231  2
             if (locale != null) {
 232  2
                 formatter = NumberFormat.getNumberInstance(locale);
 233  
             } else {
 234  0
                 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
 235  
             }
 236  2
             formatter.setParseIntegerOnly(true);
 237  2
             ParsePosition pos = new ParsePosition(0);
 238  2
             Number num = formatter.parse(value, pos);
 239  
 
 240  
             // If there was no error      and we used the whole string
 241  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 242  
                     num.doubleValue() >= Long.MIN_VALUE &&
 243  
                     num.doubleValue() <= Long.MAX_VALUE) {
 244  2
                 result = Long.valueOf(num.longValue());
 245  
             }
 246  
         }
 247  
 
 248  2
         return result;
 249  
     }
 250  
 
 251  
     /**
 252  
      * Checks if the value can safely be converted to a float primitive.
 253  
      *
 254  
      * @param value The value validation is being performed on.
 255  
      * @return the converted Float value.
 256  
      */
 257  
     public static Float formatFloat(String value) {
 258  7
         if (value == null) {
 259  2
             return null;
 260  
         }
 261  
 
 262  
         try {
 263  5
             return new Float(value);
 264  0
         } catch (NumberFormatException e) {
 265  0
             return null;
 266  
         }
 267  
 
 268  
     }
 269  
 
 270  
     /**
 271  
      * Checks if the value can safely be converted to a float primitive.
 272  
      *
 273  
      * @param value  The value validation is being performed on.
 274  
      * @param locale The locale to use to parse the number (system default if
 275  
      *               null)
 276  
      * @return the converted Float value.
 277  
      */
 278  
     public static Float formatFloat(String value, Locale locale) {
 279  2
         Float result = null;
 280  
 
 281  2
         if (value != null) {
 282  2
             NumberFormat formatter = null;
 283  2
             if (locale != null) {
 284  2
                 formatter = NumberFormat.getInstance(locale);
 285  
             } else {
 286  0
                 formatter = NumberFormat.getInstance(Locale.getDefault());
 287  
             }
 288  2
             ParsePosition pos = new ParsePosition(0);
 289  2
             Number num = formatter.parse(value, pos);
 290  
 
 291  
             // If there was no error      and we used the whole string
 292  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 293  
                     num.doubleValue() >= (Float.MAX_VALUE * -1) &&
 294  
                     num.doubleValue() <= Float.MAX_VALUE) {
 295  2
                 result = new Float(num.floatValue());
 296  
             }
 297  
         }
 298  
 
 299  2
         return result;
 300  
     }
 301  
 
 302  
     /**
 303  
      * Checks if the value can safely be converted to a double primitive.
 304  
      *
 305  
      * @param value The value validation is being performed on.
 306  
      * @return the converted Double value.
 307  
      */
 308  
     public static Double formatDouble(String value) {
 309  7
         if (value == null) {
 310  2
             return null;
 311  
         }
 312  
 
 313  
         try {
 314  5
             return new Double(value);
 315  0
         } catch (NumberFormatException e) {
 316  0
             return null;
 317  
         }
 318  
 
 319  
     }
 320  
 
 321  
     /**
 322  
      * Checks if the value can safely be converted to a double primitive.
 323  
      *
 324  
      * @param value  The value validation is being performed on.
 325  
      * @param locale The locale to use to parse the number (system default if
 326  
      *               null)
 327  
      * @return the converted Double value.
 328  
      */
 329  
     public static Double formatDouble(String value, Locale locale) {
 330  2
         Double result = null;
 331  
 
 332  2
         if (value != null) {
 333  2
             NumberFormat formatter = null;
 334  2
             if (locale != null) {
 335  2
                 formatter = NumberFormat.getInstance(locale);
 336  
             } else {
 337  0
                 formatter = NumberFormat.getInstance(Locale.getDefault());
 338  
             }
 339  2
             ParsePosition pos = new ParsePosition(0);
 340  2
             Number num = formatter.parse(value, pos);
 341  
 
 342  
             // If there was no error      and we used the whole string
 343  2
             if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
 344  
                     num.doubleValue() >= (Double.MAX_VALUE * -1) &&
 345  
                     num.doubleValue() <= Double.MAX_VALUE) {
 346  2
                 result = new Double(num.doubleValue());
 347  
             }
 348  
         }
 349  
 
 350  2
         return result;
 351  
     }
 352  
 
 353  
     /**
 354  
      * Checks if the field is a valid date.
 355  
      *
 356  
      * <p>The {@code Locale} is used with {@code java.text.DateFormat}. The {@link java.text.DateFormat#setLenient(boolean)}
 357  
      * method is set to {@code false} for all.
 358  
      * </p>
 359  
      *
 360  
      * @param value  The value validation is being performed on.
 361  
      * @param locale The Locale to use to parse the date (system default if null)
 362  
      * @return the converted Date value.
 363  
      */
 364  
     public static Date formatDate(String value, Locale locale) {
 365  2
         Date date = null;
 366  
 
 367  2
         if (value == null) {
 368  0
             return null;
 369  
         }
 370  
 
 371  
         try {
 372  
             // Get the formatters to check against
 373  2
             DateFormat formatterShort = null;
 374  2
             DateFormat formatterDefault = null;
 375  2
             if (locale != null) {
 376  2
                 formatterShort =
 377  
                         DateFormat.getDateInstance(DateFormat.SHORT, locale);
 378  2
                 formatterDefault =
 379  
                         DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
 380  
             } else {
 381  0
                 formatterShort =
 382  
                         DateFormat.getDateInstance(
 383  
                                 DateFormat.SHORT,
 384  
                                 Locale.getDefault());
 385  0
                 formatterDefault =
 386  
                         DateFormat.getDateInstance(
 387  
                                 DateFormat.DEFAULT,
 388  
                                 Locale.getDefault());
 389  
             }
 390  
 
 391  
             // Turn off lenient parsing
 392  2
             formatterShort.setLenient(false);
 393  2
             formatterDefault.setLenient(false);
 394  
 
 395  
             // Firstly, try with the short form
 396  
             try {
 397  2
                 date = formatterShort.parse(value);
 398  0
             } catch (ParseException e) {
 399  
                 // Fall back on the default one
 400  0
                 date = formatterDefault.parse(value);
 401  2
             }
 402  0
         } catch (ParseException e) {
 403  
             // Bad date, so LOG and return null
 404  0
             if (LOG.isDebugEnabled()) {
 405  0
                 LOG.debug("Date parse failed value=[" + value + "], " +
 406  
                         "locale=[" + locale + "] " + e);
 407  
             }
 408  2
         }
 409  
 
 410  2
         return date;
 411  
     }
 412  
 
 413  
     /**
 414  
      * Checks if the field is a valid date.
 415  
      *
 416  
      * <p>The pattern is used with {@code java.text.SimpleDateFormat}.
 417  
      * If strict is true, then the length will be checked so '2/12/1999' will
 418  
      * not pass validation with the format 'MM/dd/yyyy' because the month isn't
 419  
      * two digits. The {@link java.text.SimpleDateFormat#setLenient(boolean)}
 420  
      * method is set to {@code false} for all.
 421  
      * </p>
 422  
      *
 423  
      * @param value       The value validation is being performed on.
 424  
      * @param datePattern The pattern passed to {@code SimpleDateFormat}.
 425  
      * @param strict      Whether or not to have an exact match of the
 426  
      *                    datePattern.
 427  
      * @return the converted Date value.
 428  
      */
 429  
     public static Date formatDate(String value, String datePattern, boolean strict) {
 430  2
         Date date = null;
 431  
 
 432  2
         if (value == null
 433  
                 || datePattern == null
 434  
                 || datePattern.length() == 0) {
 435  0
             return null;
 436  
         }
 437  
 
 438  
         try {
 439  2
             SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
 440  2
             formatter.setLenient(false);
 441  
 
 442  2
             date = formatter.parse(value);
 443  
 
 444  1
             if (strict && datePattern.length() != value.length()) {
 445  0
                 date = null;
 446  
             }
 447  1
         } catch (ParseException e) {
 448  
             // Bad date so return null
 449  1
             if (LOG.isDebugEnabled()) {
 450  0
                 LOG.debug("Date parse failed value=[" + value + "], " +
 451  
                         "pattern=[" + datePattern + "], " +
 452  
                         "strict=[" + strict + "] " + e);
 453  
             }
 454  1
         }
 455  
 
 456  2
         return date;
 457  
     }
 458  
 
 459  
     /**
 460  
      * Checks if the field is a valid credit card number.
 461  
      *
 462  
      * <p>Reference Sean M. Burke's <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">
 463  
      * script</a>.</p>
 464  
      *
 465  
      * @param value The value validation is being performed on.
 466  
      * @return the converted Credit Card number.
 467  
      */
 468  
     public static Long formatCreditCard(String value) {
 469  0
         return GenericValidator.isCreditCard(value) ? Long.valueOf(value) : null;
 470  
     }
 471  
 
 472  
 }
 473