Search in sources :

Example 56 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class TurtleWriter method startRDF.

@Override
public void startRDF() throws RDFHandlerException {
    if (writingStarted) {
        throw new RuntimeException("Document writing has already started");
    }
    writingStarted = true;
    try {
        xsdStringToPlainLiteral = getWriterConfig().get(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL);
        prettyPrint = getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT);
        inlineBNodes = getWriterConfig().get(BasicWriterSettings.INLINE_BLANK_NODES);
        if (prettyPrint) {
            writer.setIndentationString("  ");
        } else {
            writer.setIndentationString("");
        }
        if (baseIRI != null && getWriterConfig().get(BasicWriterSettings.BASE_DIRECTIVE)) {
            writeBase(baseIRI.toString());
        }
        // Write namespace declarations
        for (Map.Entry<String, String> entry : namespaceTable.entrySet()) {
            String name = entry.getKey();
            String prefix = entry.getValue();
            writeNamespace(prefix, name);
        }
        if (!namespaceTable.isEmpty()) {
            writer.writeEOL();
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 57 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class NQuadsWriter method handleStatement.

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
        throw new RuntimeException("Document writing has not yet been started");
    }
    try {
        // SUBJECT
        writeValue(st.getSubject());
        writer.write(" ");
        // PREDICATE
        writeValue(st.getPredicate());
        writer.write(" ");
        // OBJECT
        writeValue(st.getObject());
        if (null != st.getContext()) {
            writer.write(" ");
            writeValue(st.getContext());
        }
        writer.write(" .\n");
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 58 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project timbuctoo by HuygensING.

the class Rdf4jRdfParser method importRdf.

@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException {
    try {
        RDFFormat format = Rio.getParserFormatForMIMEType(input.getMimeType().toString()).orElseThrow(() -> new UnsupportedRDFormatException(input.getMimeType() + " is not a supported rdf type."));
        RDFParser rdfParser = Rio.createParser(format);
        rdfParser.setPreserveBNodeIDs(true);
        rdfParser.setRDFHandler(new TimRdfHandler(rdfProcessor, defaultGraph, input.getFile().getName()));
        rdfParser.parse(input.getReader(), baseUri);
    } catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
        throw new RdfProcessingFailedException(e);
    } catch (RDFHandlerException e) {
        if (e.getCause() instanceof RdfProcessingFailedException) {
            throw (RdfProcessingFailedException) e.getCause();
        } else {
            throw new RdfProcessingFailedException(e);
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TimRdfHandler(nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.rdf4j.parsers.TimRdfHandler) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 59 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project timbuctoo by HuygensING.

the class TimRdfHandler method handleStatement.

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
    try {
        if (Thread.currentThread().isInterrupted()) {
            rdfProcessor.commit();
            throw new RDFHandlerException("Interrupted");
        }
        String graph = st.getContext() == null ? defaultGraph : st.getContext().stringValue();
        rdfProcessor.onQuad(isAssertion(), handleNode(st.getSubject()), st.getPredicate().stringValue(), handleNode(st.getObject()), (st.getObject() instanceof Literal) ? ((Literal) st.getObject()).getDatatype().toString() : null, (st.getObject() instanceof Literal) ? ((Literal) st.getObject()).getLanguage().orElse(null) : null, graph);
    } catch (RdfProcessingFailedException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Literal(org.eclipse.rdf4j.model.Literal) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)

Aggregations

RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)59 IOException (java.io.IOException)41 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)19 Resource (org.eclipse.rdf4j.model.Resource)11 Value (org.eclipse.rdf4j.model.Value)10 RDFLoader (org.eclipse.rdf4j.repository.util.RDFLoader)8 BNode (org.eclipse.rdf4j.model.BNode)7 IRI (org.eclipse.rdf4j.model.IRI)7 Literal (org.eclipse.rdf4j.model.Literal)7 Statement (org.eclipse.rdf4j.model.Statement)6 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 RDFInserter (org.eclipse.rdf4j.repository.util.RDFInserter)4 SAXException (org.xml.sax.SAXException)4 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2