Search in sources :

Example 1 with RDFParser

use of org.apache.jena.riot.RDFParser in project jena by apache.

the class UpdateEngineWorker method visit.

@Override
public void visit(UpdateLoad update) {
    // LOAD SILENT? iri ( INTO GraphRef )?
    String source = update.getSource();
    Node dest = update.getDest();
    Graph graph = graph(datasetGraph, dest);
    // We must load buffered if silent so that the dataset graph sees
    // all or no triples/quads when there is a parse error
    // (no nested transaction abort).
    boolean loadBuffered = update.getSilent() || !datasetGraph.supportsTransactionAbort();
    try {
        if (dest == null) {
            // Quads accepted (extension).
            if (loadBuffered) {
                DatasetGraph dsg2 = DatasetGraphFactory.create();
                RDFDataMgr.read(dsg2, source);
                dsg2.find().forEachRemaining(datasetGraph::add);
            } else {
                RDFDataMgr.read(datasetGraph, source);
            }
            return;
        }
        // LOAD SILENT? iri INTO GraphRef
        // Load triples. To give a decent error message and also not have the usual
        // parser behaviour of just selecting default graph triples when the
        // destination is a graph, we need to do the same steps as RDFParser.parseURI,
        // with different checking.
        TypedInputStream input = RDFDataMgr.open(source);
        String contentType = input.getContentType();
        Lang lang = RDFDataMgr.determineLang(source, contentType, Lang.TTL);
        if (lang == null)
            throw new UpdateException("Failed to determine the syntax for '" + source + "'");
        if (!RDFLanguages.isTriples(lang))
            throw new UpdateException("Attempt to load quads into a graph");
        RDFParser parser = RDFParser.source(input.getInputStream()).forceLang(lang).build();
        if (loadBuffered) {
            Graph g = GraphFactory.createGraphMem();
            parser.parse(g);
            GraphUtil.addInto(graph, g);
        } else {
            parser.parse(graph);
        }
    } catch (RuntimeException ex) {
        if (!update.getSilent()) {
            if (ex instanceof UpdateException)
                throw ex;
            throw new UpdateException("Failed to LOAD '" + source + "' :: " + ex.getMessage(), ex);
        }
    }
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) TemplateLib.remapDefaultGraph(org.apache.jena.sparql.modify.TemplateLib.remapDefaultGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) Lang(org.apache.jena.riot.Lang) UpdateException(org.apache.jena.update.UpdateException) RDFParser(org.apache.jena.riot.RDFParser) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 2 with RDFParser

use of org.apache.jena.riot.RDFParser in project jena by apache.

the class tdbloader method loadQuadsStdin.

private void loadQuadsStdin() {
    DataLoader loader = chooseLoader(super.getDatasetGraph(), graphName);
    StreamRDF dest = loader.stream();
    if (lang == null)
        lang = Lang.NQUADS;
    RDFParser parser = RDFParser.create().lang(lang).source(System.in).build();
    long elapsed = Timer.time(() -> {
        loader.startBulk();
        parser.parse(dest);
        loader.finishBulk();
    });
// return elapsed;
}
Also used : DataLoader(org.apache.jena.tdb2.loader.DataLoader) StreamRDF(org.apache.jena.riot.system.StreamRDF) RDFParser(org.apache.jena.riot.RDFParser)

Aggregations

RDFParser (org.apache.jena.riot.RDFParser)2 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)1 Graph (org.apache.jena.graph.Graph)1 Node (org.apache.jena.graph.Node)1 Lang (org.apache.jena.riot.Lang)1 StreamRDF (org.apache.jena.riot.system.StreamRDF)1 TemplateLib.remapDefaultGraph (org.apache.jena.sparql.modify.TemplateLib.remapDefaultGraph)1 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)1 DataLoader (org.apache.jena.tdb2.loader.DataLoader)1 UpdateException (org.apache.jena.update.UpdateException)1