Search in sources :

Example 1 with RDFParser

use of org.openrdf.rio.RDFParser 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 RDFParser

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

the class SparqlRepositorySailGraph method ignoreDatatypesInAllParsers.

// wrap RDF parser factories such that they ignore invalid values in data-typed literals
// (e.g. a value of "fish" for an xsd:integer literal,
// or a value of 1995-01-01T00:00:00+02:00 for an xsd:gYear literal).
// The default behavior is to throw an exception when bad literals are encountered,
// resulting in failure.
private static void ignoreDatatypesInAllParsers() {
    RDFParserRegistry r = RDFParserRegistry.getInstance();
    Collection<RDFParserFactory> oldFactories = new LinkedList<RDFParserFactory>();
    Collection<RDFParserFactory> newFactories = new LinkedList<RDFParserFactory>();
    for (final RDFFormat f : r.getKeys()) {
        final RDFParserFactory pf = r.get(f);
        pf.getParser().setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
        RDFParserFactory pfn = new RDFParserFactory() {

            public RDFFormat getRDFFormat() {
                return f;
            }

            public RDFParser getParser() {
                RDFParser p = pf.getParser();
                p.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
                return p;
            }
        };
        oldFactories.add(pf);
        newFactories.add(pfn);
    }
    for (RDFParserFactory pf : oldFactories) {
        r.remove(pf);
    }
    for (RDFParserFactory pfn : newFactories) {
        r.add(pfn);
    }
}
Also used : RDFParserFactory(org.openrdf.rio.RDFParserFactory) RDFParser(org.openrdf.rio.RDFParser) RDFParserRegistry(org.openrdf.rio.RDFParserRegistry) LinkedList(java.util.LinkedList) RDFFormat(org.openrdf.rio.RDFFormat)

Example 3 with RDFParser

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

the class SailLoader method loadFile.

private long loadFile(final File fileOrDirectory, final SailConnection c) throws IOException {
    if (fileOrDirectory.isDirectory()) {
        long count = 0;
        for (File child : fileOrDirectory.listFiles()) {
            count += loadFile(child, c);
        }
        return count;
    } else {
        RDFFormat format;
        long startTime = System.currentTimeMillis();
        if (verbose) {
            LOGGER.info("loading file: " + fileOrDirectory);
        }
        String n = fileOrDirectory.getName();
        InputStream is;
        if (n.endsWith(".gz")) {
            String n0 = n.substring(0, n.lastIndexOf("."));
            format = RDFFormat.forFileName(n0);
            is = new GZIPInputStream(new FileInputStream(fileOrDirectory));
        } else {
            format = RDFFormat.forFileName(n);
            is = new FileInputStream(fileOrDirectory);
        }
        try {
            if (null == format) {
                LOGGER.warning("could not guess format of file: " + n);
                return 0;
            }
            RDFParser p = Rio.createParser(format);
            p.setStopAtFirstError(false);
            SailConnectionAdder adder = new SailConnectionAdder(c);
            p.setRDFHandler(adder);
            try {
                p.parse(is, baseUri);
            } catch (Throwable t) {
                // Attempt to recover.
                t.printStackTrace(System.err);
            } finally {
                is.close();
            }
            long endTime = System.currentTimeMillis();
            if (verbose) {
                LOGGER.info("\tfinished in " + (endTime - startTime) + "ms");
            }
            return adder.count;
        } finally {
            is.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RDFParser(org.openrdf.rio.RDFParser) File(java.io.File) FileInputStream(java.io.FileInputStream) RDFFormat(org.openrdf.rio.RDFFormat)

Example 4 with RDFParser

use of org.openrdf.rio.RDFParser 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

RDFParser (org.openrdf.rio.RDFParser)4 RDFFormat (org.openrdf.rio.RDFFormat)2 SailConnection (org.openrdf.sail.SailConnection)2 BabelReader (edu.mit.simile.babel.BabelReader)1 ExhibitJsonReader (edu.mit.simile.babel.exhibit.ExhibitJsonReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Repository (org.openrdf.repository.Repository)1 RepositoryException (org.openrdf.repository.RepositoryException)1 SailRepository (org.openrdf.repository.sail.SailRepository)1 RDFHandler (org.openrdf.rio.RDFHandler)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1 RDFParserFactory (org.openrdf.rio.RDFParserFactory)1