@Override public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { int argsNum = node.jjtGetNumChildren(); for(int step = 0 ; step < argsNum; step++) { SimpleNode simpleNode = (SimpleNode) node.jjtGetChild(step); Object argObject = simpleNode.value(context); //传入参数 if(argObject instanceof String) { System.out.println((String)argObject); writer.write((String)argObject); writer.flush(); } } return true; }
public String mergeTemplateIntoString(String templateFilename, Map<String, Object> params) throws VelocityTemplateParsingException { try (InputStream is = settingsFacade.getRawConfig(templateFilename)) { StringReader reader = new StringReader(IOUtils.toString(is)); SimpleNode node = rs.parse(reader, templateFilename); Template template = new Template(); template.setRuntimeServices(rs); template.setData(node); template.initDocument(); StringWriter writer = new StringWriter(); template.merge(new VelocityContext(params), writer); return writer.toString(); } catch (ParseException|IOException e) { throw new VelocityTemplateParsingException("Couldn't merge template into string", e); } }
private void initTemplate(@NonNull final String verifyExpression) throws ParseException { // Init pattern ... final RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); final StringReader reader = new StringReader(verifyExpression); SimpleNode node = runtimeServices.parse(reader, this.getDocumentation().getId()); final Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); this.template = template; // Parse expression looking for variable ... final Pattern pattern = Pattern.compile(VELOCITY_VARIABLE); final Matcher matcher = pattern.matcher(verifyExpression); while (matcher.find()) { final String key = matcher.group(1); final ClassProperty property = ClassProperty.to(key); this.templateProperties.add(property); } }
@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 static Template create(String source, String templateName) throws ParseException { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(source); SimpleNode node = runtimeServices.parse(reader, templateName); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); return template; }
public VelocitySqlSource(Configuration configuration, String scriptText) { this.configuration = configuration; try { RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(scriptText); SimpleNode node = runtimeServices.parse(reader, "Template name"); script = new Template(); script.setRuntimeServices(runtimeServices); script.setData(node); script.initDocument(); } catch (Exception ex) { throw new BuilderException("Error parsing velocity script", ex); } }
/** * @작성자 : KYJ * @작성일 : 2017. 10. 14. * @param url * @return * @throws Exception */ public static Template createTemplate(InputStream is) throws Exception { String readFileToString = ValueUtil.toString(is); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(readFileToString); SimpleNode node = runtimeServices.parse(reader, "URLTemplate"); Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setData(node); template.initDocument(); return template; }
/** * Returns the result of substituting the variables defined by the fields of this class * (a concrete subclass of TemplateVars) into the template returned by {@link #parsedTemplate()}. */ String toText() { VelocityContext velocityContext = toVelocityContext(); StringWriter writer = new StringWriter(); SimpleNode parsedTemplate = parsedTemplate(); boolean rendered = velocityRuntimeInstance.render( velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate); if (!rendered) { // I don't know when this happens. Usually you get an exception during rendering. throw new IllegalArgumentException("Template rendering failed"); } return writer.toString(); }
static SimpleNode parsedTemplateForResource(String templateStr, String resourceName) { try { return velocityRuntimeInstance.parse(templateStr, resourceName); } catch (ParseException e) { throw new AssertionError(e); } }
static SimpleNode parsedTemplateForString(String string) { try { Reader reader = new StringReader(string); return RuntimeSingleton.parse(reader, string); } catch (ParseException e) { throw new AssertionError(e); } }
public static Object compile(String script, String name) { try { StringReader reader = new StringReader(script); Template template = new Template(); SimpleNode node = engine.parse(reader, template); template.setRuntimeServices(engine); template.setData(node); template.setName(name); template.initDocument(); return template; } catch (Exception ex) { throw new BuilderException("Error parsing velocity script '" + name + "'", ex); } }
@Override SimpleNode parsedTemplate() { return TEMPLATE; }
@Override SimpleNode parsedTemplate() { return parsedTemplateForString("integer=$integer string=$string list=$list"); }
@Override SimpleNode parsedTemplate() { throw new UnsupportedOperationException(); }