1 package org.apache.turbine.services.intake.validator; 2 3 18 19 import java.text.ParseException ; 20 21 import java.util.Map ; 22 23 import org.apache.commons.lang.StringUtils; 24 25 53 public class BooleanValidator 54 extends DefaultValidator 55 { 56 57 private static String [] trueValues = {"TRUE","T","YES","Y","1"}; 58 59 60 private static String [] falseValues = {"FALSE","F","NO","N","0"}; 61 62 65 public BooleanValidator() 66 { 67 } 68 69 75 public BooleanValidator(Map paramMap) 76 throws InvalidMaskException 77 { 78 super(paramMap); 79 } 80 81 89 public void assertValidity(String testValue) 90 throws ValidationException 91 { 92 super.assertValidity(testValue); 93 94 if (required || StringUtils.isNotEmpty(testValue)) 95 { 96 try 97 { 98 parse(testValue); 99 } 100 catch (ParseException e) 101 { 102 throw new ValidationException(e.getMessage()); 103 } 104 } 105 } 106 107 113 public Boolean parse(String stringValue) 114 throws ParseException 115 { 116 Boolean result = null; 117 118 for (int cnt = 0; 119 cnt < Math.max(trueValues.length, falseValues.length); cnt++) 120 { 121 if ((cnt < trueValues.length) && 123 stringValue.equalsIgnoreCase(trueValues[cnt])) 124 { 125 result = Boolean.TRUE; 126 break; 127 } 128 129 if ((cnt < falseValues.length) && 130 stringValue.equalsIgnoreCase(falseValues[cnt])) 131 { 132 result = Boolean.FALSE; 133 break; 134 } 135 } 136 137 if (result == null) 138 { 139 throw new ParseException (stringValue + 140 " could not be converted to a Boolean", 0); 141 } 142 return result; 143 } 144 } 145 | Popular Tags |