public <T> Model<T> readModel(Reader message, Class<T> modelRoot) throws SAXException, IOException {
AssertArgument.isNotNull(message, "message");
AssertArgument.isNotNull(modelRoot, "modelRoot");
JavaResult result = new JavaResult();
ExecutionContext executionContext = descriptor.getSmooks().createExecutionContext();
Map<Class<?>, Map<String, BeanWriter>> beanWriters = descriptor.getBeanWriters();
BeanTracker beanTracker = new BeanTracker(beanWriters);
if(reportPath != null) {
executionContext.setEventListener(new HtmlReportGenerator(reportPath));
}
executionContext.getBeanContext().addObserver(beanTracker);
if(validate && descriptor.getSchema() != null) {
// Validate the message against the schemas...
Document messageDoc = toDocument(message);
// Validate the document and then filter it through smooks...
descriptor.getSchema().newValidator().validate(new DOMSource(messageDoc));
descriptor.getSmooks().filterSource(executionContext, new DOMSource(messageDoc), result);
} else {
descriptor.getSmooks().filterSource(executionContext, new StreamSource(message), result);
}
Model<T> model;
if(modelRoot == JavaResult.class) {
model = new Model<T>(modelRoot.cast(result), beanTracker.beans, beanWriters, NamespaceReaper.getNamespacePrefixMappings(executionContext));
} else {
model = new Model<T>(modelRoot.cast(result.getBean(modelRoot)), beanTracker.beans, beanWriters, NamespaceReaper.getNamespacePrefixMappings(executionContext));
}
return model;
}