package com.skaringa.javaxml.serializers;
import java.io.PrintStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TimeZone;
import org.xml.sax.Attributes;
import com.skaringa.javaxml.DeserializerException;
import com.skaringa.javaxml.SerializerException;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;
import com.skaringa.util.ISO8601DateFormat;
import com.skaringa.util.ISO8601DateTimeFormat;
/**
* Implementation of ComponentSerializer for java.util.Date
*/
public final class DateSerializer extends AbstractSerializer {
private Date _dummy = new Date();
private String _xmlTypeName = "xsd:dateTime";
/**
* Construct a DateSerializer for xsd:dateTime type
*/
public DateSerializer() {
}
/**
* Construct a DateSerializer for a given xml type
* @param xmlType may be 'xsd:dateTime' or 'xsd:date'
*/
public DateSerializer(String xmlType) {
_xmlTypeName = xmlType;
}
/**
* @see ComponentSerializer#serialize(Object, Class, String, Map, Map, DocumentOutputHandlerInterface)
*/
public void serialize(
Object obj,
Class type,
String name,
Map propertyMap,
Map objectIdMap,
DocumentOutputHandlerInterface output)
throws SerializerException {
startElement(obj, getXMLTypeName(), name, propertyMap, output);
if (obj != null) {
output.appendText(
newFormatter(_xmlTypeName).format((java.util.Date) obj));
}
output.endElement(name);
}
/**
* @see ComponentSerializer#getXMLTypeName()
*/
public String getXMLTypeName() {
return _xmlTypeName;
}
/**
* @see ComponentSerializer#startDeserialize(String, Attributes, Object, Stack, ClassLoader)
*/
public Object startDeserialize(
String name,
Attributes attrs,
Object parent,
Stack objStack,
ClassLoader classLoader)
throws DeserializerException {
Date obj = null;
String nullAttr = attrs.getValue("xsi:nil");
if (nullAttr == null || nullAttr.equals("false")) {
obj = _dummy;
// defer creation of corrrect value until endDeserialize()
}
return obj;
}
/**
* @see ComponentSerializer#endDeserialize(Object, String)
*/
public Object endDeserialize(Object obj, String text)
throws DeserializerException {
if (obj != null) {
try {
obj = newFormatter(_xmlTypeName).parse(text);
}
catch (java.text.ParseException e) {
throw new DeserializerException(e.getMessage());
}
}
return obj;
}
/**
* @see ComponentSerializer#setMember(Object, String, Object)
*/
public void setMember(Object parent, String name, Object value)
throws DeserializerException {
// not allowed with date type
throw new DeserializerException(
"no child element allowed for java.util.Date");
}
/**
* @see com.skaringa.javaxml.serializers.ComponentSerializer#writeXMLTypeDefinition(Class, Map, DocumentOutputHandlerInterface)
*/
public void writeXMLTypeDefinition(
Class type,
Map propertyMap,
DocumentOutputHandlerInterface output)
throws SerializerException {
// nothing to define
}
/**
* @see ComponentSerializer#addUsedClasses(Class, Set)
*/
public void addUsedClasses(Class base, Set usedClasses)
throws SerializerException {
// no other classes used
}
/**
* Returns the appropriate formatter for the specified XML type.
*
* @param xmlType may be 'xsd:dateTime' or 'xsd:date'
* @return a <code>DateFormat</code> value
*/
private DateFormat newFormatter(String xmlType) {
if ("xsd:date".equals(xmlType)) {
return new ISO8601DateFormat(TimeZone.getDefault());
}
return new ISO8601DateTimeFormat(TimeZone.getDefault());
}
/**
* @see CollectionSerializer#toJson(Object, Class, Map, PrintStream)
*/
public void toJson(Object obj, Class type, Map propertyMap,
PrintStream output) throws SerializerException {
if (obj != null) {
output.print('"');
output.print(
newFormatter(_xmlTypeName).format((java.util.Date) obj));
output.print('"');
} else {
output.print("null");
}
}
}