1 7 package com.inversoft.util.typevalidator; 8 9 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 14 24 public class StringTypeValidator extends BaseTypeValidator { 25 26 29 public static final String DEFAULT_MESSAGE = "Invalid String"; 30 31 32 44 protected String internalValidate(Object value, Object params, 45 String message, Locale locale, Object [] mesgParams) { 46 if (value == null) { 47 return null; 48 } 49 50 String str = null; 51 boolean valid = true; 52 int length = 0; 53 try { 54 str = value.toString(); 55 length = (str == null) ? 0 : str.length(); 56 } catch (Exception e) { 57 valid = false; 58 } 59 60 if (valid && length > 0 && params != null) { 61 Object min = ((Map ) params).get("min"); 62 Object max = ((Map ) params).get("max"); 63 if (min != null) { 64 if (!(min instanceof Number )) { 65 try { 66 min = Double.valueOf(min.toString()); 67 } catch (NumberFormatException nfe) { 68 throw new NumberFormatException ("Invalid min value: " + 69 min); 70 } 71 } 72 73 if (min != null) { 74 if (length < (((Number ) min).doubleValue())) { 75 valid = false; 76 } 77 } 78 } 79 80 if (max != null) { 81 if (!(max instanceof Number )) { 82 try { 83 max = Double.valueOf(max.toString()); 84 } catch (NumberFormatException nfe) { 85 throw new NumberFormatException ("Invalid max value: " + 86 max); 87 } 88 } 89 90 if (max != null) { 91 if (length > (((Number ) max).doubleValue())) { 92 valid = false; 93 } 94 } 95 } 96 } 97 98 String error = null; 99 if (!valid) { 100 error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams); 101 } 102 103 return error; 104 } 105 } | Popular Tags |