1 16 package org.apache.commons.betwixt.strategy; 17 18 import java.text.ParseException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.Locale ; 21 22 import org.apache.commons.beanutils.ConversionException; 23 import org.apache.commons.betwixt.expression.Context; 24 25 42 public class DefaultObjectStringConverter extends ConvertUtilsObjectStringConverter { 43 44 45 private static final SimpleDateFormat formatter 46 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.UK); 47 48 64 public String objectToString(Object object, Class type, String flavour, Context context) { 65 if ( object != null ) { 66 if ( object instanceof java.util.Date && isUtilDate( type ) ) { 67 68 return formatter.format( (java.util.Date ) object ); 69 70 } else { 71 return super.objectToString( object, type, flavour, context ); 73 } 74 } 75 return ""; 76 } 77 78 88 public Object stringToObject(String value, Class type, String flavour, Context context) { 89 if ( isUtilDate( type ) ) { 90 try { 91 92 return formatter.parse( value ); 93 94 } catch ( ParseException ex ) { 95 handleException( ex ); 96 return value; 100 } 101 } else { 102 return super.stringToObject( value, type, flavour, context ); 104 } 105 } 106 107 114 protected void handleException(Exception e) { 115 throw new ConversionException( "String to object conversion failed: " + e.getMessage(), e ); 116 } 117 118 123 private boolean isUtilDate(Class type) { 124 return ( java.util.Date .class.isAssignableFrom(type) 125 && !java.sql.Date .class.isAssignableFrom(type) 126 && !java.sql.Time .class.isAssignableFrom(type) 127 && !java.sql.Timestamp .class.isAssignableFrom(type) ); 128 } 129 } 130 | Popular Tags |