@SuppressWarnings( { "unchecked" })
public JaxbRoot unmarshal(Document document, Origin origin) {
Element rootElement = document.getDocumentElement();
if ( rootElement == null ) {
throw new MappingException( "No root element found", origin );
}
final Schema validationSchema;
final Class jaxbTarget;
if ( "entity-mappings".equals( rootElement.getNodeName() ) ) {
final String explicitVersion = rootElement.getAttribute( "version" );
validationSchema = resolveSupportedOrmXsd( explicitVersion );
jaxbTarget = JaxbEntityMappings.class;
}
else {
validationSchema = hbmSchema();
jaxbTarget = JaxbHibernateMapping.class;
}
final Object target;
try {
JAXBContext jaxbContext = JAXBContext.newInstance( jaxbTarget );
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema( validationSchema );
target = unmarshaller.unmarshal( new DOMSource( document ) );
}
catch ( JAXBException e ) {
throw new MappingException( "Unable to perform unmarshalling", e, origin );
}
return new JaxbRoot( target, origin );
}