protected byte[] serialize( final Object o, final XMLBinding binding ) {
if ( o == null ) {
throw new NullPointerException( "Can't serialize null" );
}
XMLObjectWriter writer = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
writer = XMLObjectWriter.newInstance( bos );
final XMLReferenceResolver xmlReferenceResolver = new XMLReferenceResolver();
xmlReferenceResolver.setIdentifierAttribute( JavolutionTranscoder.REFERENCE_ATTRIBUTE_ID );
xmlReferenceResolver.setReferenceAttribute( JavolutionTranscoder.REFERENCE_ATTRIBUTE_REF_ID );
writer.setReferenceResolver( xmlReferenceResolver );
writer.setBinding( binding );
writer.write( o, "root" );
writer.flush();
return bos.toByteArray();
} catch ( final Exception e ) {
throw new IllegalArgumentException( "Non-serializable object", e );
} finally {
try {
writer.close();
} catch ( final XMLStreamException e ) {
// fail silently
}
}