package com.skaringa.javaxml.handler.sax;
import java.util.Map;
import com.skaringa.javaxml.SerializerException;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;
import com.skaringa.javaxml.serializers.ComponentSerializer;
import com.skaringa.javaxml.serializers.SerializerRegistry;
/**
* A implementation of the SAX2 XMLReader interface.
* This class is able to parse serializable objects.
*/
public final class ObjectXMLReader extends AbstractXMLReader {
private Map _objectIdMap;
/**
* Construct an instance of ObjectXMLReader.
* @throws SerializerException If an implemention of identity hash map can't be found.
*/
public ObjectXMLReader() throws SerializerException {
// This bunch of code is only necessary to be compatible with JDK 1.3
// JDK 1.3 doesn't have an implementation of IdentityHashMap, therefore
// IdentityMap from Jakarta Commons Collections is used in this case.
Class identityMapClass;
try {
identityMapClass = Class.forName("java.util.IdentityHashMap");
// since Java 1.4
}
catch (ClassNotFoundException e) {
try {
identityMapClass =
Class.forName("org.apache.commons.collections.map.IdentityMap");
// fallback for Java 1.3
}
catch (ClassNotFoundException ee) {
throw new SerializerException(
"Skaringa needs an implemation "
+ "of an identity hash map either from JDK >1.4 "
+ "or from Jakarta commons collections");
}
}
try {
_objectIdMap = (Map) identityMapClass.newInstance();
}
catch (InstantiationException e) {
throw new SerializerException(e.toString());
}
catch (IllegalAccessException e) {
throw new SerializerException(e.toString());
}
}
/**
* @see AbstractXMLReader#parseObject(Object, Class, DocumentOutputHandlerInterface)
*/
public void parseObject(
Object obj,
Class type,
DocumentOutputHandlerInterface output)
throws SerializerException {
output.setProperties(getPropertyMap());
ComponentSerializer ser =
SerializerRegistry.getInstance().getSerializer(type);
output.startDocument();
ser.serialize(
obj,
type,
ser.getXMLTypeName(),
getPropertyMap(),
_objectIdMap,
output);
output.endDocument();
}
}