Search in sources :

Example 1 with ParseErrorLogger

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());
    }
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 2 with ParseErrorLogger

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());
        }
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Charset(java.nio.charset.Charset) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 3 with ParseErrorLogger

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()));
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) StringReader(java.io.StringReader) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 4 with ParseErrorLogger

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")));
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) StringReader(java.io.StringReader) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) NamespaceImpl(org.eclipse.rdf4j.model.impl.NamespaceImpl) Test(org.junit.Test)

Example 5 with ParseErrorLogger

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")));
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) StringReader(java.io.StringReader) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) NamespaceImpl(org.eclipse.rdf4j.model.impl.NamespaceImpl) Test(org.junit.Test)

Aggregations

ParseErrorLogger (org.eclipse.rdf4j.rio.helpers.ParseErrorLogger)6 StringReader (java.io.StringReader)3 Model (org.eclipse.rdf4j.model.Model)3 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)3 ParserConfig (org.eclipse.rdf4j.rio.ParserConfig)3 RDFParser (org.eclipse.rdf4j.rio.RDFParser)3 Test (org.junit.Test)3 HttpResponse (org.apache.http.HttpResponse)2 NamespaceImpl (org.eclipse.rdf4j.model.impl.NamespaceImpl)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1