01: package de.schlund.pfixcore.oxm.impl.serializers;
02:
03: import java.lang.annotation.Annotation;
04: import java.text.SimpleDateFormat;
05: import java.util.Calendar;
06: import java.util.Date;
07:
08: import de.schlund.pfixcore.oxm.impl.AnnotationAware;
09: import de.schlund.pfixcore.oxm.impl.SerializationContext;
10: import de.schlund.pfixcore.oxm.impl.SerializationException;
11: import de.schlund.pfixcore.oxm.impl.SimpleTypeSerializer;
12:
13: public class DateSerializer implements SimpleTypeSerializer,
14: AnnotationAware {
15:
16: private final static String DEFAULT_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
17: private String pattern;
18:
19: public DateSerializer() {
20: pattern = DEFAULT_PATTERN;
21: }
22:
23: public DateSerializer(String pattern) {
24: this .pattern = pattern;
25: }
26:
27: public void setAnnotation(Annotation annotation) {
28: if (annotation instanceof de.schlund.pfixcore.oxm.impl.annotation.DateSerializer) {
29: de.schlund.pfixcore.oxm.impl.annotation.DateSerializer dateAnno = (de.schlund.pfixcore.oxm.impl.annotation.DateSerializer) annotation;
30: pattern = dateAnno.value();
31: }
32: }
33:
34: public String serialize(Object obj, SerializationContext context)
35: throws SerializationException {
36: SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
37: if (obj instanceof Date) {
38: return dateFormat.format((Date) obj);
39: } else if (obj instanceof Calendar) {
40: return dateFormat.format(((Calendar) obj).getTime());
41: }
42: throw new SerializationException("Type not supported: "
43: + obj.getClass().getName());
44: }
45: }
|