Make namespace aware. Fail the build in case of namespace mismatch to avoid msv hanging. Code formatting.

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@345548 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ltheussl 2005-11-18 19:36:11 +00:00
parent b0bd6ac521
commit 1141d95fc6

View File

@ -76,23 +76,24 @@ public class JaxpMsvBean
*/ */
public void validate() throws Exception public void validate() throws Exception
{ {
VerifierFactory verifierFactory = new TheFactoryImpl(); VerifierFactory verifierFactory = new TheFactoryImpl();
Verifier verifier = verifierFactory.newVerifier(new File(schema));
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.setErrorHandler( new ErrorHandlerImpl() ); 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(); endMessage();
} }
@ -136,9 +137,9 @@ public class JaxpMsvBean
{ {
if ( isValid ) if ( isValid )
{ {
log.info(file + " verified: OK"); log.info( file + " verified: OK" );
} else { } 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) 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() log.error( " ERROR on line " + e.getLineNumber()
+ " of file " + xmlFile.getName() + "," ); + " of file " + xmlFile.getName() + "," );
log.error( " XPath location " + tracker.getXPath() + ":" ); log.error( " XPath location " + tracker.getXPath() + ":" );
log.error( " " + e.getMessage() ); log.error( " " + e.getMessage() );
} else if (type == MSV_FATAL_ERROR) } else if ( type == MSV_FATAL_ERROR )
{ {
log.error( " Non-recoverable parsing error on line " log.error( " Non-recoverable parsing error on line "
+ e.getLineNumber() + " of file " + xmlFile.getName() + "," ); + e.getLineNumber() + " of file " + xmlFile.getName() + "," );
log.error( " XPath location " + tracker.getXPath() + ":" ); log.error( " XPath location " + tracker.getXPath() + ":" );
log.error( " " + e.getMessage() ); log.error( " " + e.getMessage() );
} else if (type == MSV_WARNING) } else if ( type == MSV_WARNING )
{ {
log.warn( " WARNING on line " + e.getLineNumber() log.warn( " WARNING on line " + e.getLineNumber()
+ " of file " + xmlFile.getName() + "," ); + " of file " + xmlFile.getName() + "," );
@ -176,39 +177,47 @@ public class JaxpMsvBean
{ {
public void warning(SAXParseException e) throws SAXException public void warning(SAXParseException e) throws SAXException
{ {
errorMessage(e, MSV_WARNING); errorMessage( e, MSV_WARNING );
} }
public void error(SAXParseException e) throws SAXException 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" // unexpected attribute "xsi:schemaLocation"
// ignore, this is due to a valid xsd declaration // 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 // Jaxp ignores additionals namespaces declared in the xml file (xmlns:xsi) and it can't validate
// using multiple schema at once // using multiple schema at once
return; 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 public void fatalError(SAXParseException e) throws SAXException
{ {
errorMessage(e, MSV_FATAL_ERROR); errorMessage( e, MSV_FATAL_ERROR );
setValid(false); setValid( false );
} }
} }
private class EntityResolverImpl implements EntityResolver private class EntityResolverImpl implements EntityResolver
{ {
public InputSource resolveEntity(String publicId, public InputSource resolveEntity(String publicId,
String systemId) throws SAXException String systemId) throws SAXException
{ {
log.warn(" WARNING: External entity " + systemId log.warn( " WARNING: External entity " + systemId
+ " won't be resolved"); + " won't be resolved" );
return new InputSource(EMPTY_STREAM); return new InputSource( EMPTY_STREAM );
} }
} }