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 void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); boolean htmlOutput = true; String language = null; String contentType = "text/html; charset='utf-8'"; if (path != null) { if (path.endsWith(".rdf")) { contentType = "application/rdf+xml; charset='utf-8'"; htmlOutput = false; language = "RDF/XML"; } else if (path.endsWith(".xml")) { contentType = "application/xml; charset='utf-8'"; htmlOutput = false; language = "RDF/XML"; } else if (path.endsWith(".n3")) { contentType = "text/n3; charset='utf-8'"; htmlOutput = false; language = "N3"; } } response.setContentType(contentType); response.setStatus(HttpServletResponse.SC_OK); Writer writer = response.getWriter(); synchronized (board) { if (htmlOutput) { writer.write( "<!DOCTYPE html>\n" + "<html lang='en'>" + "<head><meta charset='utf-8'/><title>MATe model</title></head>" + "<body><ul>"); StmtIterator it = model.listStatements(); /* TODO: well, this could be prettier */ while (it.hasNext()) writer.write("<li>" + it.nextStatement() + "</li>"); writer.write("<ul></body></html>"); } else model.write(writer, language); } }