public Node2JAXB(ExtensionPointRegistry registry) {
contextHelper = JAXBContextHelper.getInstance(registry);
}
public Object transform(Node source, TransformationContext context) {
ValidationEventCollector validationEventCollector = new ValidationEventCollector();
Object response = null;
if (source == null)
return null;
try {
JAXBContext jaxbContext = contextHelper.createJAXBContext(context, false);
Object result;
// TUSCANY-3791
synchronized(source){
/* some debug code
System.setProperty("jaxb.debug", "true");
unmarshaller.setListener(new DebugListener());
*/
Unmarshaller unmarshaller = contextHelper.getUnmarshaller(jaxbContext);
try {
validationEventCollector.reset();
unmarshaller.setEventHandler(validationEventCollector);
result = unmarshaller.unmarshal(source, JAXBContextHelper.getJavaType(context.getTargetDataType()));
} finally {
contextHelper.releaseJAXBUnmarshaller(jaxbContext, unmarshaller);
}
}
response = JAXBContextHelper.createReturnValue(jaxbContext, context.getTargetDataType(), result);
} catch (Exception e) {
throw new TransformationException(e);
}
if (validationEventCollector.hasEvents()){
String validationErrors = "";
for(ValidationEvent event : validationEventCollector.getEvents()){
validationErrors += "Event: " + event.getMessage() + " ";
}
throw new TransformationException(validationErrors);
}
return response;