use of org.xml.sax.SAXParseException in project jena by apache.
the class TestErrorMsg method check.
/**
* @param filename
* Read this file
* @param regex
* Error msg must match this.
*
private void check(String filename, String regex)
throws IOException, MalformedPatternException, SAXException {
check(filename, regex, null);
}
*/
private void check(String filename, String regexPresent, String regexAbsent) throws IOException {
final StringBuffer buf = new StringBuffer();
ARP arp = new ARP();
arp.getHandlers().setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
buf.append(exception.getMessage());
buf.append("\n");
}
@Override
public void error(SAXParseException e) {
warning(e);
}
@Override
public void fatalError(SAXParseException e) {
warning(e);
}
});
try (InputStream in = new FileInputStream("testing/arp/error-msgs/" + filename + ".rdf")) {
arp.load(in, "file:///" + filename);
} catch (SAXException e) {
}
String contents = buf.toString();
if (regexPresent != null)
assertTrue("Should find /" + regexPresent + "/", Pattern.compile(regexPresent, Pattern.DOTALL).matcher(contents).find());
if (regexAbsent != null)
assertTrue("Should not find /" + regexAbsent + "/", !Pattern.compile(regexAbsent, Pattern.DOTALL).matcher(contents).find());
contents = null;
}
use of org.xml.sax.SAXParseException in project jena by apache.
the class MoreTests method testInterrupt.
public void testInterrupt() throws SAXException, IOException {
ARP a = new ARP();
try (InputStream in = new FileInputStream("testing/wg/miscellaneous/consistent001.rdf")) {
a.getHandlers().setStatementHandler(new StatementHandler() {
int countDown = 10;
@Override
public void statement(AResource subj, AResource pred, AResource obj) {
if (countDown-- == 0)
Thread.currentThread().interrupt();
}
@Override
public void statement(AResource subj, AResource pred, ALiteral lit) {
}
});
a.getHandlers().setErrorHandler(new ErrorHandler() {
@Override
public void error(SAXParseException exception) throws SAXException {
throw new RuntimeException("Unexpected error", exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
throw new RuntimeException("Unexpected warning", exception);
}
});
try {
a.load(in);
fail("Thread was not interrupted.");
} catch (InterruptedIOException | SAXParseException e) {
}
}
// System.err.println("Finished "+Thread.interrupted());
}
use of org.xml.sax.SAXParseException in project jmeter by apache.
the class XMLSchemaAssertion method setSchemaResult.
/**
* set Schema result
*
* @param result
* @param xmlStr
* @param xsdFileName
*/
private void setSchemaResult(AssertionResult result, String xmlStr, String xsdFileName) {
try {
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setValidating(true);
parserFactory.setNamespaceAware(true);
parserFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
parserFactory.setAttribute(JAXP_SCHEMA_SOURCE, xsdFileName);
// create a parser:
DocumentBuilder parser = parserFactory.newDocumentBuilder();
parser.setErrorHandler(new SAXErrorHandler(result));
parser.parse(new InputSource(new StringReader(xmlStr)));
// if everything went fine then xml schema validation is valid
} catch (SAXParseException e) {
// Only set message if error not yet flagged
if (!result.isError() && !result.isFailure()) {
result.setError(true);
result.setFailureMessage(errorDetails(e));
}
} catch (SAXException e) {
if (log.isWarnEnabled()) {
log.warn(e.toString());
}
result.setResultForFailure(e.getMessage());
} catch (IOException e) {
log.warn("IO error", e);
result.setResultForFailure(e.getMessage());
} catch (ParserConfigurationException e) {
log.warn("Problem with Parser Config", e);
result.setResultForFailure(e.getMessage());
}
}
use of org.xml.sax.SAXParseException in project jena by apache.
the class XMLHandler method generalError.
void generalError(int id, Exception e) throws SAXParseException {
ARPLocation where = new ARPLocation(locator);
// System.err.println(e.getMessage());
warning(null, id, new ParseException(id, where, e));
}
use of org.xml.sax.SAXParseException in project jena by apache.
the class ParseException method formatMessage.
/**
* Calls e.getMessage() and also accesses line and column information for
* SAXParseException's.
*
* @return e.getMessage() possibly prepended by error location information.
* @param e
* The exception to describe.
*/
public static String formatMessage(Exception e) {
String msg = e.getMessage();
if (msg == null)
msg = e.toString();
if (!(e instanceof SAXParseException))
return msg;
SAXParseException sax = (SAXParseException) e;
String file = sax.getSystemId();
if (file == null)
file = sax.getPublicId();
String rslt = file == null ? "" : file;
if (sax.getLineNumber() == -1)
return (file != null ? (file + ": ") : "") + msg;
if (sax.getColumnNumber() == -1) {
return rslt + "(line " + sax.getLineNumber() + "): " + msg;
}
return rslt + "(line " + sax.getLineNumber() + " column " + sax.getColumnNumber() + "): " + msg;
}
Aggregations