private static String applyTemplate(String templateName, VelocityContext context) { VelocityEngine velocity = new VelocityEngine(); Properties properties = new Properties(); properties.setProperty("resource.loader", "class"); properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocity.init(properties); Writer writer = new StringWriter(); velocity.mergeTemplate(templateName, "UTF-8", context, writer); try { writer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return writer.toString(); }
public void render(String template, Map<String, Object> map, OutputStream stream) { VelocityContext context = new VelocityContext(map); OutputStreamWriter writer = new OutputStreamWriter(stream); try { engine.mergeTemplate(baseTemplateDir + "/" + template, "UTF-8", context, writer); writer.flush(); } catch(Exception e) { throw new VoldemortException(e); } }
/** * Renders the page in UTF-8 */ public void render() { try { this.response.setHeader("Content-type", "text/html; charset=UTF-8"); this.response.setCharacterEncoding("UTF-8"); this.response.setContentType(this.mimeType); this.engine.mergeTemplate(this.template, "UTF-8", this.context, this.response.getWriter()); } catch (final Exception e) { throw new PageRenderException(e); } }
/** * Render a template and return the result * * @param templateName the template name only without the .vm extension * @param context the context * @return string */ public static String renderTemplate(final String templateName, final VelocityContext context) { final StringWriter stringWriter = new StringWriter(); final VelocityEngine engine = new VelocityEngine(); engine.init("src/test/resources/velocity.properties"); engine.mergeTemplate(TEMPLATE_BASE_DIR + templateName + ".vm", "UTF-8", context, stringWriter); return stringWriter.getBuffer().toString(); }
public String process(Map<String, Object> context, TemplateIdentifier templateIdentifier) { StringWriter stringWriter = new StringWriter(); VelocityContext velocityContext = new VelocityContext(context); try { velocityEngine.mergeTemplate(templateIdentifier.getFileName(), "UTF-8", velocityContext, stringWriter); return stringWriter.toString(); } catch (ResourceNotFoundException e) { LOGGER.error("", e); } catch (ParseErrorException e) { LOGGER.error("", e); } catch (MethodInvocationException e) { LOGGER.error("", e); } catch (Exception e) { LOGGER.error("", e); } return ""; }
/** * Merge the specified Velocity template with the given model and write * the result to the given Writer. * @param velocityEngine VelocityEngine to work with * @param templateLocation the location of template, relative to Velocity's resource loader path * @param model the Map that contains model names as keys and model objects as values * @param writer the Writer to write the result to * @throws VelocityException if the template wasn't found or rendering failed * instead, following Velocity 1.6's corresponding deprecation in its own API. */ public static void mergeTemplate( VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer) { VelocityContext velocityContext = new VelocityContext(model); velocityEngine.mergeTemplate(templateLocation, velocityContext, writer); }
/** * Merge the specified Velocity template with the given model and write the result * to the given Writer. * @param velocityEngine VelocityEngine to work with * @param templateLocation the location of template, relative to Velocity's resource loader path * @param encoding the encoding of the template file * @param model the Map that contains model names as keys and model objects as values * @param writer the Writer to write the result to * @throws VelocityException if the template wasn't found or rendering failed */ public static void mergeTemplate( VelocityEngine velocityEngine, String templateLocation, String encoding, Map<String, Object> model, Writer writer) throws VelocityException { VelocityContext velocityContext = new VelocityContext(model); velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer); }
/** * Merge the specified Velocity template with the given model and write the result * to the given Writer. * @param velocityEngine VelocityEngine to work with * @param templateLocation the location of template, relative to Velocity's resource loader path * @param encoding the encoding of the template file * @param model the Map that contains model names as keys and model objects as values * @param writer the Writer to write the result to * @throws VelocityException if the template wasn't found or rendering failed */ public static void mergeTemplate( VelocityEngine velocityEngine, String templateLocation, String encoding, Map<String, Object> model, Writer writer) throws VelocityException { VelocityContext velocityContext = new VelocityContext(model); velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer); }
private static void processMetadataTemplate(File templateFile, VelocityEngine ve, VelocityContext vc, Logger logger) { final String templateName = templateFile.getName(); final String outputName = templateName.substring(0, templateName.lastIndexOf('.')); logger.info(String.format("Writing metadata file '%s'...", outputName)); try (Writer writer = new FileWriter(outputName)) { ve.mergeTemplate(templateName, RuntimeConstants.ENCODING_DEFAULT, vc, writer); } catch (Exception e) { final String msgPattern = "Failed to generate metadata file from template '%s': %s"; logger.log(Level.SEVERE, String.format(msgPattern, templateName, e.getMessage()), e); } }
public void merge ( String templateName, Map<String,Object> data, PrintWriter to ) { final VelocityContext ctx = new VelocityContext ( fBaseRenderContext ); for ( Entry<String, Object> e : data.entrySet () ) { ctx.put ( e.getKey(), e.getValue() ); } fServlet.getVelocity ().mergeTemplate ( templateName, "UTF-8", ctx, to ); }
private static void createOutputFileList(VelocityEngine ve, String template, String output, XMLModel model) throws Exception { VelocityContext context = getContext(null, null); context.put("model", model); File parentPatch = new File(output).getParentFile(); if (!parentPatch.exists()) { parentPatch.mkdirs(); } FileWriter listout = new FileWriter(new File(output)); String encoding = "utf-8"; ve.mergeTemplate(template, encoding, context, listout); listout.close(); }
private static void createOutputFileList(VelocityEngine ve, String template, String output) throws Exception { VelocityContext context = getContext(null, null); File parentPatch = new File(output).getParentFile(); if (!parentPatch.exists()) { parentPatch.mkdirs(); } FileWriter listout = new FileWriter(new File(output)); String encoding = "utf-8"; ve.mergeTemplate(template, encoding, context, listout); listout.close(); }
public static void writeWithTemplate(String path, String encoding, VelocityContext context, Writer writer) throws Exception { get().mergeTemplate(path, encoding, context, writer); // Velocity.mergeTemplate(path, encoding, context, writer); }
public void render(Writer writer) { try { VelocityContext context = new VelocityContext(attributes); if(path != null) { velocityEngine.mergeTemplate(path, encoding, context, writer); } else { velocityEngine.evaluate(context, writer, getClass().getName(), new InputStreamReader(clazz.getResourceAsStream(resource), encoding)); } } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Renders the page in UTF-8 */ public void render() { try { response.setHeader ("Content-type", "text/html; charset=UTF-8"); response.setCharacterEncoding ("UTF-8"); response.setContentType(mimeType); engine.mergeTemplate(template, "UTF-8", context, response.getWriter()); } catch (Exception e) { throw new PageRenderException(e); } }
@Override public void process(HttpServletRequest request, String templateId, Map<String, Object> model, Writer out ) { org.apache.velocity.app.VelocityEngine velocityEngine = VelocityEngine.newVelocityEngine(); initializeVelocityEngine(velocityEngine); VelocityContext context = new VelocityContext(); model.entrySet().stream().forEach( e -> context.put(e.getKey(), e.getValue()) ); velocityEngine.mergeTemplate(templateId, UTF_8.name(), context, out); }
@Autowired private VelocityEngine velocityEngine; public void createHtml(String templateLocation, Map<String, Object> model) throws Exception { ToolManager toolManager = new ToolManager(); ToolContext toolContext = toolManager.createContext(); VelocityContext velocityContext = new VelocityContext(model, toolContext); StringWriter resultWriter = new StringWriter(); velocityEngine.mergeTemplate(templateLocation, "UTF-8", velocityContext, resultWriter); String html = resultWriter.toString(); // use the HTML here }
private void merge(OutputBuilder builder, Writer writer) throws IOException { VelocityContext context = builder.getContext(); Optional<Type> type = builder.getType(); Collection<String> imports = cleanupImports(builder.getImports(), type); if (type.isPresent()) { context.put("impl", type.get()); } context.put("processor", builder.getProcessor()); context.put("processingDate", PROCESSING_DATE_FORMAT.format(new Date())); context.put("imports", imports); getEngine().mergeTemplate(builder.getTemplateFile(), ENCODING, context, writer); }
public static void processWithVelocity(PageContext pageContext, String template_file, Writer writer, VelocityContext vc) { try { VelocityEngine vengine = getVelocityEngine(pageContext.getServletContext()); vengine.mergeTemplate(template_file, org.apache.velocity.runtime.RuntimeSingleton.getString(Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), vc, writer); } catch (Exception e) { throw new OntopiaRuntimeException(e); } }
protected void postEncode(final MessageContext<SAMLObject> messageContext, final String endpointURL) throws MessageEncodingException { log.debug("Invoking Velocity template to create POST body"); try { final VelocityContext velocityContext = new VelocityContext(); this.populateVelocityContext(velocityContext, messageContext, endpointURL); responseAdapter.setContentType("text/html"); responseAdapter.init(); final OutputStreamWriter out = responseAdapter.getOutputStreamWriter(); this.getVelocityEngine().mergeTemplate(this.getVelocityTemplateId(), "UTF-8", velocityContext, out); out.flush(); } catch (Exception e) { throw new MessageEncodingException("Error creating output document", e); } }