public static String RenderTemplatePage(Bindings b, String templateName) throws IOException { MediaType mt = MediaType.TEXT_HTML; Resource config = model.createResource("eh:/root"); Mode prefixMode = Mode.PreferPrefixes; ShortnameService sns = new StandardShortnameService(); List<Resource> noResults = CollectionUtils.list(root.inModel(model)); Graph resultGraph = graphModel.getGraph(); resultGraph.getPrefixMapping().setNsPrefix("api", API.NS); resultGraph.add(Triple.create(root.asNode(), API.items.asNode(), RDF.nil.asNode())); APIResultSet rs = new APIResultSet(resultGraph, noResults, true, true, "details", View.ALL); VelocityRenderer vr = new VelocityRenderer(mt, null, config, prefixMode, sns); VelocityRendering vx = new VelocityRendering(b, rs, vr); VelocityEngine ve = vx.createVelocityEngine(); VelocityContext vc = vx.createVelocityContext(b); ByteArrayOutputStream bos = new ByteArrayOutputStream(); Writer w = new OutputStreamWriter(bos, "UTF-8"); Template t = ve.getTemplate(templateName); t.merge(vc, w); w.close(); return bos.toString(); }
public String executeQuery(String queryString) { /* //System.out.println(queryString); Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ResultSetFormatter.out(ostream, results, query); //ResultSetFormatter.out(System.out, results, query); String r = ""; try{ r = new String(ostream.toByteArray(), "UTF-8"); //System.out.println(r); } catch(Exception e){ System.out.println(e.getMessage()); } qe.close(); return r; */ //// new added for test JSON output try { Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ResultSetFormatter.outputAsJSON(ostream, results); // ResultSetFormatter.out(ostream, results, query); // ResultSetFormatter.out(System.out, results, query); String r = ""; try { r = new String(ostream.toByteArray(), "UTF-8"); // System.out.println(r); } catch (Exception e) { System.out.println(e.getMessage()); } qe.close(); return r; } catch (Exception e) { System.out.println(e.toString()); return ""; } }