Search in sources :

Example 11 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class WriterDatasetThrift method write.

@Override
public void write(OutputStream out, DatasetGraph dsg, PrefixMap prefixMap, String baseURI, Context context) {
    StreamRDF stream = BinRDF.streamToOutputStream(out, withValues);
    stream.start();
    StreamOps.sendDatasetToStream(dsg, stream, prefixMap);
    stream.finish();
}
Also used : StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 12 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class DataValidatorJSON method execute.

public static JsonObject execute(ValidationAction action) {
    JsonBuilder obj = new JsonBuilder();
    obj.startObject();
    String syntax = getArgOrNull(action, paramSyntax);
    if (syntax == null || syntax.equals(""))
        syntax = RDFLanguages.NQUADS.getName();
    Lang language = RDFLanguages.shortnameToLang(syntax);
    if (language == null) {
        ServletOps.errorBadRequest("Unknown syntax: " + syntax);
        return null;
    }
    String string = getArg(action, paramData);
    StringReader sr = new StringReader(string);
    obj.key(jInput).value(string);
    StreamRDF dest = StreamRDFLib.sinkNull();
    try {
        RDFParser.create().source(sr).lang(language).parse(dest);
    } catch (RiotParseException ex) {
        obj.key(jErrors);
        // Errors array
        obj.startArray();
        obj.startObject();
        obj.key(jParseError).value(ex.getMessage());
        obj.key(jParseErrorLine).value(ex.getLine());
        obj.key(jParseErrorCol).value(ex.getCol());
        obj.finishObject();
        obj.finishArray();
        // Outer object
        obj.finishObject();
        return obj.build().getAsObject();
    } catch (RiotException ex) {
        obj.key(jErrors);
        // Errors array
        obj.startArray();
        obj.startObject();
        obj.key(jParseError).value(ex.getMessage());
        obj.finishObject();
        obj.finishArray();
        // Outer object
        obj.finishObject();
        return obj.build().getAsObject();
    }
    obj.finishObject();
    return obj.build().getAsObject();
}
Also used : JsonBuilder(org.apache.jena.atlas.json.JsonBuilder) StreamRDF(org.apache.jena.riot.system.StreamRDF) StringReader(java.io.StringReader)

Example 13 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class TestDatasetOps method gsp_x_ct.

private void gsp_x_ct(String urlDataset, String acceptheader, String contentTypeResponse) {
    HttpEntity e = datasetToHttpEntity(data);
    HttpOp.execHttpPut(urlDataset(), e);
    // Do manually so the test can validate the expected ContentType
    try (TypedInputStream in = HttpOp.execHttpGet(urlDataset, acceptheader)) {
        assertEqualsIgnoreCase(contentTypeResponse, in.getContentType());
        Lang lang = RDFLanguages.contentTypeToLang(in.getContentType());
        DatasetGraph dsg = DatasetGraphFactory.create();
        StreamRDF dest = StreamRDFLib.dataset(dsg);
        RDFParser.source(in).lang(lang).parse(dest);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) StreamRDF(org.apache.jena.riot.system.StreamRDF) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 14 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class TestStreamRDFThrift method dataset_02.

@Test
public void dataset_02() {
    DatasetGraph dsg1 = datasetGraph;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamRDFWriter.write(out, dsg1, Lang.RDFTHRIFT);
    byte[] bytes = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    DatasetGraph dsg2 = DatasetGraphFactory.create();
    StreamRDF stream2 = StreamRDFLib.dataset(dsg2);
    BinRDF.inputStreamToStream(in, stream2);
    boolean b = IsoMatcher.isomorphic(dsg1, dsg2);
    assertTrue(b);
    // Stronger - same bNode and same as in original data.
    Node obj = Iter.first(dsg1.listGraphNodes(), Node::isBlank);
    termAsObject(dsg1, obj);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) Node(org.apache.jena.graph.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 15 with StreamRDF

use of org.apache.jena.riot.system.StreamRDF in project jena by apache.

the class SPARQL_Upload method uploadWorker.

/** Process an HTTP file upload of RDF with additiona name field for the graph name.
     *  We can't stream straight into a dataset because the graph name can be after the data.
     *  @return graph name and count
     */
// ?? Combine with Upload.fileUploadWorker
// Difference is the handling of names for graphs.
private static UploadDetails uploadWorker(HttpAction action, String base) {
    DatasetGraph dsgTmp = DatasetGraphFactory.create();
    ServletFileUpload upload = new ServletFileUpload();
    String graphName = null;
    boolean isQuads = false;
    long count = -1;
    String name = null;
    ContentType ct = null;
    Lang lang = null;
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String fieldName = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                // Graph name.
                String value = Streams.asString(stream, "UTF-8");
                if (fieldName.equals(HttpNames.paramGraph)) {
                    graphName = value;
                    if (graphName != null && !graphName.equals("") && !graphName.equals(HttpNames.valueDefault)) {
                        IRI iri = IRIResolver.parseIRI(value);
                        if (iri.hasViolation(false))
                            ServletOps.errorBadRequest("Bad IRI: " + graphName);
                        if (iri.getScheme() == null)
                            ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: " + graphName);
                        if (iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) {
                            // Redundant??
                            if (iri.getRawHost() == null)
                                ServletOps.errorBadRequest("Bad IRI: no host name: " + graphName);
                            if (iri.getRawPath() == null || iri.getRawPath().length() == 0)
                                ServletOps.errorBadRequest("Bad IRI: no path: " + graphName);
                            if (iri.getRawPath().charAt(0) != '/')
                                ServletOps.errorBadRequest("Bad IRI: Path does not start '/': " + graphName);
                        }
                    }
                } else if (fieldName.equals(HttpNames.paramDefaultGraphURI))
                    graphName = null;
                else
                    // Add file type?
                    action.log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName));
            } else {
                // Process the input stream
                name = item.getName();
                if (name == null || name.equals("") || name.equals("UNSET FILE NAME"))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                String contentTypeHeader = item.getContentType();
                ct = ContentType.create(contentTypeHeader);
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
                if (lang == null) {
                    lang = RDFLanguages.filenameToLang(name);
                    // present we wrap the stream accordingly
                    if (name.endsWith(".gz"))
                        stream = new GZIPInputStream(stream);
                }
                if (lang == null)
                    // Desperate.
                    lang = RDFLanguages.RDFXML;
                isQuads = RDFLanguages.isQuads(lang);
                action.log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s", action.id, name, ct.getContentType(), ct.getCharset(), lang.getName()));
                StreamRDF x = StreamRDFLib.dataset(dsgTmp);
                StreamRDFCounting dest = StreamRDFLib.count(x);
                ActionSPARQL.parse(action, dest, stream, lang, base);
                count = dest.count();
            }
        }
        if (graphName == null || graphName.equals(""))
            graphName = HttpNames.valueDefault;
        if (isQuads)
            graphName = null;
        return new UploadDetails(graphName, dsgTmp, count);
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex);
        return null;
    }
}
Also used : IRI(org.apache.jena.iri.IRI) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Aggregations

StreamRDF (org.apache.jena.riot.system.StreamRDF)34 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)9 Graph (org.apache.jena.graph.Graph)7 RiotException (org.apache.jena.riot.RiotException)7 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BaseTest (org.apache.jena.atlas.junit.BaseTest)3 Node (org.apache.jena.graph.Node)3 Triple (org.apache.jena.graph.Triple)3 Lang (org.apache.jena.riot.Lang)3 ContentType (org.apache.jena.atlas.web.ContentType)2 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 CmdException (jena.cmd.CmdException)1