下面列出了org.xml.sax.SAXParseException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void propagateEvent( int severity, SAXParseException saxException )
throws SAXException {
ValidationEventImpl ve =
new ValidationEventImpl( severity, saxException.getMessage(), getLocation() );
Exception e = saxException.getException();
if( e != null ) {
ve.setLinkedException( e );
} else {
ve.setLinkedException( saxException );
}
// call the client's event handler. If it returns false, then bail-out
// and terminate the unmarshal operation.
boolean result = handleEvent( ve );
if( ! result ) {
// bail-out of the parse with a SAX exception, but convert it into
// an UnmarshalException back in in the AbstractUnmarshaller
throw saxException;
}
}
/**
* Translates <code>SAXParseException</code>.
*
* @param e
* exception to translate
*/
private void translate( SAXParseException e )
{
type = DesignFileException.DESIGN_EXCEPTION_INVALID_XML;
exceptionName = e.getClass( ).getName( );
message = e.getMessage( );
description.append( " ( line = " ); //$NON-NLS-1$
description.append( e.getLineNumber( ) );
description.append( ") " ); //$NON-NLS-1$
description.append( e.getClass( ).getName( ) );
description.append( " (" ); //$NON-NLS-1$
description.append( "message : " ); //$NON-NLS-1$
description.append( e.getMessage( ) );
description.append( ")" ); //$NON-NLS-1$
lineNo = e.getLineNumber( );
if ( e.getCause( ) != null && e.getCause( ) instanceof RuntimeException )
{
translateCausedException( e.getCause( ) );
}
}
@Override
public Map read(InputStream source) throws IOException, SAXException {
logger.debug("Start to read in module.");
parser = getXMLParser();
ModuleContentHandler handler = new ModuleContentHandler();
handler.register(parser);
InputSource inpsrc = new InputSource();
if (encrypted)
inpsrc.setByteStream(new EncryptedInputStream(source));
else
inpsrc.setByteStream(source);
try {
parser.parse(inpsrc);
} catch (SAXParseException e) {
throw new SAXException(e.getLineNumber() + ":" +
e.getColumnNumber() + ": " +
e.getMessage());
}
return handler.getFunctions();
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
/** Creates an XMLParseException from a SAXParseException. */
protected static XMLParseException createXMLParseException(SAXParseException exception) {
final String fPublicId = exception.getPublicId();
final String fExpandedSystemId = exception.getSystemId();
final int fLineNumber = exception.getLineNumber();
final int fColumnNumber = exception.getColumnNumber();
XMLLocator location = new XMLLocator() {
public String getPublicId() { return fPublicId; }
public String getExpandedSystemId() { return fExpandedSystemId; }
public String getBaseSystemId() { return null; }
public String getLiteralSystemId() { return null; }
public int getColumnNumber() { return fColumnNumber; }
public int getLineNumber() { return fLineNumber; }
public int getCharacterOffset() { return -1; }
public String getEncoding() { return null; }
public String getXMLVersion() { return null; }
};
return new XMLParseException(location, exception.getMessage(),exception);
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
/** Creates an XMLParseException from a SAXParseException. */
protected static XMLParseException createXMLParseException(SAXParseException exception) {
final String fPublicId = exception.getPublicId();
final String fExpandedSystemId = exception.getSystemId();
final int fLineNumber = exception.getLineNumber();
final int fColumnNumber = exception.getColumnNumber();
XMLLocator location = new XMLLocator() {
public String getPublicId() { return fPublicId; }
public String getExpandedSystemId() { return fExpandedSystemId; }
public String getBaseSystemId() { return null; }
public String getLiteralSystemId() { return null; }
public int getColumnNumber() { return fColumnNumber; }
public int getLineNumber() { return fLineNumber; }
public int getCharacterOffset() { return -1; }
public String getEncoding() { return null; }
public String getXMLVersion() { return null; }
};
return new XMLParseException(location, exception.getMessage(),exception);
}
/** Creates an XMLParseException from a SAXParseException. */
protected static XMLParseException createXMLParseException(SAXParseException exception) {
final String fPublicId = exception.getPublicId();
final String fExpandedSystemId = exception.getSystemId();
final int fLineNumber = exception.getLineNumber();
final int fColumnNumber = exception.getColumnNumber();
XMLLocator location = new XMLLocator() {
public String getPublicId() { return fPublicId; }
public String getExpandedSystemId() { return fExpandedSystemId; }
public String getBaseSystemId() { return null; }
public String getLiteralSystemId() { return null; }
public int getColumnNumber() { return fColumnNumber; }
public int getLineNumber() { return fLineNumber; }
public int getCharacterOffset() { return -1; }
public String getEncoding() { return null; }
public String getXMLVersion() { return null; }
};
return new XMLParseException(location, exception.getMessage(),exception);
}
/**
* Throws back the exception with the name of the XML file and the position
* where the exception occurred.
*/
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw new SAXParseException("Fatal error while parsing XML at line "
+ exception.getLineNumber() + " column "
+ exception.getColumnNumber() + ": " + exception.getMessage(), null);
}
private void createSAXParseHint(GraphRewrite event, EvaluationContext context, XmlFileModel sourceFile, SAXParseException e)
{
int lineNumber = e.getLineNumber();
int column = e.getColumnNumber();
InlineHintService service = new InlineHintService(event.getGraphContext());
InlineHintModel hintModel = service.create();
hintModel.setRuleID(((Rule) context.get(Rule.class)).getId());
hintModel.setLineNumber(lineNumber);
hintModel.setColumnNumber(column);
// FIXME - Fake value as we don't get an actual length of the error from the parser
hintModel.setLength(1);
hintModel.setFile(sourceFile);
hintModel.setEffort(1);
IssueCategoryRegistry issueCategoryRegistry = IssueCategoryRegistry.instance(event.getRewriteContext());
hintModel.setIssueCategory(issueCategoryRegistry.loadFromGraph(event.getGraphContext(), IssueCategoryRegistry.POTENTIAL));
if (e.getCause() instanceof InvalidXSDURLException)
{
String xsdUrl = ((InvalidXSDURLException) e.getCause()).getUrl();
hintModel.setTitle(XmlFileModel.XSD_URL_NOT_VALID);
hintModel.setHint(xsdUrl + " is not a valid url.");
}
else
{
hintModel.setTitle(XmlFileModel.NOT_VALID_XML);
String message = "XSD Validation failed due to:\n\n";
message += "\t" + e.getMessage();
message += System.lineSeparator()+System.lineSeparator();
hintModel.setHint(message);
}
sourceFile.setGenerateSourceReport(true);
}
/**
* {@inheritDoc}
*/
public void warning(SAXParseException exception) throws SAXException {
if (log.isWarnEnabled()) {
String message = null;
message = "Warning on line " + exception.getLineNumber() + ": "
+ exception.getMessage();
log.warn(message);
}
}
public void testValidation() throws Exception {
Game game = ServerTestHelper.startServerGame(getTestMap(true));
Colony colony = getStandardColony(6);
Player player = game.getPlayerByNationId("model.nation.dutch");
ServerTestHelper.newTurn();
ServerTestHelper.newTurn();
String serialized = null;
try {
Validator validator = buildValidator("schema/data/data-game.xsd");
serialized = game.serialize();
validator.validate(new StreamSource(new StringReader(serialized)));
} catch (SAXParseException e) {
int col = e.getColumnNumber();
String errMsg = e.getMessage()
+ "\nAt line=" + e.getLineNumber()
+ ", column=" + col + ":\n";
if (serialized != null) {
errMsg += serialized.substring(Math.max(0, col - 100),
Math.min(col + 100, serialized.length()));
}
fail(errMsg);
}
ServerTestHelper.stopServerGame();
}
/**
* Throws back the exception with the name of the XML file and the position
* where the exception occurred.
*/
public void fatalError(SAXParseException exception) throws SAXException {
throw new SAXParseException("Fatal error while parsing XML at line "
+ exception.getLineNumber() + " column " + exception.getColumnNumber()
+ ": " + exception.getMessage(), null);
}
/**
* {@inheritDoc}
*/
public void warning(SAXParseException exception) throws SAXException {
if (log.isWarnEnabled()) {
String message = null;
message = "Warning on line " + exception.getLineNumber() + ": "
+ exception.getMessage();
log.warn(message);
}
}
private void logParseFailure(SAXParseException e, String serialized) {
int col = e.getColumnNumber();
String errMsg = e.getMessage()
+ "\nAt line=" + e.getLineNumber()
+ ", column=" + col + ":\n"
+ serialized.substring(Math.max(0, col - 100),
Math.min(col + 100, serialized.length()));
fail(errMsg);
}
public void parseFile(String filePath) {
initPart();
fileName = new File(filePath).getName();
initSingletonId(filePath);
String myerror = "";
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
InputSource inp = new InputSource(new FileReader(filePath));
parser.parse(inp, this);
/*
System.out.println("eventTermArrayList.size(); = " + eventTermArrayList.size());
System.out.println("timeLinks = " + timeLinks.size());
System.out.println("locationLinks = " + locationLinks.size());
System.out.println("plotLinks = " + plotLinks.size());
*/
} catch (SAXParseException err) {
myerror = "\n** Parsing error" + ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId();
myerror += "\n" + err.getMessage();
System.out.println("myerror = " + myerror);
} catch (SAXException e) {
Exception x = e;
if (e.getException() != null)
x = e.getException();
myerror += "\nSAXException --" + x.getMessage();
System.out.println("myerror = " + myerror);
} catch (Exception eee) {
eee.printStackTrace();
myerror += "\nException --" + eee.getMessage();
System.out.println("myerror = " + myerror);
}
//System.out.println("myerror = " + myerror);
}
private void print(@NotNull SAXParseException x, @NotNull ValidationMessageSeverity severity) {
ValidationMessage message = new ValidationMessage(severity, x.getMessage(), Integer.valueOf(x.getLineNumber()),
Integer.valueOf(x.getColumnNumber()), null);
messages.add(message);
}
private String toString(SAXParseException exception) {
return exception.getMessage();
}
public void error(SAXParseException ex) {
throw new RuntimeException("Error compiling schema from WADL : "
+ ex.getMessage(), ex);
}
public void error(SAXParseException e) throws SAXException {
throw new SAXException("Line: " + e.getLineNumber() + " message: " + e.getMessage());
}