Java Code Examples for org.apache.jena.atlas.io.IndentedWriter#flush()

The following examples show how to use org.apache.jena.atlas.io.IndentedWriter#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplatePlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public void exec(List<Var> variables, List<Binding> values, Context context) {
	final IndentedWriter writer = ContextUtils.getTemplateOutput(context);
	boolean first = true;
	final FunctionEnv env = new FunctionEnvBase(context);
	String result;
	for(Iterator<Binding> it=values.iterator(); it.hasNext();) {
		Binding binding = it.next();
		if (first && before != null) {
			result = getExprEval(before, binding, context, env);
			writer.print(result);
		}
		if (!first && separator != null) {
			result = getExprEval(separator, binding, context, env);
			writer.print(result);
		}
		result = getExprEval(expr, binding, context, env);
		writer.print(result);
		first = false;
		if (!it.hasNext() && after != null) {
			result = getExprEval(after, binding, context, env);
			writer.print(result);
		}
		writer.flush();
	}
}
 
Example 2
Source File: tarql.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void processResults(TarqlQueryExecution ex) throws IOException {
	if (testQuery && ex.getFirstQuery().getConstructTemplate() != null) {
		IndentedWriter out = new IndentedWriter(System.out); 
		new FmtTemplate(out, new SerializationContext(ex.getFirstQuery())).format(ex.getFirstQuery().getConstructTemplate());
		out.flush();
	}
	if (ex.getFirstQuery().isSelectType()) {
		System.out.println(ResultSetFormatter.asText(ex.execSelect()));
	} else if (ex.getFirstQuery().isAskType()) {
		System.out.println(ResultSetFormatter.asText(ex.execSelect()));
	} else if (ex.getFirstQuery().isConstructType()) {
		resultTripleIterator = resultTripleIterator.andThen(ex.execTriples());
	} else {
		cmdError("Only query forms CONSTRUCT, SELECT and ASK are supported");
	}
}
 
Example 3
Source File: StreamingRDFWriter.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void writeTurtle(String baseIRI, PrefixMapping prefixes, boolean writeBase) {
	// Auto-register RDF prefix so that rdf:type is displayed well
	// All other prefixes come from the query and should be as author intended
	prefixes = ensureRDFPrefix(prefixes);

	if (writeBase) {
		// Jena's streaming Turtle writers don't output base even if it is provided,
		// so we write it directly.
		IndentedWriter w = new IndentedWriter(out);
		RiotLib.writeBase(w, baseIRI);
		w.flush();
	}
	
	StreamRDF writer = new WriterStreamRDFBlocks(out);
	if (dedupWindowSize > 0) {
		writer = new StreamRDFDedup(writer, dedupWindowSize);
	}
	writer.start();
	writer.base(baseIRI);
	for (Entry<String, String> e : prefixes.getNsPrefixMap().entrySet()) {
		writer.prefix(e.getKey(), e.getValue());
	}
	StreamOps.sendTriplesToStream(triples, writer);
	writer.finish();
}
 
Example 4
Source File: PHandlerSPARQLUpdateOutput.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Patch patch) {
    IndentedWriter x = new IndentedWriter(System.out) ;
    x.setLineNumbers(true);
    x.setLinePrefix("GSP>> ");
    RDFChanges scData = new RDFChangesWriteUpdate(x) ;
    patch.play(scData);
    x.flush();
}
 
Example 5
Source File: JSONX.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Write out a JSON value - pass a JSON Object to get legal exchangeable JSON */
private static void writeFlat(OutputStream output, JsonValue jValue) {
    IndentedWriter iOut = new IndentedWriter(output) ;
    iOut.setFlatMode(true);
    JSON.write(iOut, jValue) ;
    iOut.flush() ;
}
 
Example 6
Source File: SHACLCWriter.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void write(IndentedWriter out, Graph graph, PrefixMap prefixMap, String baseURI, Context context) {
	Model model = ModelFactory.createModelForGraph(graph);
	if(baseURI != null) {
		out.println("BASE <" + baseURI + ">");
		out.println();
	}
	writeImports(out, model.getResource(baseURI));
	writePrefixes(out, prefixMap);
	writeShapes(out, model);
	out.flush();
}
 
Example 7
Source File: RDFChangesHTTP.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
private void send$() {
    long number = counter.incrementAndGet();

    byte[] bytes = collected();
    String idStr;

    if ( patchId != null )
        idStr = Id.str(patchId);
    else
        idStr = Long.toString(number);
    FmtLog.info(LOG, "Send patch %s (%d bytes) -> %s", idStr, bytes.length, destLabel);

    if ( false ) {
        if ( LOG.isDebugEnabled() ) {
            String s = new String(bytes, StandardCharsets.UTF_8);
            LOG.debug("== Sending ...");
            // Do NOT close!
            IndentedWriter w = IndentedWriter.stdout;
            String x = w.getLinePrefix();
            w.setLinePrefix(">> ");
            w.print(s);
            w.setLinePrefix(x);
            if ( ! s.endsWith("\n") )
                w.println();
            w.flush();
            LOG.debug("== ==");
        }
    }

    int attempts = 0 ;
    for(;;) {
        HttpPost postRequest = new HttpPost(urlSupplier.get());
        postRequest.setEntity(new ByteArrayEntity(bytes));

        try(CloseableHttpResponse r = httpClient.execute(postRequest) ) {
            attempts++;
            statusLine = r.getStatusLine();
            response = readResponse(r);
            int sc = r.getStatusLine().getStatusCode();
            if ( sc >= 200 && sc <= 299 )
                return ;
            if ( sc >= 300 && sc <= 399 ) {
                FmtLog.info(LOG, "Send patch %s HTTP %d", idStr, sc);
                throw new DeltaHttpException(sc, "HTTP Redirect");
            }
            if ( sc == 400 ) {
                // Bad request.
                // This includes being out of sync with the patch log due to a concurrent update.
                FmtLog.warn(LOG, "Patch %s : HTTP bad request: %s", idStr, r.getStatusLine().getReasonPhrase());
                throw new DeltaBadPatchException(r.getStatusLine().getReasonPhrase());
            }
            if ( sc == 401 && attempts == 1 && resetAction != null ) {
                resetAction.run();
                continue;
            }
            if ( sc >= 400 && sc <= 499 )
                throw new DeltaHttpException(sc, r.getStatusLine().getReasonPhrase());
            if ( sc >= 500 )
                throw new DeltaHttpException(sc, r.getStatusLine().getReasonPhrase());
            break;
        }
        catch (DeltaHttpException ex) { throw ex; }
        catch (IOException e) { throw IOX.exception(e); }
    }
}
 
Example 8
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static void write(IndentedWriter out, Path path, SerializationContext context) {
    PathWriterWorker w = new PathWriterWorker(out, context);
    path.visit(w);
    out.flush();
}