Search in sources :

Example 31 with StreamRDF

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

the class DataValidatorHTML method executeHTML.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("data validation request") ;
        String syntax = FusekiLib.safeParameter(httpRequest, paramSyntax);
        if (syntax == null || syntax.equals(""))
            syntax = RDFLanguages.NQUADS.getName();
        Lang language = RDFLanguages.shortnameToLang(syntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + syntax);
            return;
        }
        Reader input = createInput(httpRequest, httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        ErrorHandlerMsg errorHandler = new ErrorHandlerMsg(outStream);
        // Capture logging errors.
        PrintStream stderr = System.err;
        System.setErr(new PrintStream(outStream));
        // Headers
        setHeaders(httpResponse);
        outStream.println("<html>");
        printHead(outStream, "Jena Data Validator Report");
        outStream.println("<body>");
        outStream.println("<h1>RIOT Parser Report</h1>");
        outStream.println("<p>Line and column numbers refer to original input</p>");
        outStream.println("<p>&nbsp;</p>");
        // Need to escape HTML. 
        OutputStream output1 = new OutputStreamNoHTML(new BufferedOutputStream(outStream));
        StreamRDF output = StreamRDFWriter.getWriterStream(output1, Lang.NQUADS);
        try {
            startFixed(outStream);
            RDFParser parser = RDFParser.create().lang(language).errorHandler(errorHandler).resolveURIs(false).build();
            RiotException exception = null;
            startFixed(outStream);
            try {
                output.start();
                parser.parse(output);
                output.finish();
                output1.flush();
                outStream.flush();
                System.err.flush();
            } catch (RiotException ex) {
                ex.printStackTrace(stderr);
                exception = ex;
            }
        } finally {
            finishFixed(outStream);
            System.err.flush();
            System.setErr(stderr);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in validationRequest", ex);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Lang(org.apache.jena.riot.Lang) RDFParser(org.apache.jena.riot.RDFParser) RiotException(org.apache.jena.riot.RiotException) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF)

Example 32 with StreamRDF

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

the class SPARQL_REST_RW method addDataIntoNonTxn.

/** Add data where the destination does not support full transactions.
     *  In particular, with no abort, and actions probably going to the real storage
     *  parse errors can lead to partial updates.  Instead, parse to a temporary
     *  graph, then insert that data.  
     * @param action
     * @param cleanDest Whether to remove data first (true = PUT, false = POST)
     * @return whether the target existed beforehand.
     */
protected static boolean addDataIntoNonTxn(HttpAction action, boolean overwrite) {
    Graph graphTmp = GraphFactory.createGraphMem();
    StreamRDF dest = StreamRDFLib.graph(graphTmp);
    try {
        incomingData(action, dest);
    } catch (RiotException ex) {
        errorBadRequest(ex.getMessage());
        return false;
    }
    // Now insert into dataset
    action.beginWrite();
    Target target = determineTarget(action);
    boolean existedBefore = false;
    try {
        if (log.isDebugEnabled())
            log.debug("  ->" + target);
        existedBefore = target.exists();
        if (overwrite && existedBefore)
            clearGraph(target);
        FusekiLib.addDataInto(graphTmp, target.dsg, target.graphName);
        action.commit();
        return existedBefore;
    } catch (Exception ex) {
        // but it might and there is no harm safely trying. 
        try {
            action.abort();
        } catch (Exception ex2) {
        }
        errorOccurred(ex.getMessage());
        return existedBefore;
    } finally {
        action.endWrite();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) IOException(java.io.IOException)

Example 33 with StreamRDF

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

the class TestStreamRDFThrift method graph_01.

// graph_01 and graph_02 are the same test but use different ways to read/write the graph.
// Ditto dataset_01 and dataset_02
@Test
public void graph_01() {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // With values.
    StreamRDF stream = BinRDF.streamToOutputStream(out, true);
    StreamOps.graphToStream(graph, stream);
    byte[] bytes = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    Graph g2 = GraphFactory.createGraphMem();
    StreamRDF stream2 = StreamRDFLib.graph(g2);
    BinRDF.inputStreamToStream(in, stream2);
    //assertTrue(graph.isIsomorphicWith(g2)) ;
    //****
    boolean b = IsoMatcher.isomorphic(graph, g2);
    assertTrue(b);
    // Stronger - same bNodes.
    sameTerms(graph, g2);
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) StreamRDF(org.apache.jena.riot.system.StreamRDF) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 34 with StreamRDF

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

the class TestStreamRDFThrift method dataset_01.

@Test
public void dataset_01() {
    DatasetGraph dsg1 = datasetGraph;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamRDF stream = BinRDF.streamToOutputStream(out);
    StreamOps.datasetToStream(dsg1, stream);
    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 : StreamRDF(org.apache.jena.riot.system.StreamRDF) ByteArrayInputStream(java.io.ByteArrayInputStream) 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)

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