// We don't want the default handler which prints errors to stderr.
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) {
log.error(Severity.WARNING,
new FileAndLine(xmlFile.getAbsolutePath(), 0),
"Warning when parsing: %1$s",
e.toString());
}
@Override
public void fatalError(SAXParseException e) {
log.error(Severity.ERROR,
new FileAndLine(xmlFile.getAbsolutePath(), 0),
"Fatal error when parsing: %1$s",
xmlFile.getName(), e.toString());
}
@Override
public void error(SAXParseException e) {
log.error(Severity.ERROR,
new FileAndLine(xmlFile.getAbsolutePath(), 0),
"Error when parsing: %1$s",
e.toString());
}
});
Document doc = builder.parse(is);
doc.setUserData(DATA_ORIGIN_FILE, xmlFile, null /*handler*/);
findLineNumbers(doc, 1);
if (merger.isInsertSourceMarkers()) {
setSource(doc, xmlFile);
}
return doc;
} catch (FileNotFoundException e) {
log.error(Severity.ERROR,
new FileAndLine(xmlFile.getAbsolutePath(), 0),
"XML file not found");
} catch (Exception e) {
log.error(Severity.ERROR,
new FileAndLine(xmlFile.getAbsolutePath(), 0),
"Failed to parse XML file: %1$s",
e.toString());
}
return null;