throw new JBIException("Failed to load schema: " + e, e);
}
}
protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
Validator validator = schema.newValidator();
CountingErrorHandler errorHandler = new CountingErrorHandler();
validator.setErrorHandler(errorHandler);
DOMResult result = new DOMResult();
// Transform first so that the input source will be parsed only once
// if it is a StreamSource
getMessageTransformer().transform(exchange, in, out);
try {
// Only DOMSource and SAXSource are allowed for validating
// See http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/Validator.html#validate(javax.xml.transform.Source,%20javax.xml.transform.Result)
// As we expect a DOMResult as output, we must ensure that the input is a
// DOMSource
Source src = new SourceTransformer().toDOMSource(out.getContent());
validator.validate(src, result);
if (errorHandler.hasErrors()) {
Fault fault = exchange.createFault();
fault.setProperty("org.servicemix.schema", schema);
fault.setContent(new DOMSource(result.getNode(), result.getSystemId()));
throw new FaultException("Failed to validate against schema: " + schema, exchange, fault);