package com.skaringa.javaxml.serializers;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.xml.sax.Attributes;
import com.skaringa.javaxml.DeserializerException;
import com.skaringa.javaxml.SerializerException;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;
/**
* Implementation of ComponentSerializer for collections.
*/
public final class CollectionSerializer extends AbstractSerializer {
private String _xmlTypeName;
/**
* Construct a CollectionSerializer for a concrete collection type.
* @param xmlTypeName The XML type name of the collection.
*/
CollectionSerializer(String xmlTypeName) {
_xmlTypeName = xmlTypeName;
}
/**
* @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) {
java.util.Collection v = (java.util.Collection) obj;
java.util.Iterator it = v.iterator();
while (it.hasNext()) {
Object comp = it.next();
Class fieldType = Object.class;
if (null != comp) {
fieldType = comp.getClass();
}
ComponentSerializer ser =
SerializerRegistry.getInstance().getSerializer(fieldType);
ser.serialize(comp, fieldType, "cel", propertyMap, objectIdMap, output);
}
}
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 {
Collection coll = null;
String nullAttr = attrs.getValue("xsi:nil");
if (nullAttr == null || nullAttr.equals("false")) {
try {
// get the collection target type
Class targetType = Class.forName(_xmlTypeName, true, classLoader);
coll = (Collection) targetType.newInstance();
}
catch (ClassNotFoundException e) {
// fall through
}
catch (InstantiationException e) {
// fall through
}
catch (IllegalAccessException e) {
// fall through
}
catch (NullPointerException e) {
// fall through
}
if (coll == null) {
coll = new java.util.Vector();
}
}
return coll;
}
/**
* @see ComponentSerializer#endDeserialize(Object, String)
*/
public Object endDeserialize(Object obj, String text)
throws DeserializerException {
return obj;
}
/**
* @see ComponentSerializer#setMember(Object, String, Object)
*/
public void setMember(Object parent, String name, Object value)
throws DeserializerException {
if (parent == null) {
throw new DeserializerException("child of null object is forbidden");
}
if (parent instanceof Collection) {
Collection coll = (Collection) parent;
coll.add(value);
}
else {
throw new DeserializerException(
"invalid sequence: java.util.Collection expected, but was: "
+ parent.getClass().getName());
}
}
/**
* @see com.skaringa.javaxml.serializers.ComponentSerializer#writeXMLTypeDefinition(Class, Map, DocumentOutputHandlerInterface)
*/
public void writeXMLTypeDefinition(
Class type,
Map propertyMap,
DocumentOutputHandlerInterface output)
throws SerializerException {
if (Collection.class.equals(type)) {
writeXMLCollectionDef("cel", null, output);
}
else {
writeXMLExtensionDef(
output,
SerializerRegistry
.getInstance()
.getSerializer(Collection.class)
.getXMLTypeName());
}
}
/**
* @see ComponentSerializer#addUsedClasses(Class, Set)
*/
public void addUsedClasses(Class base, Set usedClasses)
throws SerializerException {
usedClasses.add(base);
usedClasses.add(Collection.class);
}
/**
* @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("null");
} else {
output.print("[");
java.util.Collection v = (java.util.Collection) obj;
java.util.Iterator it = v.iterator();
while (it.hasNext()) {
Object comp = it.next();
Class fieldType = Object.class;
if (null != comp) {
fieldType = comp.getClass();
}
ComponentSerializer ser =
SerializerRegistry.getInstance().getSerializer(fieldType);
ser.toJson(comp, fieldType, propertyMap, output);
if (it.hasNext()) {
output.print(",");
}
}
output.print("]");
}
}
}