Author: ltheussl Date: Wed Nov 30 13:32:02 2005 New Revision: 350057 URL: http://svn.apache.org/viewcvs?rev=350057&view=rev Log: Reorganize the SAX event pipeline, this fixes the problem of slow parsing when the namespace is not correct
Modified: maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/JaxpMsvBean.java maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/XPathLocationTracker.java Modified: maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/JaxpMsvBean.java URL: http://svn.apache.org/viewcvs/maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/JaxpMsvBean.java?rev=350057&r1=350056&r2=350057&view=diff ============================================================================== --- maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/JaxpMsvBean.java (original) +++ maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/JaxpMsvBean.java Wed Nov 30 13:32:02 2005 @@ -66,6 +66,7 @@ private static final int MSV_FATAL_ERROR = 2; private boolean isValid = true; + private boolean validNamespace = true; private XPathLocationTracker tracker; //~ Methods -------------------------------------------------------------- @@ -82,18 +83,20 @@ verifier.setErrorHandler( new ErrorHandlerImpl() ); 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 ) ) ); + tracker = new XPathLocationTracker(); + tracker.setParent( reader ); + tracker.setContentHandler( handler ); + tracker.setEntityResolver( new EntityResolverImpl() ); + + tracker.parse( new InputSource( new FileInputStream( file ) ) ); + endMessage(); } @@ -101,7 +104,7 @@ * Sets the schema. * @param newSchema The schema to set */ - public void setSchema(String newSchema) + public void setSchema( String newSchema ) { this.schema = newSchema; } @@ -110,7 +113,7 @@ * Sets the file. * @param newFile The file to set */ - public void setFile(String newFile) + public void setFile( String newFile ) { this.file = newFile; } @@ -138,33 +141,36 @@ if ( isValid ) { log.info( file + " verified: OK" ); - } else { + } + else + { 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 ); 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 ) + // if namespace is not correct, error messages are crap + if ( validNamespace ) + { + 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 ) { 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() + "," ); @@ -175,45 +181,36 @@ private class ErrorHandlerImpl implements ErrorHandler { - public void warning(SAXParseException e) throws SAXException + public void warning( SAXParseException e ) throws SAXException { 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) ; - setValid( false ); - + errorMessage( e, MSV_ERROR); + isValid = 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 ); + validNamespace = false; } - + throw e; } - public void fatalError(SAXParseException e) throws SAXException + public void fatalError( SAXParseException e ) throws SAXException { errorMessage( e, MSV_FATAL_ERROR ); - setValid( false ); + isValid = false; + throw e; } } private class EntityResolverImpl implements EntityResolver { - public InputSource resolveEntity(String publicId, - String systemId) throws SAXException + public InputSource resolveEntity( String publicId, + String systemId ) throws SAXException { log.warn( " WARNING: External entity " + systemId + " won't be resolved" ); Modified: maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/XPathLocationTracker.java URL: http://svn.apache.org/viewcvs/maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/XPathLocationTracker.java?rev=350057&r1=350056&r2=350057&view=diff ============================================================================== --- maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/XPathLocationTracker.java (original) +++ maven/maven-1/plugins/trunk/plugin/src/main/org/apache/maven/XPathLocationTracker.java Wed Nov 30 13:32:02 2005 @@ -21,8 +21,8 @@ import java.util.Map; import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; import org.xml.sax.helpers.XMLFilterImpl; /** @@ -31,36 +31,26 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Lukas Theussl</a> */ - public class XPathLocationTracker extends XMLFilterImpl { private State state; private static final Integer[] ints = new Integer[] { - new Integer(0), - new Integer(1), - new Integer(2), - new Integer(3), - new Integer(4) + new Integer( 0 ), + new Integer( 1 ), + new Integer( 2 ), + new Integer( 3 ), + new Integer( 4 ) }; /** - * Constructor: sets the ContentHandler. - * @param handler The ContentHandler - */ - public XPathLocationTracker( ContentHandler handler ) - { - setContentHandler(handler); - } - - /** * Overriding ContentHandler. * @throws SAXException SAXException */ public void startDocument() throws SAXException { - state = new State(null); + state = new State( null ); super.startDocument(); } @@ -82,11 +72,19 @@ * @param atts atts * @throws SAXException SAXException */ - public void startElement(String uri, String localName, - String qName, Attributes atts) throws SAXException + public void startElement( String uri, String localName, + String qName, Attributes atts ) throws SAXException { - state = state.push(qName); - super.startElement(uri, localName, qName, atts); + state = state.push( qName ); + try + { + 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 + } } /** @@ -96,10 +94,18 @@ * @param qName qName * @throws SAXException SAXException */ - public void endElement(String uri, String localName, String qName) + public void endElement( String uri, String localName, String qName ) throws SAXException { - super.endElement(uri, localName, qName); + try + { + 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(); } @@ -114,17 +120,19 @@ if ( state == null ) { throw new IllegalStateException( - "startDocument event is not invoked"); + "startDocument event is not invoked" ); } return state.getXPath(); } - private static Integer getInt(int i) + private static Integer getInt( int i ) { if ( i < ints.length ) { return ints[i]; - } else { + } + else + { return new Integer(i); } } @@ -148,8 +156,10 @@ currentName = rawName; if ( child == null ) { - child = new State(this); - } else { + child = new State( this ); + } + else + { child.reset(); } return child; @@ -163,14 +173,16 @@ private void count( String rawName ) { - Integer i = (Integer) counter.get(rawName); + Integer i = (Integer) counter.get( rawName ); if ( i == null ) { - i = getInt(1); - } else { - i = getInt(i.intValue() + 1); + i = getInt( 1 ); } - counter.put(rawName, i); + else + { + i = getInt( i.intValue() + 1 ); + } + counter.put( rawName, i ); } private void reset() @@ -185,16 +197,18 @@ if ( parent == null ) // root { xPath = "/"; - if ( currentName != null) + if ( currentName != null ) { xPath += currentName; } - } else { // child node + } + else // child node + { xPath = parent.getXPath(); if ( currentName != null ) { xPath += '/' + currentName; - Integer i = (Integer) counter.get(currentName); + Integer i = (Integer) counter.get( currentName ); xPath += '[' + i.toString() + ']'; } }