diff --git a/plugin/src/main/org/apache/maven/JaxpMsvBean.java b/plugin/src/main/org/apache/maven/JaxpMsvBean.java index 44fed9b2..f09863f8 100644 --- a/plugin/src/main/org/apache/maven/JaxpMsvBean.java +++ b/plugin/src/main/org/apache/maven/JaxpMsvBean.java @@ -76,23 +76,24 @@ public class JaxpMsvBean */ public void validate() throws Exception { - VerifierFactory verifierFactory = new TheFactoryImpl(); - Verifier verifier = verifierFactory.newVerifier(new File(schema)); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(false); - factory.setValidating(false); - XMLReader reader = factory.newSAXParser().getXMLReader(); - - VerifierHandler handler = verifier.getVerifierHandler(); - tracker = new XPathLocationTracker(handler); - reader.setContentHandler(tracker); - reader.setEntityResolver( new EntityResolverImpl() ); + Verifier verifier = verifierFactory.newVerifier( new File( schema ) ); verifier.setErrorHandler( new ErrorHandlerImpl() ); - reader.parse(new InputSource(new FileInputStream(file))); + VerifierHandler handler = verifier.getVerifierHandler(); + tracker = new XPathLocationTracker( handler ); + + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware( true ); + factory.setValidating( false ); + + + XMLReader reader = factory.newSAXParser().getXMLReader(); + reader.setContentHandler( tracker ); + reader.setEntityResolver( new EntityResolverImpl() ); + + reader.parse( new InputSource( new FileInputStream( file ) ) ); endMessage(); } @@ -136,9 +137,9 @@ public class JaxpMsvBean { if ( isValid ) { - log.info(file + " verified: OK"); + log.info( file + " verified: OK" ); } else { - log.info(file + " is NOT valid!"); + log.info( file + " is NOT valid!" ); } } @@ -149,21 +150,21 @@ public class JaxpMsvBean private void errorMessage(SAXParseException e, int type) { - File xmlFile = new File(file); + File xmlFile = new File( file ); - if (type == MSV_ERROR) + if ( type == MSV_ERROR ) { log.error( " ERROR on line " + e.getLineNumber() + " of file " + xmlFile.getName() + "," ); log.error( " XPath location " + tracker.getXPath() + ":" ); log.error( " " + e.getMessage() ); - } else if (type == MSV_FATAL_ERROR) + } else if ( type == MSV_FATAL_ERROR ) { log.error( " Non-recoverable parsing error on line " + e.getLineNumber() + " of file " + xmlFile.getName() + "," ); log.error( " XPath location " + tracker.getXPath() + ":" ); log.error( " " + e.getMessage() ); - } else if (type == MSV_WARNING) + } else if ( type == MSV_WARNING ) { log.warn( " WARNING on line " + e.getLineNumber() + " of file " + xmlFile.getName() + "," ); @@ -176,39 +177,47 @@ public class JaxpMsvBean { public void warning(SAXParseException e) throws SAXException { - errorMessage(e, MSV_WARNING); + errorMessage( e, MSV_WARNING ); } public void error(SAXParseException e) throws SAXException { - if (e.getMessage() != null && e.getMessage().indexOf("xsi:schemaLocation") > -1) + /*if (e.getMessage() != null && e.getMessage().indexOf("xsi:schemaLocation") > -1) { // unexpected attribute "xsi:schemaLocation" // ignore, this is due to a valid xsd declaration // Jaxp ignores additionals namespaces declared in the xml file (xmlns:xsi) and it can't validate // using multiple schema at once return; + }*/ + errorMessage( e, MSV_ERROR) ; + setValid( false ); + + if ( e.getMessage() != null + && e.getMessage().startsWith("namespace URI of tag") ) + { + // Fail the build if namespace declaration is wrong. + // Otherwise parsing seems to hang after the first element. + throw new SAXException( e ); } - errorMessage(e, MSV_ERROR); - setValid(false); + } public void fatalError(SAXParseException e) throws SAXException { - errorMessage(e, MSV_FATAL_ERROR); - setValid(false); + errorMessage( e, MSV_FATAL_ERROR ); + setValid( false ); } } - private class EntityResolverImpl implements EntityResolver { public InputSource resolveEntity(String publicId, String systemId) throws SAXException { - log.warn(" WARNING: External entity " + systemId - + " won't be resolved"); - return new InputSource(EMPTY_STREAM); + log.warn( " WARNING: External entity " + systemId + + " won't be resolved" ); + return new InputSource( EMPTY_STREAM ); } }