Reorganize the SAX event pipeline, this fixes the problem of slow parsing when the namespace is not correct
git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@350057 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cbd121a8be
commit
8158ed0a6a
@ -66,6 +66,7 @@ public class JaxpMsvBean
|
|||||||
private static final int MSV_FATAL_ERROR = 2;
|
private static final int MSV_FATAL_ERROR = 2;
|
||||||
|
|
||||||
private boolean isValid = true;
|
private boolean isValid = true;
|
||||||
|
private boolean validNamespace = true;
|
||||||
private XPathLocationTracker tracker;
|
private XPathLocationTracker tracker;
|
||||||
|
|
||||||
//~ Methods --------------------------------------------------------------
|
//~ Methods --------------------------------------------------------------
|
||||||
@ -82,18 +83,20 @@ public class JaxpMsvBean
|
|||||||
verifier.setErrorHandler( new ErrorHandlerImpl() );
|
verifier.setErrorHandler( new ErrorHandlerImpl() );
|
||||||
|
|
||||||
VerifierHandler handler = verifier.getVerifierHandler();
|
VerifierHandler handler = verifier.getVerifierHandler();
|
||||||
tracker = new XPathLocationTracker( handler );
|
|
||||||
|
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||||
factory.setNamespaceAware( true );
|
factory.setNamespaceAware( true );
|
||||||
factory.setValidating( false );
|
factory.setValidating( false );
|
||||||
|
|
||||||
|
|
||||||
XMLReader reader = factory.newSAXParser().getXMLReader();
|
XMLReader reader = factory.newSAXParser().getXMLReader();
|
||||||
reader.setContentHandler( tracker );
|
|
||||||
reader.setEntityResolver( new EntityResolverImpl() );
|
|
||||||
|
|
||||||
reader.parse( new InputSource( new FileInputStream( file ) ) );
|
tracker = new XPathLocationTracker();
|
||||||
|
tracker.setParent( reader );
|
||||||
|
tracker.setContentHandler( handler );
|
||||||
|
tracker.setEntityResolver( new EntityResolverImpl() );
|
||||||
|
|
||||||
|
tracker.parse( new InputSource( new FileInputStream( file ) ) );
|
||||||
|
|
||||||
endMessage();
|
endMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,33 +141,36 @@ 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!" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setValid(boolean valid)
|
|
||||||
{
|
|
||||||
this.isValid = valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 )
|
||||||
|
{
|
||||||
|
// if namespace is not correct, error messages are crap
|
||||||
|
if ( validNamespace )
|
||||||
{
|
{
|
||||||
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() + "," );
|
||||||
@ -178,35 +184,26 @@ 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 );
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void error( SAXParseException e ) throws SAXException
|
public void error( SAXParseException e ) throws SAXException
|
||||||
{
|
{
|
||||||
/*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);
|
errorMessage( e, MSV_ERROR);
|
||||||
setValid( false );
|
isValid = false;
|
||||||
|
|
||||||
if ( e.getMessage() != null
|
if ( e.getMessage() != null
|
||||||
&& e.getMessage().startsWith("namespace URI of tag") )
|
&& e.getMessage().startsWith("namespace URI of tag") )
|
||||||
{
|
{
|
||||||
// Fail the build if namespace declaration is wrong.
|
validNamespace = false;
|
||||||
// Otherwise parsing seems to hang after the first element.
|
|
||||||
throw new SAXException( e );
|
|
||||||
}
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 );
|
isValid = false;
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,8 +21,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.ContentHandler;
|
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
import org.xml.sax.SAXParseException;
|
||||||
import org.xml.sax.helpers.XMLFilterImpl;
|
import org.xml.sax.helpers.XMLFilterImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,7 +31,6 @@ import org.xml.sax.helpers.XMLFilterImpl;
|
|||||||
*
|
*
|
||||||
* @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
|
* @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class XPathLocationTracker extends XMLFilterImpl
|
public class XPathLocationTracker extends XMLFilterImpl
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -45,15 +44,6 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
new Integer( 4 )
|
new Integer( 4 )
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor: sets the ContentHandler.
|
|
||||||
* @param handler The ContentHandler
|
|
||||||
*/
|
|
||||||
public XPathLocationTracker( ContentHandler handler )
|
|
||||||
{
|
|
||||||
setContentHandler(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overriding ContentHandler.
|
* Overriding ContentHandler.
|
||||||
* @throws SAXException SAXException
|
* @throws SAXException SAXException
|
||||||
@ -86,8 +76,16 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
String qName, Attributes atts ) throws SAXException
|
String qName, Attributes atts ) throws SAXException
|
||||||
{
|
{
|
||||||
state = state.push( qName );
|
state = state.push( qName );
|
||||||
|
try
|
||||||
|
{
|
||||||
super.startElement( uri, localName, qName, atts );
|
super.startElement( uri, localName, qName, atts );
|
||||||
}
|
}
|
||||||
|
catch ( SAXParseException e )
|
||||||
|
{
|
||||||
|
// ignored on purpose: just avoid to throw an exception here
|
||||||
|
// it is thrown by the ErrorHandler in JaxpMsvBean instead
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overriding ContentHandler.
|
* Overriding ContentHandler.
|
||||||
@ -98,8 +96,16 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
*/
|
*/
|
||||||
public void endElement( String uri, String localName, String qName )
|
public void endElement( String uri, String localName, String qName )
|
||||||
throws SAXException
|
throws SAXException
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
super.endElement( uri, localName, qName );
|
super.endElement( uri, localName, qName );
|
||||||
|
}
|
||||||
|
catch ( SAXParseException e )
|
||||||
|
{
|
||||||
|
// ignored on purpose: just avoid to throw an exception here
|
||||||
|
// it is thrown by the ErrorHandler in JaxpMsvBean instead
|
||||||
|
}
|
||||||
state = state.pop();
|
state = state.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +130,9 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
if ( i < ints.length )
|
if ( i < ints.length )
|
||||||
{
|
{
|
||||||
return ints[i];
|
return ints[i];
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return new Integer(i);
|
return new Integer(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,7 +157,9 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
if ( child == null )
|
if ( child == null )
|
||||||
{
|
{
|
||||||
child = new State( this );
|
child = new State( this );
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
child.reset();
|
child.reset();
|
||||||
}
|
}
|
||||||
return child;
|
return child;
|
||||||
@ -167,7 +177,9 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
if ( i == null )
|
if ( i == null )
|
||||||
{
|
{
|
||||||
i = getInt( 1 );
|
i = getInt( 1 );
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
i = getInt( i.intValue() + 1 );
|
i = getInt( i.intValue() + 1 );
|
||||||
}
|
}
|
||||||
counter.put( rawName, i );
|
counter.put( rawName, i );
|
||||||
@ -189,7 +201,9 @@ public class XPathLocationTracker extends XMLFilterImpl
|
|||||||
{
|
{
|
||||||
xPath += currentName;
|
xPath += currentName;
|
||||||
}
|
}
|
||||||
} else { // child node
|
}
|
||||||
|
else // child node
|
||||||
|
{
|
||||||
xPath = parent.getXPath();
|
xPath = parent.getXPath();
|
||||||
if ( currentName != null )
|
if ( currentName != null )
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user