use of org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller 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;
}
use of org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller in project cxf 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;
}
Aggregations