/** * 根据模板生成文件 * @param inputVmFilePath 模板路径 * @param outputFilePath 输出文件路径 * @param context * @throws Exception */ public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception { try { Properties properties = new Properties(); properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath)); Velocity.init(properties); //VelocityEngine engine = new VelocityEngine(); Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8"); File outputFile = new File(outputFilePath); FileWriterWithEncoding writer = new FileWriterWithEncoding(outputFile, "utf-8"); template.merge(context, writer); writer.close(); } catch (Exception ex) { throw ex; } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/decisiontable-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
public void testTemplate1() throws Exception { VelocityContext context = new VelocityContext(); MyBean bean = createBean(); context.put("bean", bean); Yaml yaml = new Yaml(); context.put("list", yaml.dump(bean.getList())); VelocityEngine ve = new VelocityEngine(); ve.setProperty("file.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.init(); Template t = ve.getTemplate("template/mybean1.vm"); StringWriter writer = new StringWriter(); t.merge(context, writer); String output = writer.toString().trim().replaceAll("\\r\\n", "\n"); // System.out.println(output); String etalon = Util.getLocalResource("template/etalon2-template.yaml").trim(); assertEquals(etalon.length(), output.length()); assertEquals(etalon, output); // parse the YAML document Yaml loader = new Yaml(); MyBean parsedBean = loader.loadAs(output, MyBean.class); assertEquals(bean, parsedBean); }
private void sendEmail(final String fromEmail, final IUser to, final String inSubject, final String inTemplate, final Map<String, Object> values) { final Properties props = new Properties(); props.setProperty("resource.loader", "class"); props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); final VelocityEngine engine = new VelocityEngine(props); final VelocityContext context = new VelocityContext(); engine.init(); for(final String key : values.keySet()) { LOGGER.debug(() -> String.format("\t -- %s=%s", key, values.get(key))); context.put(key, values.get(key)); } final StringWriter writer = new StringWriter(); final Template template = engine.getTemplate("templates/" + inTemplate); template.merge(context, writer); final String inBody = writer.toString(); sendEmail(fromEmail, to.getEmail(), inSubject, inBody); }
private static StringWriter buildStringWriter(List<Node> nodes, List<Edge> edges, Map<EVMEnvironment, SymExecutor> executions) throws ReportException { VelocityEngine velocityEngine = new VelocityEngine(); Properties p = new Properties(); p.setProperty("resource.loader", "class"); p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocityEngine.init(p); Template template = velocityEngine.getTemplate(TEMPLATE_FILE); VelocityContext velocityContext = new VelocityContext(); ObjectMapper objectMapper = new ObjectMapper(); try { velocityContext.put("nodes", objectMapper.writeValueAsString(nodes)); velocityContext.put("edges", objectMapper.writeValueAsString(edges)); velocityContext.put("executions", executions); } catch (IOException e) { logger.error("Error building the report: " + e); throw new ReportException("Failed creating ReportItem"); } StringWriter stringWriter = new StringWriter(); template.merge(velocityContext, stringWriter); return stringWriter; }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/constant-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/scriptdecisiontable-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if(!((PermissionService)permissionStore).isAdmin()){ throw new NoPermissionException(); } String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/permission-config-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/decisiontree-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/variable-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/rule-flow-designer.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/parameter-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String key=req.getParameter("key"); String msg=null; if(StringUtils.isBlank(key)){ msg="<h2 style='color:red'>请指定要查看的调试消息的key值</h2>"; }else{ msg=debugMessageHolder.getDebugMessage(key); } VelocityContext context = new VelocityContext(); context.put("title", "URule Console"); context.put("msg", msg); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/console.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/client-config-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/ul-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/scorecard-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); String file=req.getParameter("file"); file=Utils.decodeURL(file); String project = buildProjectNameFromFile(file); if(project!=null){ context.put("project", project); } resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/ruleset-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); context.put("welcomePage", welcomePage); context.put("title", title); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/frame.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); context.put("files", req.getParameter("files")); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/rete-diagram.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/package-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("html/action-editor.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
private static List<String> generateHtmlChunks(List<ReportItem> reportItemList) { List<String> htmlChunks = new ArrayList<>(); VelocityEngine velocityEngine = new VelocityEngine(); Properties p = new Properties(); p.setProperty("resource.loader", "class"); p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocityEngine.init(p); Template template = velocityEngine.getTemplate("template/report_template.html"); int maxItemsInReport = CliHelper.getMaxItemsInReport(); List<List<ReportItem>> reportItemsChunks = Lists.partition(reportItemList, maxItemsInReport); for (List<ReportItem> reportItemsChunk : reportItemsChunks ) { VelocityContext velocityContext = new VelocityContext(); velocityContext.put("jarPath", CliHelper.getPathToAnalyze()); velocityContext.put("ruleName", reportItemsChunk.get(0).getRuleName()); velocityContext.put("reportItems", reportItemsChunk); StringWriter stringWriter = new StringWriter(); template.merge(velocityContext, stringWriter); htmlChunks.add(stringWriter.toString()); } return htmlChunks; }
/** * Merge the template with the context. * Can be overridden to customize the behavior. * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see org.apache.velocity.Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getWrappedThrowable(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause==null ? ex : cause); } }
private String buildEmailTemplate(User fromUser, User toUser, String originPassword) { try { final String detailUrl = HttpURL.build(webDomain).append("/admin/members/list").toString(); Template template = velocityEngine.getTemplate("email/register_email.vm"); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("fromUser", fromUser); velocityContext.put("toUser", toUser); velocityContext.put("password", originPassword); velocityContext.put("detailUrl", detailUrl); StringWriter stringWriter = new StringWriter(); template.merge(velocityContext, stringWriter); return stringWriter.toString(); } catch (Throwable e) { LOGGER.warn("sendMessage", "send message to user error : %s", ExceptionUtil.findRootCause(e).getMessage()); return null; } }
@Override protected void writeUIOther(String destResourcePath, List<String> tables, List<String> modelNames, List<String> varModelNames) throws IOException { Template indexHtml = VelocityUtil.getTempate("/templates/pom/ui/angular1/index.vm"); Template indexJs = VelocityUtil.getTempate("/templates/pom/ui/angular1/indexjs.vm"); Template confirmModal = VelocityUtil.getTempate("/templates/pom/ui/angular1/confirm_modal.html"); Template messageModal = VelocityUtil.getTempate("/templates/pom/ui/angular1/message_modal.html"); VelocityContext ctx = new VelocityContext(); ctx.put("modelNames", modelNames); ctx.put("tables", tables); ctx.put("varModelNames", varModelNames); ctx.put("project", this.project); ctx.put("project", VelocityUtil.tableNameConvertModelName(this.project.split("-")[0])); ctx.put("miniProject", VelocityUtil.tableNameConvertModelName(this.project.split("-")[0]).substring(0, 1)); write(merge(indexHtml, ctx), destResourcePath + "/static/index.html"); write(merge(indexJs, ctx), destResourcePath + "/static/js/app.js"); write(merge(confirmModal, ctx), destResourcePath + "/static/confirm_modal.html"); write(merge(messageModal, ctx), destResourcePath + "/static/message_modal.html"); }
protected void writeConfiguration(String destProject, String project, String packagePath) throws IOException { String configurationPath = getSrcConfigurationPath(); if (configurationPath == null) return; for (String filename : getSrcConfigurationFiles()) { String vmFile = (configurationPath + "/" + filename).replaceAll("[/]+", "/"); VelocityContext ctx = new VelocityContext(); ctx.put("packagePath", packagePath); ctx.put("project", project); for (Map.Entry<String, Object> entry : options.entrySet()) { ctx.put(entry.getKey(), entry.getValue()); } String destFileName = destProject + "/" + (packagePath + "/config/").replace(".", "/") + "/" + filename.replace(".vm", ".java"); Template tempate = VelocityUtil.getTempate(vmFile); String merge = merge(tempate, ctx); write(merge, destFileName); } }
protected void writeBin(String destDir, String project, String packagePath) throws IOException { String binPath = getBinPath(); List<String> files = getBinFiles(); for (String fileName : files) { String file = binPath + fileName; Template template = VelocityUtil.getTempate(file); VelocityContext ctx = new VelocityContext(); ctx.put("packagePath", packagePath); ctx.put("project", project); if (file.contains("shutdown")) { ctx.put("application", this.applicationName); } else { ctx.put("application", packagePath + "." + this.applicationName); } StringWriter writer = new StringWriter(); template.merge(ctx, writer); writer.close(); write(writer.toString(), destDir + fileName); } }
protected void writeApplicationConfigs(String destApplicationPath, String project, String packagePath) throws IOException { String applicationPath = getSrcApplicationFilesPath(); List<String> applicationFiles = getApplicationFiles(); for (String filename : applicationFiles) { if (filename.contains("/")) { String parentPath = filename.substring(0, filename.lastIndexOf("/")); new File(destApplicationPath + "/" + parentPath).mkdirs(); } Template tempate = VelocityUtil.getTempate((applicationPath + "/" + filename).replaceAll("[/]+", "/")); VelocityContext ctx = new VelocityContext(); ctx.put("project", project); ctx.put("driverClass", driverClass); ctx.put("username", username); ctx.put("password", password); ctx.put("packagePath", packagePath); ctx.put("realPath", packagePath.replace(".", "/")); ctx.put("url", url); StringWriter writer = new StringWriter(); tempate.merge(ctx, writer); writer.close(); write(writer.toString(), destApplicationPath + "/" + filename); } }
protected void writePom(String srcPom, String destPom, String project, String packagePath) throws IOException { Template tempate = VelocityUtil.getTempate(srcPom); VelocityContext ctx = new VelocityContext(); ctx.put("packagePath", packagePath); ctx.put("project", project); ctx.put("application", packagePath + "." + this.applicationName); ctx.put("driverClass", driverClass); ctx.put("serviceProject", serviceProject); ctx.put("serviceApiProject", serviceApiProject); ctx.put("parentProject", parentProject); ctx.put("springCloudVersion", springCloudVersion); ctx.put("springBootVersion", springBootVersion); for (Map.Entry<String, Object> entry : options.entrySet()) { ctx.put(entry.getKey(), entry.getValue()); } StringWriter writer = new StringWriter(); tempate.merge(ctx, writer); writer.close(); write(writer.toString(), destPom); }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("uflo-html/central.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("uflo-html/todo.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("uflo-html/calendar.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=retriveMethod(req); if(method!=null){ invokeMethod(method, req, resp); }else{ VelocityContext context = new VelocityContext(); context.put("contextPath", req.getContextPath()); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); Template template=ve.getTemplate("uflo-html/designer.html","utf-8"); PrintWriter writer=resp.getWriter(); template.merge(context, writer); writer.close(); } }
public void outputCode(String codePath, String templatePath) throws IOException { VelocityContext context = new VelocityContext(); context.put("cModule", cModule); Template template = null; try { template = Velocity.getTemplate(templatePath, TEMPLATE_ENCODING); } catch (ResourceNotFoundException e) { throw e; } File file = new File(codePath); logger.info("Generating {} ({})", file.getName(), templatePath); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), OUTPUT_ENCODING))); template.merge(context, pw); pw.close(); }