1 package org.apache.turbine.services.intake.validator; 2 3 18 19 import java.text.DateFormat ; 20 import java.text.ParseException ; 21 import java.text.SimpleDateFormat ; 22 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.lang.StringUtils; 29 30 import org.apache.turbine.services.intake.IntakeException; 31 32 54 public class DateStringValidator 55 extends DefaultValidator 56 { 57 private static final String DEFAULT_DATE_MESSAGE = 58 "Date could not be parsed"; 59 60 61 private List dateFormats = null; 62 63 64 private String dateFormatMessage = null; 65 66 67 private boolean flexible = false; 68 69 70 private DateFormat df = null; 71 72 73 private SimpleDateFormat sdf = null; 74 75 public DateStringValidator(Map paramMap) 76 throws IntakeException 77 { 78 init(paramMap); 79 } 80 81 84 public DateStringValidator() 85 { 86 dateFormats = new ArrayList (5); 87 } 88 89 95 public void init(Map paramMap) 96 throws InvalidMaskException 97 { 98 super.init(paramMap); 99 100 Constraint constraint = (Constraint) paramMap.get(FORMAT_RULE_NAME); 101 102 if (constraint != null) 103 { 104 dateFormats.add(constraint.getValue()); 105 setDateFormatMessage(constraint.getMessage()); 106 } 107 108 for(int i = 1 ;; i++) 109 { 110 constraint = (Constraint) paramMap.get(FORMAT_RULE_NAME + i); 111 112 if (constraint == null) 113 { 114 break; } 116 117 dateFormats.add(constraint.getValue()); 118 setDateFormatMessage(constraint.getMessage()); 119 } 120 121 if (StringUtils.isEmpty(dateFormatMessage)) 122 { 123 dateFormatMessage = DEFAULT_DATE_MESSAGE; 124 } 125 126 constraint = (Constraint) paramMap.get(FLEXIBLE_RULE_NAME); 127 128 if (constraint != null) 129 { 130 flexible = Boolean.valueOf(constraint.getValue()).booleanValue(); 131 } 132 133 if (dateFormats.size() == 0) 134 { 135 df = DateFormat.getInstance(); 136 df.setLenient(flexible); 137 } 138 else 139 { 140 sdf = new SimpleDateFormat (); 141 sdf.setLenient(flexible); 142 } 143 } 144 145 153 public void assertValidity(String testValue) 154 throws ValidationException 155 { 156 super.assertValidity(testValue); 157 158 if (required || StringUtils.isNotEmpty(testValue)) 159 { 160 try 161 { 162 parse(testValue); 163 } 164 catch (ParseException e) 165 { 166 errorMessage = dateFormatMessage; 167 throw new ValidationException(dateFormatMessage); 168 } 169 } 170 } 171 172 182 public Date parse(String s) 183 throws ParseException 184 { 185 Date date = null; 186 187 if (s == null) 188 { 189 throw new ParseException ("Input string was null", -1); 190 } 191 192 for (int i = 1; i < dateFormats.size() && date == null; i++) 193 { 194 sdf.applyPattern((String ) dateFormats.get(i)); 195 196 try 197 { 198 date = sdf.parse(s); 199 } 200 catch (ParseException e) 201 { 202 } 204 } 205 206 if (date == null) 207 { 208 sdf.applyPattern((String ) dateFormats.get(0)); 209 210 try 211 { 212 date = sdf.parse(s); 213 } 214 catch (ParseException e) 215 { 216 } 218 } 219 220 if (date == null && df != null) 221 { 222 date = df.parse(s); 223 } 224 225 if (date == null) 228 { 229 throw new ParseException ("Could not parse the date", 0); 230 } 231 232 return date; 233 } 234 235 242 public String format(Date date) 243 { 244 String s = null; 245 if (date != null) 246 { 247 sdf.applyPattern((String ) dateFormats.get(0)); 248 s = sdf.format(date); 249 } 250 return s; 251 } 252 253 254 258 263 public String getDateFormatMessage() 264 { 265 return dateFormatMessage; 266 } 267 268 275 public void setDateFormatMessage(String message) 276 { 277 if (StringUtils.isNotEmpty(message)) 278 { 279 dateFormatMessage = message; 280 } 281 } 282 283 288 public List getDateFormats() 289 { 290 return dateFormats; 291 } 292 293 298 public void setDateFormats(List formats) 299 { 300 this.dateFormats = formats; 301 } 302 303 308 public boolean isFlexible() 309 { 310 return flexible; 311 } 312 313 318 public void setFlexible(boolean flexible) 319 { 320 this.flexible = flexible; 321 } 322 } 323 | Popular Tags |