/** * Process the template with the given name forn the given context node and write the result to the given * output writer. The way the template is retrieved depends on the template loader, which can be set using the * setTemplateLoader() method. * * @param context the initial context node to apply this template to * @param templateName the name of the template * @param out where to write the results * @throws IOException * @throws TemplateException */ public void processFileTemplate(Node context, String templateName, Writer out) throws IOException, TemplateException { processTemplate(context,freemarker.getTemplate(templateName),null,out); }
private void processTemplate(Node context, Template template, Map<String, ?> initialEnv, Writer out) throws IOException, TemplateException { Map<String, Object> root = new HashMap<>(); if(initialEnv != null) { for(Map.Entry<String, ?> entry : initialEnv.entrySet()) { root.put(entry.getKey(), entry.getValue()); } } root.put("namespace", new NamespaceDirective()); root.put("evalLDPath",new LDPathMethod<>(backend)); root.put("ldpath",new LDPathDirective<>(backend)); TemplateStackModel contexts = new TemplateStackModel(); contexts.push(new TemplateNodeModel<>(context,backend)); root.put("context",contexts); template.process(root,out); } }
/** * Renders a GraphNode with a template located in the templates * folder of any active bundle * * @param node the GraphNode to be rendered * @param templatePath the freemarker path to the template * @param out where the result is written to */ public void render(GraphNode node, final String templatePath, Writer out) { //A GraphNode backend could be graph unspecific, so the same engine could be //reused, possibly being signifantly more performant (caching, etc.) RDFBackend<RDFTerm> backend = new ClerezzaBackend(node.getGraph()); RDFTerm context = node.getNode(); TemplateEngine<RDFTerm> engine = new TemplateEngine<RDFTerm>(backend); engine.setTemplateLoader(templateLoader); try { engine.processFileTemplate(context, templatePath, null, out); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } catch (TemplateException e) { throw new RuntimeException(e); } }
TemplateEngine<Value> engine = new TemplateEngine<>(backend); engine.setDirectoryForTemplateLoading(template.getAbsoluteFile().getParentFile()); engine.processFileTemplate(context,template.getName(),out); out.flush();
/** * Renders a GraphNode with a template located in the templates * folder of any active bundle * * @param node the GraphNode to be rendered * @param templatePath the freemarker path to the template * @param out where the result is written to */ public void render(GraphNode node, final String templatePath, Writer out) { //A GraphNode backend could be graph unspecific, so the same engine could be //reused, possibly being signifantly more performant (caching, etc.) RDFBackend<RDFTerm> backend = new ClerezzaBackend(node.getGraph()); RDFTerm context = node.getNode(); TemplateEngine<RDFTerm> engine = new TemplateEngine<RDFTerm>(backend); engine.setTemplateLoader(templateLoader); try { engine.processFileTemplate(context, templatePath, null, out); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } catch (TemplateException e) { throw new RuntimeException(e); } }
/** * Process the template with the given name forn the given context node and write the result to the given * output writer. The initial environment is passed over to the invocation of the template. The way the template * is retrieved depends on the template loader, which can be set using the setTemplateLoader() method. * * @param context the initial context node to apply this template to * @param templateName the name of the template * @param initialEnv an initial root environment for processing the template * @param out where to write the results * @throws IOException * @throws TemplateException */ public void processFileTemplate(Node context, String templateName, Map<String, ?> initialEnv, Writer out) throws IOException, TemplateException { processTemplate(context,freemarker.getTemplate(templateName),initialEnv,out); }