Search in sources :

Example 1 with RDFHandler

use of org.openrdf.rio.RDFHandler in project blueprints by tinkerpop.

the class SailTest method addFile.

protected void addFile(final InputStream in, final RDFFormat format) throws Exception {
    try {
        SailConnection sc = sail.getConnection();
        sc.begin();
        try {
            RDFHandler h = new SailAdder(sc);
            RDFParser p = Rio.createParser(format);
            p.setRDFHandler(h);
            p.parse(in, "http://example.org/bogusBaseURI/");
            sc.commit();
        } finally {
            sc.rollback();
            sc.close();
        }
    } finally {
        in.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) RDFParser(org.openrdf.rio.RDFParser) RDFHandler(org.openrdf.rio.RDFHandler)

Example 2 with RDFHandler

use of org.openrdf.rio.RDFHandler in project blueprints by tinkerpop.

the class SailGraph method loadRDF.

/**
     * Load RDF data into the SailGraph. Supported formats include rdf-xml, n-triples, turtle, n3, trix, or trig.
     * Before loading data, the current transaction is successfully committed.
     *
     * @param input       The InputStream of RDF data.
     * @param baseURI     The baseURI for RDF data.
     * @param rdfParser   The {@link RDFParser} to use. It's {@link RDFHandler} will be
     *                    changed to an internal one. The main purpose of this is to use
     *                    a custom {@link ValueFactory}. It is recommended to use
     *                    <code>Rio.createParser(getFormat(format))</code> and set the
     *                    {@link ValueFactory} of the parser to something that
     *                    <code>extends</code> the {@link ValueFactory} of the
     *                    {@link Sail} used to initialize this class.
     *                    <p>
     *                    For example, <code>extend {@link ValueFactoryImpl}</code> if
     *                    you used a <code>GraphSail</code> to initialize this class.
     * @param baseGraph   The baseGraph to insert the data into.  May be null, in which case data is added to the default graph.
     * @param rdfHandlers Any number of {@link RDFHandler}s into which to pass parsed RDF statements <b>after</b>
     *                    they have been added to the graph.
     *                    These may be used, for example, for implementing your own logging.
     *                    Can be <code>null</code> if you only want to use the default
     *                    {@link RDFHandler} created internally.
     */
public void loadRDF(final InputStream input, final String baseURI, final RDFParser rdfParser, final String baseGraph, final RDFHandler... rdfHandlers) {
    try {
        this.commit();
        final SailConnection c = this.rawGraph.getConnection();
        try {
            c.begin();
            RDFHandler h = null == baseGraph ? new SailAdder(c) : new SailAdder(c, new URIImpl(baseGraph));
            if (rdfHandlers != null) {
                RDFHandler[] handlers = new RDFHandler[rdfHandlers.length + 1];
                handlers[0] = h;
                System.arraycopy(rdfHandlers, 0, handlers, 1, rdfHandlers.length);
                h = new RDFHandlerWrapper(handlers);
            }
            rdfParser.setRDFHandler(h);
            rdfParser.parse(input, baseURI);
            c.commit();
        } finally {
            c.rollback();
            c.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) RDFHandlerWrapper(org.openrdf.rio.helpers.RDFHandlerWrapper) URIImpl(org.openrdf.model.impl.URIImpl) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) RDFHandler(org.openrdf.rio.RDFHandler)

Aggregations

RDFHandler (org.openrdf.rio.RDFHandler)2 SailConnection (org.openrdf.sail.SailConnection)2 URIImpl (org.openrdf.model.impl.URIImpl)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1 RDFParser (org.openrdf.rio.RDFParser)1 RDFHandlerWrapper (org.openrdf.rio.helpers.RDFHandlerWrapper)1 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)1 SailException (org.openrdf.sail.SailException)1