VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); VelocityContext context = new VelocityContext(); context.put("date", getMyTimestampFunction()); Template t = ve.getTemplate( "templates/email_html_new.vm" ); StringWriter writer = new StringWriter(); t.merge( context, writer );
protected void action() throws Exception { StringWriter stringWriter = new StringWriter(); stringWriter.write("$('#"); stringWriter.write(id); stringWriter.write("').html(\""); FileObject templateFileObject = fullContextFileRepository .getFileObjectDetectLocale(path); Template template = velocity.getTemplate( templateFileObject.getPath(), ENCODING); HtmlStringWriter tempWriter = new HtmlStringWriter(); template.merge(context, tempWriter, macroList); stringWriter.write(tempWriter.toString()); stringWriter.write("\");\n"); synchronized (writer) { writer.write(stringWriter.toString()); } } }
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { // create the velocity context VelocityContext context = createVelocityContext(model); // merge the template try { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(templateContent); SimpleNode node = runtimeServices.parse(reader, "StringTemplate"); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); template.merge(context, writer); } catch (Exception e) { throw new PippoRuntimeException(e); } }
public String generatePayloadFromTemplate(String template,SequenceGeneratorInput input,String vmId) throws ParseException { String payload = null; RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(template); SimpleNode node = runtimeServices.parse(reader,template); Template templateData = new Template(); templateData.setRuntimeServices(runtimeServices); templateData.setData(node); templateData.initDocument(); VelocityContext vc = new VelocityContext(); String identityUrl = input.getInventoryModel().getVnf().getIdentityUrl(); logger.debug("IdentityUrl:" + identityUrl); logger.debug("Vm-Id:" + vmId); vc.put("identity-url", identityUrl); vc.put("vm-id", vmId); StringWriter stringWriter = new StringWriter(); templateData.merge(vc, stringWriter); payload= stringWriter.toString(); logger.debug("Payload Data:" +stringWriter.toString()); return payload; } }
public String getContentAsString() { try { Template template = getTemplate(); StringWriter writer = new StringWriter(); template.merge(null, writer); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public String render(ModelAndView modelAndView) { Template template = velocityEngine.getTemplate(provider.layout(), "utf-8"); Object model = modelAndView.getModel(); if (model == null) { model = Collections.emptyMap(); } if (model instanceof Map) { VelocityContext context = initialContextProvider.getVelocityContext((Map) model, controller, modelAndView.getViewName()); StringWriter writer = new StringWriter(); template.merge(context, writer); Object meta = context.get("meta"); context.put("meta", GSON.toJson(meta)); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } }
private static String applyToVelocityTemplate(String appMapURL, List<JenkinsAMChart> strategyCharts) { VelocityEngine ve = new VelocityEngine(); StringWriter writer = new StringWriter(); ve.setProperty("resource.loader", "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); Template t = ve.getTemplate("amHistogramChartsReport.vm"); VelocityContext context = new VelocityContext(); context.put("strategyCharts", strategyCharts); context.put("url", appMapURL); t.merge(context, writer); return writer.toString(); }
public void processTempleate(InternalContextAdapter context, Writer writer, String path) throws Exception { String templatePath = findTemplatePath(path); Template template = velocity.getTemplate(templatePath, ENCODING); template.merge(context, writer, macroList); writer.flush(); }
/** * 处理外部pagelet */ public void processTempleate(Context context, Writer writer, String path) throws Exception { TinyVelocityContext tinyVelocityContext = new TinyVelocityContext( context); if (initContext != null) { context.putSubContext(BASE_CONTEXT, initContext); } VelocityContext velocityContext = new VelocityContext( tinyVelocityContext); String templatePath = findTemplatePath(path); Template template = velocity.getTemplate(templatePath, ENCODING); template.merge(velocityContext, writer, macroList); writer.flush(); }
private void mergeAndWrite(Template tmpl, VelocityContext context, Writer writer, boolean deleted) throws ExportException { try { // common variables context.put(VM_UTIL, this); if (deleted) { context.put(VM_DELETED, Boolean.TRUE); context.put(VM_VERSION, "D"); } tmpl.merge(context, writer); writer.flush(); // writer.close(); } catch (Exception e) { log.error("Error", e); throw new ExportException("Couldn't merge template and context.", e); } }
private RenderedTemplate renderTemplate(Template velocityTemplate, VelocityContext velocityContext) { PipedInputStream pipedInputStream = new PipedInputStream(); try (PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream)) { Writer writer = new OutputStreamWriter(pipedOutputStream); RenderedTemplate renderedTemplate = new RenderedTemplate(); renderedTemplate.setContentStream(pipedInputStream); TemplateResourcesTool templateResourcesTool = (TemplateResourcesTool) velocityContext.get(RESOURCE_TOOL_CONTEXT_KEY); return CompletableFuture.runAsync(() -> { velocityTemplate.merge(velocityContext, writer); try { writer.close(); } catch (IOException e) { throw new DispatcherRuntimeException("Failed to render template", e); } }).thenApply((a) -> { Set<ReferencedResource> referencedResources = templateResourcesTool.getReferencedResources(); renderedTemplate.setReferencedResources(referencedResources); return renderedTemplate; }).join(); } catch (IOException e) { throw new DispatcherRuntimeException("Failed to render template", e); } }
/** * 融合模板和内容并写入到Writer * @param template 模板 * @param context 内容 * @param writer Writer */ public static void merge(Template template, VelocityContext context, Writer writer) { if(template == null) { throw new UtilException("Template is null"); } if(context == null) { context = new VelocityContext(globalContext); }else { //加入全局上下文 for (Entry<String, Object> entry : globalContext.entrySet()) { context.put(entry.getKey(), entry.getValue()); } } template.merge(context, writer); }
VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); final String templatePath = "templates/" + templateName + ".vm"; InputStream input = this.getClass().getClassLoader().getResourceAsStream(templatePath); if (input == null) { throw new IOException("Template file doesn't exist"); } InputStreamReader reader = new InputStreamReader(input); VelocityContext context = new VelocityContext(); if (properties != null) { stringfyNulls(properties); for (Map.Entry<String, Object> property : properties.entrySet()) { context.put(property.getKey(), property.getValue()); } } Template template = ve.getTemplate(templatePath, "UTF-8"); String outFileName = File.createTempFile("report", ".html").getAbsolutePath(); BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outFileName))); if (!ve.evaluate(context, writer, templatePath, reader)) { throw new Exception("Failed to convert the template into html."); } template.merge(context, writer); writer.flush(); writer.close();
final VelocityEngine ve = new VelocityEngine(); p.toString())); ve.init(p); final Context context = new VelocityContext(); context.put("catalog", catalog); context.put("identifiers", identifiers); templateLocation, templateEncoding)); final Template template = ve.getTemplate(templateLocation, templateEncoding); template.merge(context, writer);
@Override public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception { if (StringUtils.isEmpty(templatePath)) { return; } Template template = velocityEngine.getTemplate(templatePath, ConstVal.UTF8); try (FileOutputStream fos = new FileOutputStream(outputFile); OutputStreamWriter ow = new OutputStreamWriter(fos, ConstVal.UTF8); BufferedWriter writer = new BufferedWriter(ow)) { template.merge(new VelocityContext(objectMap), writer); } logger.debug("模板:" + templatePath + "; 文件:" + outputFile); }
@Override protected void renderPage(HttpServletRequest req, VelocityContext ctx, Writer writer) { ui.getVelocityEngine().getTemplate("query.vm").merge(ctx, writer); } }
@Override public void render(BeatContext beatContext) { Template template = factory.getTemplate(viewName); HttpServletResponse response = beatContext.getResponse(); response.setContentType("text/html;charset=\"UTF-8\""); response.setCharacterEncoding("UTF-8"); // init context: Context context = new VelocityContext(beatContext.getModel().getModel()); // render: VelocityWriter vw = null; try { vw = new VelocityWriter(response.getWriter()); template.merge(context, vw); vw.flush(); } catch (IOException e) { throw ArgoException.raise(e); } finally { if (vw != null) vw.recycle(null); } } }
@Override protected void renderPage(HttpServletRequest req, VelocityContext ctx, Writer writer) { String key = (String) ctx.get("key"); Integer ordinal = (Integer) ctx.get("ordinal"); ui.getVelocityEngine().getTemplate("browse-selected-type-top.vm").merge(ctx, writer); try { Writer htmlEscapingWriter = new HtmlEscapingWriter(writer); if (!"".equals(key) && ordinal != null && ordinal.equals(-1)) { htmlEscapingWriter.append("ERROR: Key " + key + " was not found!"); } else if (ordinal != null && !ordinal.equals(-1)) { HollowStringifier stringifier = "json".equals(req.getParameter("display")) ? new HollowRecordJsonStringifier() : new HollowRecordStringifier(); stringifier.stringify(htmlEscapingWriter, ui.getStateEngine(), getTypeState(req).getSchema().getName(), ordinal); } } catch (IOException e) { throw new RuntimeException("Error streaming response", e); } ui.getVelocityEngine().getTemplate("browse-selected-type-bottom.vm").merge(ctx, writer); }
@Override public void render(Map<?, ?> bindingMap, Writer writer) { rawTemplate.merge(toContext(bindingMap), writer); IoUtil.flush(writer); }
/** * Returns a Velocity Resource which is a Template for the given SQL. */ public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception { synchronized (templateCache) { Template resource = (Template) templateCache.get(resourceName); if (resource == null) { resource = new Template(); resource.setRuntimeServices(rsvc); resource.setResourceLoader(this); resource.setName(resourceName); resource.setEncoding(encoding); resource.process(); templateCache.put(resourceName, resource); } return resource; } }