1 package org.apache.turbine.services.intake.model; 2 3 18 19 import java.text.DateFormat ; 20 import java.text.ParseException ; 21 22 import java.util.Date ; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.turbine.services.intake.IntakeException; 27 import org.apache.turbine.services.intake.validator.DateStringValidator; 28 import org.apache.turbine.services.intake.xmlmodel.XmlField; 29 import org.apache.turbine.util.TurbineRuntimeException; 30 31 41 public class DateStringField 42 extends Field 43 { 44 45 private DateFormat df = null; 46 47 54 public DateStringField(XmlField field, Group group) 55 throws IntakeException 56 { 57 super(field, group); 58 59 if (validator == null || !(validator instanceof DateStringValidator)) 60 { 61 df = DateFormat.getInstance(); 62 df.setLenient(true); 63 } 64 } 65 66 71 public void setDefaultValue(String prop) 72 { 73 defaultValue = null; 74 75 if (prop == null) 76 { 77 return; 78 } 79 80 try 81 { 82 defaultValue = getDate(prop); 83 } 84 catch (ParseException e) 85 { 86 throw new TurbineRuntimeException("Could not parse " + prop 87 + " into a valid Date for the default value", e); 88 } 89 } 90 91 99 public void setEmptyValue(String prop) 100 { 101 emptyValue = null; 102 103 if (prop == null) 104 { 105 return; 106 } 107 108 try 109 { 110 emptyValue = getDate(prop); 111 } 112 catch (ParseException e) 113 { 114 throw new TurbineRuntimeException("Could not parse " + prop 115 + " into a valid Date for the empty value", e); 116 } 117 } 118 119 124 protected String getDefaultValidator() 125 { 126 return DateStringValidator.class.getName(); 127 } 128 129 132 protected void doSetValue() 133 { 134 if (isMultiValued) 135 { 136 String [] inputs = parser.getStrings(getKey()); 137 Date [] values = new Date [inputs.length]; 138 for (int i = 0; i < inputs.length; i++) 139 { 140 try 141 { 142 values[i] = StringUtils.isNotEmpty(inputs[i]) 143 ? getDate(inputs[i]) : (Date ) getEmptyValue(); 144 } 145 catch (ParseException e) 146 { 147 values[i] = null; 148 } 149 } 150 setTestValue(values); 151 } 152 else 153 { 154 String val = parser.getString(getKey()); 155 try 156 { 157 setTestValue(StringUtils.isNotEmpty(val) ? getDate(val) : (Date ) getEmptyValue()); 158 } 159 catch (ParseException e) 160 { 161 setTestValue(null); 162 } 163 } 164 } 165 166 175 private Date getDate(String dateString) 176 throws ParseException 177 { 178 Date date = null; 179 if (validator != null && validator instanceof DateStringValidator) 181 { 182 date = ((DateStringValidator) validator).parse(dateString); 183 } 184 else 185 { 186 date = df.parse(dateString); 187 } 188 return date; 189 } 190 191 196 public String toString() 197 { 198 String s = null; 199 Object value = getValue(); 200 if (value == null) 201 { 202 s = ""; 203 } 204 else if (value instanceof String ) 205 { 206 s = (String ) value; 207 } 208 else if (validator != null && validator instanceof DateStringValidator) 209 { 210 s = ((DateStringValidator) validator).format((Date ) value); 211 } 212 else 213 { 214 s = df.format((Date ) value); 215 } 216 return s; 217 } 218 } 219 | Popular Tags |