use of org.eclipse.rdf4j.rio.helpers.ParseErrorLogger in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDF.
/**
* Parse the response in this thread using the provided {@link RDFHandler}. All HTTP connections are
* closed and released in this method
*/
protected void getRDF(HttpUriRequest method, RDFHandler handler, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
String mimeType = getResponseMIMEType(response);
try {
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setParserConfig(getParserConfig());
parser.setParseErrorListener(new ParseErrorLogger());
parser.setRDFHandler(handler);
parser.parse(response.getEntity().getContent(), method.getURI().toASCIIString());
} catch (RDFParseException e) {
throw new RepositoryException("Malformed query result from server", e);
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
}
use of org.eclipse.rdf4j.rio.helpers.ParseErrorLogger in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDFBackground.
/**
* Parse the response in a background thread. HTTP connections are dealt with in the
* {@link BackgroundGraphResult} or (in the error-case) in this method.
*/
protected GraphQueryResult getRDFBackground(HttpUriRequest method, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
boolean submitted = false;
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
GraphQueryResult gRes = null;
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setParserConfig(getParserConfig());
parser.setParseErrorListener(new ParseErrorLogger());
Charset charset = null;
// SES-1793 : Do not attempt to check for a charset if the format is
// defined not to have a charset
// This prevents errors caused by people erroneously attaching a
// charset to a binary formatted document
HttpEntity entity = response.getEntity();
if (format.hasCharset() && entity != null && entity.getContentType() != null) {
// required?
try {
charset = ContentType.parse(entity.getContentType().getValue()).getCharset();
} catch (IllegalCharsetNameException e) {
// work around for Joseki-3.2
// Content-Type: application/rdf+xml;
// charset=application/rdf+xml
}
if (charset == null) {
charset = UTF8;
}
}
if (entity == null) {
throw new RepositoryException("Server response was empty.");
}
String baseURI = method.getURI().toASCIIString();
gRes = background.parse(parser, entity.getContent(), charset, baseURI);
submitted = true;
return gRes;
} finally {
if (!submitted) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.rio.helpers.ParseErrorLogger in project rdf4j by eclipse.
the class CustomTurtleParserTest method test780IRISpace.
@Test
public void test780IRISpace() throws Exception {
String ttl = "_:b25978837 a <http://purl.bioontology.org/ontology/UATC/\\u0020SERINE\\u0020\\u0020> .";
try {
Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE);
fail();
} catch (RDFParseException e) {
// Invalid IRI
}
Model model = Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE, new ParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false), SimpleValueFactory.getInstance(), new ParseErrorLogger());
assertEquals(1, model.size());
model.filter(null, RDF.TYPE, null).objects().forEach(obj -> assertEquals("http://purl.bioontology.org/ontology/UATC/%20SERINE%20%20", obj.stringValue()));
}
use of org.eclipse.rdf4j.rio.helpers.ParseErrorLogger in project rdf4j by eclipse.
the class CustomTurtleParserTest method testParsingNamespacesWithOption.
@Test
public void testParsingNamespacesWithOption() throws Exception {
ParserConfig aConfig = new ParserConfig();
aConfig.set(BasicParserSettings.NAMESPACES, Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));
Model model = Rio.parse(new StringReader("<urn:a> foo:broader <urn:b>."), "", RDFFormat.TURTLE, aConfig, vf, new ParseErrorLogger());
assertEquals(1, model.size());
assertTrue(model.contains(vf.createURI("urn:a"), SKOS.BROADER, vf.createURI("urn:b")));
}
use of org.eclipse.rdf4j.rio.helpers.ParseErrorLogger in project rdf4j by eclipse.
the class CustomTurtleParserTest method testParsingNamespacesWithOverride.
@Test
public void testParsingNamespacesWithOverride() throws Exception {
ParserConfig aConfig = new ParserConfig();
aConfig.set(BasicParserSettings.NAMESPACES, Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));
Model model = Rio.parse(new StringReader("@prefix skos : <urn:not_skos:> ." + "<urn:a> skos:broader <urn:b>."), "", RDFFormat.TURTLE, aConfig, vf, new ParseErrorLogger());
assertEquals(1, model.size());
assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:not_skos:broader"), vf.createIRI("urn:b")));
}
Aggregations