Search in sources :

Example 11 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project tomee by apache.

the class DataWriterImpl method createMarshaller.

public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
    // Class<?> cls = null;
    // 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, StandardCharsets.UTF_8.name());
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setListener(databinding.getMarshallerListener());
        databinding.applyEscapeHandler(!noEscape, eh -> JAXBUtils.setEscapeHandler(marshaller, eh));
        if (setEventHandler) {
            ValidationEventHandler h = veventHandler;
            if (veventHandler == null) {
                h = new ValidationEventHandler() {

                    public boolean handleEvent(ValidationEvent event) {
                        // continue on warnings only
                        return event.getSeverity() == ValidationEvent.WARNING;
                    }
                };
            }
            marshaller.setEventHandler(h);
        }
        final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
        final Map<String, String> nsctxt = databinding.getContextualNamespaceMap();
        // set the prefix mapper if either of the prefix map is configured
        if (nspref != null || nsctxt != null) {
            Object mapper = JAXBUtils.setNamespaceMapper(bus, nspref != null ? nspref : nsctxt, marshaller);
            if (nsctxt != null) {
                setContextualNamespaceDecls(mapper, nsctxt);
            }
        }
        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 (javax.xml.bind.MarshalException ex) {
        Message faultMessage = new Message("MARSHAL_ERROR", LOG, ex.getLinkedException().getMessage());
        throw new Fault(faultMessage, ex);
    } catch (JAXBException ex) {
        throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
    }
    for (XmlAdapter<?, ?> adapter : databinding.getConfiguredXmlAdapters()) {
        marshaller.setAdapter(adapter);
    }
    return marshaller;
}
Also used : AttachmentMarshaller(javax.xml.bind.attachment.AttachmentMarshaller) JAXBAttachmentMarshaller(org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller) Marshaller(javax.xml.bind.Marshaller) AttachmentMarshaller(javax.xml.bind.attachment.AttachmentMarshaller) JAXBAttachmentMarshaller(org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Message(org.apache.cxf.common.i18n.Message) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) Fault(org.apache.cxf.interceptor.Fault) JAXBAttachmentMarshaller(org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller) MarshalException(javax.xml.bind.MarshalException) ValidationEvent(javax.xml.bind.ValidationEvent) Map(java.util.Map)

Example 12 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project cxf by apache.

the class JAXBDataBase method getValidationEventHandler.

protected ValidationEventHandler getValidationEventHandler(Message m, String property) {
    Object value = m.getContextualProperty(property);
    ValidationEventHandler veventHandler;
    if (value instanceof String) {
        veventHandler = getValidationEventHandler((String) value);
    } else {
        veventHandler = (ValidationEventHandler) value;
    }
    if (veventHandler == null) {
        value = m.getContextualProperty(JAXBDataBinding.VALIDATION_EVENT_HANDLER);
        if (value instanceof String) {
            veventHandler = getValidationEventHandler((String) value);
        } else {
            veventHandler = (ValidationEventHandler) value;
        }
    }
    return veventHandler;
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler)

Example 13 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project OpenAM by OpenRock.

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Example 14 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project OpenAM by OpenRock.

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Example 15 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project OpenAM by OpenRock.

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Aggregations

ValidationEventHandler (javax.xml.bind.ValidationEventHandler)29 ValidationEvent (javax.xml.bind.ValidationEvent)21 Unmarshaller (javax.xml.bind.Unmarshaller)13 JAXBContext (javax.xml.bind.JAXBContext)12 InputSource (org.xml.sax.InputSource)12 SAXSource (javax.xml.transform.sax.SAXSource)11 SAXParser (javax.xml.parsers.SAXParser)10 SAXParserFactory (javax.xml.parsers.SAXParserFactory)10 JAXBException (javax.xml.bind.JAXBException)7 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)5 Marshaller (javax.xml.bind.Marshaller)5 UnmarshalException (javax.xml.bind.UnmarshalException)5 SAXException (org.xml.sax.SAXException)5 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Map (java.util.Map)2 MarshalException (javax.xml.bind.MarshalException)2 PropertyException (javax.xml.bind.PropertyException)2 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)2