| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package javax.faces.convert; |
| 17 | |
|
| 18 | |
import java.text.DateFormat; |
| 19 | |
import java.text.ParseException; |
| 20 | |
import java.text.SimpleDateFormat; |
| 21 | |
import java.util.Locale; |
| 22 | |
import java.util.TimeZone; |
| 23 | |
|
| 24 | |
import javax.faces.component.StateHolder; |
| 25 | |
import javax.faces.component.UIComponent; |
| 26 | |
import javax.faces.context.FacesContext; |
| 27 | |
import javax.faces.internal.ConvertUtil; |
| 28 | |
import javax.faces.internal.FacesMessageUtil; |
| 29 | |
|
| 30 | |
import org.seasar.framework.util.AssertionUtil; |
| 31 | |
import org.seasar.framework.util.DateConversionUtil; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public class DateTimeConverter implements Converter, StateHolder { |
| 38 | |
|
| 39 | |
public static final String CONVERTER_ID = "javax.faces.DateTime"; |
| 40 | |
|
| 41 | 2 | public static final String CONVERSION_OBJECT_ID = DateTimeConverter.class |
| 42 | |
.getName() + |
| 43 | |
".CONVERSION"; |
| 44 | |
|
| 45 | 1 | public static final String CONVERSION_STRING_ID = DateTimeConverter.class |
| 46 | |
.getName() + |
| 47 | |
".CONVERSION_STRING"; |
| 48 | |
|
| 49 | |
protected static final String STYLE_DEFAULT = "default"; |
| 50 | |
|
| 51 | |
protected static final String STYLE_MEDIUM = "medium"; |
| 52 | |
|
| 53 | |
protected static final String STYLE_SHORT = "short"; |
| 54 | |
|
| 55 | |
protected static final String STYLE_LONG = "long"; |
| 56 | |
|
| 57 | |
protected static final String STYLE_FULL = "full"; |
| 58 | |
|
| 59 | |
protected static final String TYPE_DATE = "date"; |
| 60 | |
|
| 61 | |
protected static final String TYPE_TIME = "time"; |
| 62 | |
|
| 63 | |
protected static final String TYPE_BOTH = "both"; |
| 64 | |
|
| 65 | 81 | private String dateStyle = STYLE_DEFAULT; |
| 66 | |
|
| 67 | 81 | private boolean transientValue = false; |
| 68 | |
|
| 69 | 81 | private Locale locale = null; |
| 70 | |
|
| 71 | 81 | private String pattern = null; |
| 72 | |
|
| 73 | 81 | private String timeStyle = STYLE_DEFAULT; |
| 74 | |
|
| 75 | 1 | private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault(); |
| 76 | |
|
| 77 | 81 | private TimeZone timeZone = DEFAULT_TIME_ZONE; |
| 78 | |
|
| 79 | 81 | private String type = TYPE_DATE; |
| 80 | |
|
| 81 | 81 | public DateTimeConverter() { |
| 82 | 81 | } |
| 83 | |
|
| 84 | |
public Object getAsObject(FacesContext context, UIComponent component, |
| 85 | |
String value) throws ConverterException { |
| 86 | 13 | AssertionUtil.assertNotNull("FacesContext", context); |
| 87 | 12 | AssertionUtil.assertNotNull("UIComponent", component); |
| 88 | 11 | if (value == null) { |
| 89 | 2 | return null; |
| 90 | |
} |
| 91 | 9 | value = value.trim(); |
| 92 | 9 | if (value.length() < 1) { |
| 93 | 1 | return null; |
| 94 | |
} |
| 95 | 8 | Locale locale = getLocale(); |
| 96 | 8 | DateFormat format = getDateFormat(value, locale); |
| 97 | 8 | if (format == null) { |
| 98 | 0 | format = DateConversionUtil.getDateFormat(value, locale); |
| 99 | |
} |
| 100 | 8 | format.setLenient(false); |
| 101 | 8 | TimeZone timeZone = getTimeZone(); |
| 102 | 8 | if (timeZone != null) { |
| 103 | 8 | format.setTimeZone(timeZone); |
| 104 | |
} |
| 105 | |
try { |
| 106 | 8 | return format.parse(value); |
| 107 | 1 | } catch (ParseException e) { |
| 108 | 1 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
| 109 | |
value); |
| 110 | 1 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
| 111 | |
getObjectMessageId(), args), e); |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|
| 115 | |
public String getAsString(FacesContext context, UIComponent component, |
| 116 | |
Object value) throws ConverterException { |
| 117 | 8 | AssertionUtil.assertNotNull("FacesContext", context); |
| 118 | 7 | AssertionUtil.assertNotNull("UIComponent", component); |
| 119 | 6 | if (value == null) { |
| 120 | 2 | return ""; |
| 121 | |
} |
| 122 | 4 | if (value instanceof String) { |
| 123 | 2 | return (String) value; |
| 124 | |
} |
| 125 | 2 | DateFormat formatter = getDateFormat(getLocale()); |
| 126 | 2 | TimeZone timeZone = getTimeZone(); |
| 127 | 2 | if (timeZone != null) { |
| 128 | 2 | formatter.setTimeZone(timeZone); |
| 129 | |
} |
| 130 | |
try { |
| 131 | 2 | return formatter.format(value); |
| 132 | 0 | } catch (Exception e) { |
| 133 | 0 | Object[] args = ConvertUtil.createExceptionMessageArgs(component, |
| 134 | |
value); |
| 135 | 0 | throw new ConverterException(FacesMessageUtil.getMessage(context, |
| 136 | |
getStringMessageId(), args), e); |
| 137 | |
} |
| 138 | |
} |
| 139 | |
|
| 140 | |
public boolean isTransient() { |
| 141 | 23 | return transientValue; |
| 142 | |
} |
| 143 | |
|
| 144 | |
public void setTransient(boolean transientValue) { |
| 145 | 2 | this.transientValue = transientValue; |
| 146 | 2 | } |
| 147 | |
|
| 148 | |
public Object saveState(FacesContext context) { |
| 149 | 22 | Object[] values = new Object[6]; |
| 150 | 22 | values[0] = dateStyle; |
| 151 | 22 | values[1] = locale; |
| 152 | 22 | values[2] = pattern; |
| 153 | 22 | values[3] = timeStyle; |
| 154 | 22 | values[4] = timeZone; |
| 155 | 22 | values[5] = type; |
| 156 | 22 | return values; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public void restoreState(FacesContext context, Object state) { |
| 160 | 22 | Object[] values = (Object[]) state; |
| 161 | 22 | dateStyle = (String) values[0]; |
| 162 | 22 | locale = (Locale) values[1]; |
| 163 | 22 | pattern = (String) values[2]; |
| 164 | 22 | timeStyle = (String) values[3]; |
| 165 | 22 | timeZone = (TimeZone) values[4]; |
| 166 | 22 | type = (String) values[5]; |
| 167 | 22 | } |
| 168 | |
|
| 169 | |
public String getDateStyle() { |
| 170 | 10 | return dateStyle; |
| 171 | |
} |
| 172 | |
|
| 173 | |
public void setDateStyle(String dateStyle) { |
| 174 | 8 | this.dateStyle = dateStyle; |
| 175 | 8 | } |
| 176 | |
|
| 177 | |
public Locale getLocale() { |
| 178 | 37 | if (locale == null) { |
| 179 | 2 | locale = getLocale(FacesContext.getCurrentInstance()); |
| 180 | |
} |
| 181 | 37 | return locale; |
| 182 | |
} |
| 183 | |
|
| 184 | |
public void setLocale(Locale locale) { |
| 185 | 36 | this.locale = locale; |
| 186 | 36 | } |
| 187 | |
|
| 188 | |
public void setLocaleAsString(final String locale) { |
| 189 | 1 | final String[] array = locale.split("_"); |
| 190 | 1 | if (array.length == 1) { |
| 191 | 0 | setLocale(new Locale(locale)); |
| 192 | 1 | } else if (array.length == 2) { |
| 193 | 1 | setLocale(new Locale(array[0], array[1])); |
| 194 | 0 | } else if (array.length == 3) { |
| 195 | 0 | setLocale(new Locale(array[0], array[1], array[2])); |
| 196 | |
} else { |
| 197 | 0 | throw new IllegalArgumentException(locale); |
| 198 | |
} |
| 199 | 1 | } |
| 200 | |
|
| 201 | |
public String getPattern() { |
| 202 | 11 | return pattern; |
| 203 | |
} |
| 204 | |
|
| 205 | |
public void setPattern(String pattern) { |
| 206 | 13 | this.pattern = pattern; |
| 207 | 13 | } |
| 208 | |
|
| 209 | |
public String getTimeStyle() { |
| 210 | 4 | return timeStyle; |
| 211 | |
} |
| 212 | |
|
| 213 | |
public void setTimeStyle(String timeStyle) { |
| 214 | 4 | this.timeStyle = timeStyle; |
| 215 | 4 | } |
| 216 | |
|
| 217 | |
public TimeZone getTimeZone() { |
| 218 | 14 | return timeZone; |
| 219 | |
} |
| 220 | |
|
| 221 | |
public void setTimeZone(TimeZone timeZone) { |
| 222 | 15 | this.timeZone = timeZone; |
| 223 | 15 | } |
| 224 | |
|
| 225 | |
public String getType() { |
| 226 | 5 | return type; |
| 227 | |
} |
| 228 | |
|
| 229 | |
public void setType(String type) { |
| 230 | 4 | this.type = type; |
| 231 | 4 | } |
| 232 | |
|
| 233 | |
private Locale getLocale(FacesContext context) { |
| 234 | 2 | return context.getViewRoot().getLocale(); |
| 235 | |
} |
| 236 | |
|
| 237 | |
protected DateFormat getDateFormat(Locale locale) { |
| 238 | 2 | String pattern = getPattern(); |
| 239 | 2 | if (pattern != null) { |
| 240 | 1 | return new SimpleDateFormat(pattern, locale); |
| 241 | |
} |
| 242 | 1 | if (isDefaultStyle()) { |
| 243 | 1 | return DateConversionUtil.getY4DateFormat(locale); |
| 244 | |
} |
| 245 | 0 | return getDateFormatForType(); |
| 246 | |
} |
| 247 | |
|
| 248 | |
protected DateFormat getDateFormat(String value, Locale locale) { |
| 249 | 8 | String pattern = getPattern(); |
| 250 | 8 | if (pattern != null) { |
| 251 | 6 | return new SimpleDateFormat(pattern, locale); |
| 252 | |
} |
| 253 | 2 | if (isDefaultStyle()) { |
| 254 | 1 | return DateConversionUtil.getDateFormat(value, locale); |
| 255 | |
} |
| 256 | 1 | return getDateFormatForType(); |
| 257 | |
} |
| 258 | |
|
| 259 | |
protected boolean isDefaultStyle() { |
| 260 | 3 | return TYPE_DATE.equalsIgnoreCase(getType()) && |
| 261 | |
STYLE_DEFAULT.equalsIgnoreCase(getDateStyle()) && |
| 262 | |
STYLE_DEFAULT.equalsIgnoreCase(getTimeStyle()); |
| 263 | |
} |
| 264 | |
|
| 265 | |
protected DateFormat getDateFormatForType() { |
| 266 | 1 | String type = getType(); |
| 267 | 1 | if (type.equals(TYPE_DATE)) { |
| 268 | 1 | return DateFormat.getDateInstance(calcStyle(getDateStyle()), |
| 269 | |
getLocale()); |
| 270 | 0 | } else if (type.equals(TYPE_TIME)) { |
| 271 | 0 | return DateFormat.getTimeInstance(calcStyle(getTimeStyle()), |
| 272 | |
getLocale()); |
| 273 | 0 | } else if (type.equals(TYPE_BOTH)) { |
| 274 | 0 | return DateFormat.getDateTimeInstance(calcStyle(getDateStyle()), |
| 275 | |
calcStyle(getTimeStyle()), getLocale()); |
| 276 | |
} else { |
| 277 | 0 | return null; |
| 278 | |
} |
| 279 | |
} |
| 280 | |
|
| 281 | |
protected int calcStyle(String name) { |
| 282 | 1 | if (name.equals(STYLE_DEFAULT)) { |
| 283 | 0 | return DateFormat.DEFAULT; |
| 284 | |
} |
| 285 | 1 | if (name.equals(STYLE_MEDIUM)) { |
| 286 | 1 | return DateFormat.MEDIUM; |
| 287 | |
} |
| 288 | 0 | if (name.equals(STYLE_SHORT)) { |
| 289 | 0 | return DateFormat.SHORT; |
| 290 | |
} |
| 291 | 0 | if (name.equals(STYLE_LONG)) { |
| 292 | 0 | return DateFormat.LONG; |
| 293 | |
} |
| 294 | 0 | if (name.equals(STYLE_FULL)) { |
| 295 | 0 | return DateFormat.FULL; |
| 296 | |
} |
| 297 | 0 | return DateFormat.DEFAULT; |
| 298 | |
} |
| 299 | |
|
| 300 | |
protected String getObjectMessageId() { |
| 301 | 1 | return CONVERSION_OBJECT_ID; |
| 302 | |
} |
| 303 | |
|
| 304 | |
protected String getStringMessageId() { |
| 305 | 0 | return CONVERSION_STRING_ID; |
| 306 | |
} |
| 307 | |
|
| 308 | |
} |