public static < T > void writeObject ( final T entity , final Object destination , final String comment ) { try { JAXBContext jaxbContext ; if ( entity instanceof JAXBElement ) { jaxbContext = JAXBContext . newInstance ( ( ( JAXBElement ) entity ) . getValue ( ) . getClass ( ) ) ; } else { jaxbContext = JAXBContext . newInstance ( entity . getClass ( ) ) ; } Marshaller marshaller = jaxbContext . createMarshaller ( ) ; marshaller . setProperty ( Marshaller . JAXB_FORMATTED_OUTPUT , true ) ; if ( StringUtils . isNotBlank ( comment ) ) { marshaller . setProperty ( "com.sun.xml.bind.xmlHeaders" , comment ) ; } if ( destination instanceof java . io . OutputStream ) { marshaller . marshal ( entity , ( OutputStream ) destination ) ; } else if ( destination instanceof java . io . File ) { marshaller . marshal ( entity , ( java . io . File ) destination ) ; } else if ( destination instanceof java . io . Writer ) { marshaller . marshal ( entity , ( java . io . Writer ) destination ) ; } else { throw new IllegalArgumentException ( "Unsupported destination." ) ; } } catch ( final JAXBException e ) { throw new SwidException ( "Cannot write object." , e ) ; } }
Write XML entity to the given destination .