Example usage for javax.xml.bind Marshaller getEventHandler

List of usage examples for javax.xml.bind Marshaller getEventHandler

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller getEventHandler.

Prototype

public ValidationEventHandler getEventHandler() throws JAXBException;

Source Link

Document

Return the current event handler or the default event handler if one hasn't been set.

Usage

From source file:org.apache.cxf.jaxbplus.io.DataWriterImpl.java

public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
    Class<?> cls = null;/*ww w . j a v a 2  s  .co  m*/
    if (part != null) {
        cls = part.getTypeClass();
    }

    if (cls == null) {
        cls = null != elValue ? elValue.getClass() : null;
    }

    if (cls != null && cls.isArray() && elValue instanceof Collection) {
        Collection<?> col = (Collection<?>) elValue;
        elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
    }
    Marshaller marshaller;
    try {

        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setListener(databinding.getMarshallerListener());
        if (databinding.getValidationEventHandler() != null) {
            marshaller.setEventHandler(databinding.getValidationEventHandler());
        }

        final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
        if (nspref != null) {
            JAXBUtils.setNamespaceWrapper(nspref, marshaller);
        }
        if (databinding.getMarshallerProperties() != null) {
            for (Map.Entry<String, Object> propEntry : databinding.getMarshallerProperties().entrySet()) {
                try {
                    marshaller.setProperty(propEntry.getKey(), propEntry.getValue());
                } catch (PropertyException pe) {
                    LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
                }
            }
        }

        marshaller.setSchema(schema);
        AttachmentMarshaller atmarsh = getAttachmentMarshaller();
        marshaller.setAttachmentMarshaller(atmarsh);

        if (schema != null && atmarsh instanceof JAXBAttachmentMarshaller) {
            //we need a special even handler for XOP attachments 
            marshaller.setEventHandler(new MtomValidationHandler(marshaller.getEventHandler(),
                    (JAXBAttachmentMarshaller) atmarsh));
        }
    } catch (JAXBException ex) {
        if (ex instanceof javax.xml.bind.MarshalException) {
            javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
            Message faultMessage = new Message("MARSHAL_ERROR", LOG,
                    marshalEx.getLinkedException().getMessage());
            throw new Fault(faultMessage, ex);
        } else {
            throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
        }
    }
    return marshaller;
}