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);
        }
    }
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.

@param entity
XML entity
@param destination
destination to write to. Supported destinations: {@link java.io.OutputStream}, {@link java.io.File},
{@link java.io.Writer}
@param comment
optional comment which will be added at the begining of the generated XML
@throws IllegalArgumentException
@throws SwidException
@param <T>
JAXB entity