1 16 17 package org.apache.commons.beanutils.locale; 18 19 import org.apache.commons.beanutils.ConversionException; 20 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.text.ParseException ; 25 import java.util.Locale ; 26 27 28 37 38 public abstract class BaseLocaleConverter implements LocaleConverter { 39 40 42 43 private static Log log = LogFactory.getLog(BaseLocaleConverter.class); 44 45 46 private Object defaultValue = null; 47 48 49 protected boolean useDefault = false; 50 51 52 protected Locale locale = Locale.getDefault(); 53 54 55 protected String pattern = null; 56 57 58 protected boolean locPattern = false; 59 60 62 70 protected BaseLocaleConverter(Locale locale, String pattern) { 71 72 this(null, locale, pattern, false, false); 73 } 74 75 83 protected BaseLocaleConverter(Locale locale, String pattern, boolean locPattern) { 84 85 this(null, locale, pattern, false, locPattern); 86 } 87 88 97 protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern) { 98 99 this(defaultValue, locale, pattern, false); 100 } 101 102 111 protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { 112 113 this(defaultValue, locale, pattern, true, locPattern); 114 } 115 116 126 private BaseLocaleConverter(Object defaultValue, Locale locale, 127 String pattern, boolean useDefault, boolean locPattern) { 128 129 if (useDefault) { 130 this.defaultValue = defaultValue; 131 this.useDefault = true; 132 } 133 134 if (locale != null) { 135 this.locale = locale; 136 } 137 138 this.pattern = pattern; 139 this.locPattern = locPattern; 140 } 141 142 144 154 155 abstract protected Object parse(Object value, String pattern) throws ParseException ; 156 157 158 167 public Object convert(Object value) { 168 return convert(value, null); 169 } 170 171 180 public Object convert(Object value, String pattern) { 181 return convert(null, value, pattern); 182 } 183 184 194 public Object convert(Class type, Object value) { 195 return convert(type, value, null); 196 } 197 198 209 public Object convert(Class type, Object value, String pattern) { 210 if (value == null) { 211 if (useDefault) { 212 return (defaultValue); 213 } else { 214 log.debug("Null value specified for conversion, returing null"); 217 return null; 218 } 219 } 220 221 try { 222 if (pattern != null) { 223 return parse(value, pattern); 224 } else { 225 return parse(value, this.pattern); 226 } 227 } catch (Exception e) { 228 if (useDefault) { 229 return (defaultValue); 230 } else { 231 throw new ConversionException(e); 232 } 233 } 234 } 235 } 236 | Popular Tags |