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
{
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 );
}
}