Show XPath location of an error

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@306542 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ltheussl committed 2005-10-06 06:23:52 +00:00
1 parent 652d1a563d
commit f0167d57a5
2 files changed
+311 -73

No files matched your search

+105 -73
View File
@@ -17,22 +17,25 @@ package org.apache.maven;
* ====================================================================
*/
import com.sun.msv.verifier.jaxp.SAXParserFactoryImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.*;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.iso_relax.verifier.Verifier;
import org.iso_relax.verifier.VerifierFactory;
import org.iso_relax.verifier.VerifierHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
/**
* JaxpMsvBean Bean: Uses JAXP implementation of MSV.
@@ -52,86 +55,47 @@ public class JaxpMsvBean
/** For debug output. */
private Log log = LogFactory.getLog(JaxpMsvBean.class);
private static String EMPTY = "";
private static ByteArrayInputStream EMPTY_STREAM =
private static final String EMPTY = "";
private static final ByteArrayInputStream EMPTY_STREAM =
new ByteArrayInputStream(EMPTY.getBytes());
private static int MSV_WARNING = 0;
private static int MSV_ERROR = 1;
private static int MSV_FATAL_ERROR = 2;
private static final int MSV_WARNING = 0;
private static final int MSV_ERROR = 1;
private static final int MSV_FATAL_ERROR = 2;
private boolean isValid = true;
private XPathLocationTracker tracker;
//~ Methods --------------------------------------------------------------
/**
* Performs validation.
* @throws Exception Exception
*/
public void validate() throws Exception
public void validate() throws Exception
{
SAXParserFactory factory =
new SAXParserFactoryImpl(new File(schema));
VerifierFactory verifierFactory =
new com.sun.msv.verifier.jarv.TheFactoryImpl();
Verifier verifier = verifierFactory.newVerifier(new File(schema));
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
parser.parse(new File(file), new DefaultHandler()
{
boolean isValid = true;
public void warning(SAXParseException e) throws SAXException
{
errorMessage(e, MSV_WARNING);
}
public void error(SAXParseException e) throws SAXException
{
errorMessage(e, MSV_ERROR);
isValid = false;
}
public void fatalError(SAXParseException e) throws SAXException
{
errorMessage(e, MSV_FATAL_ERROR);
isValid = false;
}
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException
{
log.warn("WARNING: External entity " + systemId
+ " won't be resolved!");
return new InputSource(EMPTY_STREAM);
}
public void endDocument()
{
if(isValid)
{
log.info(file + " verified: OK");
} else {
log.info("WARNING: " + file + " is NOT valid");
}
}
});
}
XMLReader reader = factory.newSAXParser().getXMLReader();
private void errorMessage(SAXParseException e, int type)
{
File xmlFile = new File(file);
VerifierHandler handler = verifier.getVerifierHandler();
tracker = new XPathLocationTracker(handler);
reader.setContentHandler(tracker);
reader.setEntityResolver( new EntityResolverImpl() );
verifier.setErrorHandler( new ErrorHandlerImpl() );
if (type == MSV_ERROR)
{
log.error( "com.sun.msv.verifier.ValidityViolation on line "
+ e.getLineNumber() + " of " + xmlFile.getName() + ":" );
log.error( e.getMessage() );
} else if (type == MSV_FATAL_ERROR)
{
log.error( "Non-recoverable parsing error on line "
+ e.getLineNumber() + " of " + xmlFile.getName() + ":" );
log.error( e.getMessage() );
} else if (type == MSV_WARNING)
{
log.warn( "Warning on line "
+ e.getLineNumber() + " of " + xmlFile.getName() + ":" );
log.warn( e.getMessage() );
}
reader.parse(new InputSource(new FileInputStream(file)));
endMessage();
}
/**
* Sets the schema.
*
* @param newSchema The schema to set
*/
public void setSchema(String newSchema)
@@ -141,7 +105,6 @@ public class JaxpMsvBean
/**
* Sets the file.
*
* @param newFile The file to set
*/
public void setFile(String newFile)
@@ -151,7 +114,6 @@ public class JaxpMsvBean
/**
* Gets the schema.
*
* @return The schema
*/
public String getSchema()
@@ -161,7 +123,6 @@ public class JaxpMsvBean
/**
* Gets the file.
*
* @return The file
*/
public String getFile()
@@ -169,5 +130,76 @@ public class JaxpMsvBean
return file;
}
private void endMessage()
{
if ( isValid )
{
log.info(file + " verified: OK");
} else {
log.info(file + " is NOT valid!");
}
}
private void setValid(boolean valid)
{
this.isValid = valid;
}
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)
{
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)
{
log.warn( " WARNING on line " + e.getLineNumber()
+ " of file " + xmlFile.getName() + "," );
log.warn( " XPath location " + tracker.getXPath() + ":" );
log.warn( e.getMessage() );
}
}
private class ErrorHandlerImpl implements ErrorHandler
{
public void warning(SAXParseException e) throws SAXException
{
errorMessage(e, MSV_WARNING);
}
public void error(SAXParseException e) throws SAXException
{
errorMessage(e, MSV_ERROR);
setValid(false);
}
public void fatalError(SAXParseException e) throws SAXException
{
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);
}
}
}