1 package org.apache.fulcrum.intake.validator; 2 3 56 57 import java.util.Date ; 58 import java.util.Map ; 59 import java.util.List ; 60 import java.util.ArrayList ; 61 import java.text.DateFormat ; 62 import java.text.SimpleDateFormat ; 63 import java.text.ParseException ; 64 import org.apache.fulcrum.ServiceException; 65 66 86 public class DateStringValidator 87 extends DefaultValidator 88 { 89 private static final String DEFAULT_DATE_MESSAGE = 90 "Date could not be parsed"; 91 92 private List dateFormats; 93 private String dateFormatMessage; 94 private boolean flexible; 95 private DateFormat df; 96 private SimpleDateFormat sdf; 97 98 99 public DateStringValidator(Map paramMap) 100 throws ServiceException 101 { 102 this(); 103 init(paramMap); 104 } 105 106 public DateStringValidator() 107 { 108 super(); 109 } 110 111 public void init(Map paramMap) 112 throws ServiceException 113 { 114 super.init(paramMap); 115 dateFormats = new ArrayList (5); 116 117 Constraint constraint = (Constraint)paramMap.get("format"); 118 if ( constraint != null ) 119 { 120 dateFormats.add(constraint.getValue()); 121 setDateFormatMessage(constraint.getMessage()); 122 } 123 124 int i = 1; 125 constraint = (Constraint)paramMap.get("format" + i); 126 while ( constraint != null ) 127 { 128 dateFormats.add(constraint.getValue()); 129 setDateFormatMessage(constraint.getMessage()); 130 constraint = (Constraint)paramMap.get("format" + (++i)); 131 } 132 133 if ( dateFormatMessage == null || dateFormatMessage.equals("") ) 134 { 135 dateFormatMessage = DEFAULT_DATE_MESSAGE; 136 } 137 138 constraint = (Constraint)paramMap.get("flexible"); 139 if ( constraint != null ) 140 { 141 flexible = Boolean.valueOf(constraint.getValue()).booleanValue(); 142 } 143 144 if ( dateFormats.size() == 0 || flexible ) 145 { 146 df = DateFormat.getInstance(); 147 df.setLenient(true); 148 } 149 if (dateFormats.size() != 0) 150 { 151 sdf = new SimpleDateFormat (); 152 } 153 154 } 155 156 157 165 protected void doAssertValidity(String testValue) 166 throws ValidationException 167 { 168 try 169 { 170 parse(testValue); 171 } 172 catch (ParseException e) 173 { 174 message = dateFormatMessage; 175 throw new ValidationException(dateFormatMessage); 176 } 177 } 178 179 183 public Date parse(String s) 184 throws ParseException 185 { 186 Date date = null; 187 188 if ( s == null ) 189 { 190 throw new ParseException ("Input string was null", -1); 191 } 192 193 for ( int i=0; i<dateFormats.size(); i++) 194 { 195 sdf.applyPattern((String )dateFormats.get(i)); 196 try 197 { 198 date = sdf.parse(s); 199 } 200 catch (ParseException e) 201 { 202 } 204 if ( date != null ) 205 { 206 break; 207 } 208 } 209 210 if ( date == null && df != null ) 211 { 212 date = df.parse(s); 213 } 214 215 return date; 216 } 217 218 public String format(Date date) 219 { 220 String s = null; 221 if (date != null) 222 { 223 sdf.applyPattern((String )dateFormats.get(0)); 224 s= sdf.format(date); 225 } 226 return s; 227 } 228 229 230 234 238 public String getDateFormatMessage() 239 { 240 return dateFormatMessage; 241 } 242 243 250 public void setDateFormatMessage(String v) 251 { 252 if ( v != null && !v.equals("") ) 253 { 254 dateFormatMessage = v; 255 } 256 } 257 258 262 public List getDateFormats() 263 { 264 return dateFormats; 265 } 266 267 271 public void setDateFormats(List v) 272 { 273 this.dateFormats = v; 274 } 275 276 280 public boolean isFlexible() 281 { 282 return flexible; 283 } 284 285 289 public void setFlexible(boolean v) 290 { 291 this.flexible = v; 292 } 293 294 } 295 | Popular Tags |