Search in sources :

Example 46 with SailConnection

use of org.openrdf.sail.SailConnection in project backstage by zepheira.

the class DataLoadingUtilities method loadDataFromStream.

public static void loadDataFromStream(InputStream stream, String sourceURL, String lang, Sail sail) throws Exception {
    RepoSailTuple rs = createMemoryRepository(null);
    Repository r = rs.repository;
    lang = lang.toLowerCase();
    if ("exhibit/json".equals(lang)) {
        Properties properties = new Properties();
        BabelReader reader = new ExhibitJsonReader();
        try {
            if (reader.takesReader()) {
                InputStreamReader isr = new InputStreamReader(stream);
                reader.read(isr, sail, properties, Locale.getDefault());
            } else {
                reader.read(stream, sail, properties, Locale.getDefault());
            }
        } finally {
            stream.close();
        }
    } else {
        RDFParser parser = null;
        if ("rdfxml".equals(lang)) {
            parser = new RDFXMLParser(r.getValueFactory());
        } else if ("n3".equals(lang) || "turtle".equals(lang)) {
            parser = new TurtleParser(r.getValueFactory());
        } else if ("ntriples".equals(lang)) {
            parser = new NTriplesParser(r.getValueFactory());
        }
        try {
            SailConnection c = null;
            try {
                c = sail.getConnection();
                BNodeConverterStatementHandler handler = new BNodeConverterStatementHandler(c);
                parser.setRDFHandler(handler);
                parser.setParseErrorListener(new LoggingParseErrorListener(sourceURL));
                parser.setVerifyData(false);
                parser.setStopAtFirstError(false);
                parser.parse(stream, sourceURL);
                c.commit();
                _logger.info("Read " + handler.m_count + " statements from '" + sourceURL + "'");
            } catch (RepositoryException e) {
                if (c != null)
                    c.rollback();
            } finally {
                if (c != null)
                    c.close();
            }
        } catch (Exception e) {
            throw new ModelReadFromFileException("Failed to read data from '" + sourceURL + "'", e);
        } finally {
            stream.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RDFXMLParser(org.openrdf.rio.rdfxml.RDFXMLParser) NTriplesParser(org.openrdf.rio.ntriples.NTriplesParser) RepositoryException(org.openrdf.repository.RepositoryException) Properties(java.util.Properties) RDFParser(org.openrdf.rio.RDFParser) SailException(org.openrdf.sail.SailException) RepositoryException(org.openrdf.repository.RepositoryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TurtleParser(org.openrdf.rio.turtle.TurtleParser) SailRepository(org.openrdf.repository.sail.SailRepository) Repository(org.openrdf.repository.Repository) BabelReader(edu.mit.simile.babel.BabelReader) SailConnection(org.openrdf.sail.SailConnection) ExhibitJsonReader(edu.mit.simile.babel.exhibit.ExhibitJsonReader)

Aggregations

SailConnection (org.openrdf.sail.SailConnection)46 Test (org.junit.Test)30 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)28 URI (org.openrdf.model.URI)25 SailException (org.openrdf.sail.SailException)12 Literal (org.openrdf.model.Literal)5 Resource (org.openrdf.model.Resource)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 URIImpl (org.openrdf.model.impl.URIImpl)4 Vertex (com.tinkerpop.blueprints.Vertex)3 Statement (org.openrdf.model.Statement)3 ValueFactory (org.openrdf.model.ValueFactory)3 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)3 ParsedQuery (org.openrdf.query.parser.ParsedQuery)3 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)2 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)2 LiteralImpl (org.openrdf.model.impl.LiteralImpl)2 BindingSet (org.openrdf.query.BindingSet)2