private static void validateDocumentAgainstSchema ( final Document xmlRootNode ) throws InvalidConfigurationException { final Element rootElement = xmlRootNode . getDocumentElement ( ) ; final String version = rootElement . getAttribute ( "version" ) ; String schemaFileName = "persistence_" + version . replace ( "." , "_" ) + ".xsd" ; try { final List validationErrors = new ArrayList ( ) ; final String schemaLanguage = XMLConstants . W3C_XML_SCHEMA_NS_URI ; final StreamSource streamSource = new StreamSource ( getStreamFromClasspath ( schemaFileName ) ) ; final Schema schemaDefinition = SchemaFactory . newInstance ( schemaLanguage ) . newSchema ( streamSource ) ; final Validator schemaValidator = schemaDefinition . newValidator ( ) ; schemaValidator . setErrorHandler ( new ErrorLogger ( "XML InputStream" , validationErrors ) ) ; schemaValidator . validate ( new DOMSource ( xmlRootNode ) ) ; if ( ! validationErrors . isEmpty ( ) ) { final String exceptionText = "persistence.xml is not conform against the supported schema definitions." ; throw new InvalidConfigurationException ( exceptionText ) ; } } catch ( SAXException e ) { final String exceptionText = "Error validating persistence.xml against schema defintion, caused by: " ; throw new InvalidConfigurationException ( exceptionText , e ) ; } catch ( IOException e ) { final String exceptionText = "Error opening xsd schema file. The given persistence.xml descriptor version " + version + " might not be supported yet." ; throw new InvalidConfigurationException ( exceptionText , e ) ; } }
Validates an xml object graph against its schema . Therefore it reads the version from the root tag and tries to load the related xsd file from the classpath .