1 package org.apache.fulcrum.intake.model; 2 3 56 57 import java.text.DateFormat ; 58 import java.text.ParseException ; 59 import java.util.Date ; 60 61 import org.apache.fulcrum.intake.validator.DateStringValidator; 62 import org.apache.fulcrum.intake.xmlmodel.XmlField; 63 64 73 public class DateStringField 74 extends Field 75 { 76 private DateFormat df = null; 77 78 public DateStringField(XmlField field, Group group) 79 throws Exception 80 { 81 super(field, group); 82 83 if ( validator == null || !(validator instanceof DateStringValidator)) 84 { 85 df = DateFormat.getInstance(); 86 df.setLenient(true); 87 } 88 } 89 90 95 protected void setDefaultValue(String prop) 96 { 97 defaultValue = prop; 98 } 99 100 105 protected String getDefaultValidator() 106 { 107 return "org.apache.fulcrum.intake.validator.DateStringValidator"; 108 } 109 110 113 protected void doSetValue() 114 { 115 if ( isMultiValued ) 116 { 117 String [] ss = pp.getStrings(getKey()); 118 Date [] dates = new Date [ss.length]; 119 for (int i=0; i<ss.length; i++) 120 { 121 dates[i] = getDate(ss[i]); 122 } 123 setTestValue(dates); 124 } 125 else 126 { 127 setTestValue( getDate(pp.getString(getKey())) ); 128 } 129 } 130 131 private Date getDate(String dateString) 132 { 133 Date date = null; 134 try 135 { 136 if (validator != null && validator instanceof DateStringValidator) 138 { 139 date = ((DateStringValidator)validator).parse(dateString); 140 } 141 else 142 { 143 date = df.parse(dateString); 144 } 145 } 146 catch (ParseException e) 147 { 148 } 150 return date; 151 } 152 153 public String toString() 154 { 155 String s = null; 156 Object value = getValue(); 157 if (value == null) 158 { 159 s = ""; 160 } 161 else if (value instanceof String ) 162 { 163 s = (String )value; 164 } 165 else if (validator != null && validator instanceof DateStringValidator) 166 { 167 s= ((DateStringValidator)validator).format((Date )value); 168 } 169 else 170 { 171 s = df.format((Date )value); 172 } 173 return s; 174 } 175 } 176 | Popular Tags |