本文整理汇总了Java中org.apache.velocity.app.Velocity.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Java Velocity.evaluate方法的具体用法?Java Velocity.evaluate怎么用?Java Velocity.evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.velocity.app.Velocity
的用法示例。
在下文中一共展示了Velocity.evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: usage1
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
public void usage1(String inputFile) throws FileNotFoundException {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("author", "Elliot A.");
context.put("address", "217 E Broadway");
context.put("phone", "555-1337");
FileInputStream file = new FileInputStream(inputFile);
//Evaluate
StringWriter swOut = new StringWriter();
Velocity.evaluate(context, swOut, "test", file);
String result = swOut.getBuffer().toString();
System.out.println(result);
}
示例2: prepareVelocityTemplate
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
private Set<String> prepareVelocityTemplate(String template) throws PIPException {
VelocityContext vctx = new VelocityContext();
EventCartridge vec = new EventCartridge();
VelocityParameterReader reader = new VelocityParameterReader();
vec.addEventHandler(reader);
vec.attachToContext(vctx);
try {
Velocity.evaluate(vctx, new StringWriter(), "LdapResolver", template);
} catch (ParseErrorException pex) {
throw new PIPException("Velocity template preparation failed", pex);
} catch (MethodInvocationException mix) {
throw new PIPException("Velocity template preparation failed", mix);
} catch (ResourceNotFoundException rnfx) {
throw new PIPException("Velocity template preparation failed", rnfx);
}
if (this.logger.isTraceEnabled()) {
this.logger.trace("(" + id + ") " + template + " with parameters " + reader.parameters);
}
return reader.parameters;
}
示例3: evaluateVelocityTemplate
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
private String evaluateVelocityTemplate(String template,
final Map<String, PIPRequest> templateParameters,
final PIPFinder pipFinder)
throws PIPException {
StringWriter out = new StringWriter();
VelocityContext vctx = new VelocityContext();
EventCartridge vec = new EventCartridge();
VelocityParameterWriter writer = new VelocityParameterWriter(pipFinder, templateParameters);
vec.addEventHandler(writer);
vec.attachToContext(vctx);
try {
Velocity.evaluate(vctx, out, "LdapResolver", template);
} catch (ParseErrorException pex) {
throw new PIPException("Velocity template evaluation failed", pex);
} catch (MethodInvocationException mix) {
throw new PIPException("Velocity template evaluation failed", mix);
} catch (ResourceNotFoundException rnfx) {
throw new PIPException("Velocity template evaluation failed", rnfx);
}
this.logger.warn("(" + id + ") " + " template yields " + out.toString());
return out.toString();
}
示例4: doMerge
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
public static String doMerge(String strToMerge, Map map) {
if (strToMerge == null)
return null;
try {
// ask Velocity to evaluate it.
Velocity.init();
StringWriter w = new StringWriter();
VelocityContext context = new VelocityContext(map);
Velocity.evaluate(context, w, "logTag", strToMerge);
return w.getBuffer().toString();
} catch (Exception e) {
return null;
}
}
示例5: evaluate
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
/**
* Wrapper for {@link Velocity#evaluate(org.apache.velocity.context.Context, java.io.Writer, String, java.io.InputStream)}
*
* @param templateReader A {@link Reader} of a Velocity template.
* @param variables Variables to add to context
* @param logTag The log tag
*
* @return {@link String} result of evaluation
*/
public String evaluate(Reader templateReader, Map<String, Object> variables, String logTag)
{
VelocityContext velocityContext = new VelocityContext(variables);
StringWriter writer = new StringWriter();
if (!Velocity.evaluate(velocityContext, writer, logTag, templateReader))
{
// Although the Velocity.evaluate method's Javadoc states that it will return false to indicate a failure when the template couldn't be
// processed and to see the Velocity log messages for more details, upon examining the method's implementation, it doesn't look like
// it will ever return false. Instead, other RuntimeExceptions will be thrown (e.g. ParseErrorException).
// Nonetheless, we'll leave this checking here to honor the method's contract in case the implementation changes in the future.
// Having said that, there will be no way to JUnit test this flow.
throw new IllegalStateException("Error evaluating velocity template. See velocity log message for more details.");
}
return writer.toString();
}
示例6: formatTemplate
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
public static boolean formatTemplate(String templateName, Map<?,?> context,
Writer output) {
try {
if (!context.containsKey("esc")) {
Map untyped = (Map)context;
untyped.put("esc", new VelocityEscaper());
}
Reader input = new InputStreamReader(TemplateFormatter.class.getResourceAsStream(
templateName + "." + "vm.properties"), Charset.forName("utf-8"));
VelocityContext cntx = new VelocityContext(context);
boolean ret = Velocity.evaluate(cntx, output, "Template " + templateName, input);
input.close();
output.flush();
return ret;
} catch (Exception ex) {
throw new IllegalStateException("Problems with template evaluation (" + templateName + ")", ex);
}
}
示例7: getHelpPage
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
@Secured({"ROLE_ADMIN","ROLE_SURVEY_ADMIN"})
@RequestMapping(produces = "text/html")
public String getHelpPage(Model uiModel) {
try {
StringWriter sw = new StringWriter();
VelocityContext velocityContext = new VelocityContext();
Velocity.evaluate(velocityContext, sw, "velocity-log" ,
surveySettingsService.velocityTemplate_findById(HELP_VELOCITY_TEMPLATE_ID).getDefinition());
uiModel.addAttribute("helpContent", sw.toString().trim());
return "help/index";
} catch (Exception e) {
log.error(e.getMessage(),e);
throw (new RuntimeException(e));
}
}
示例8: sendTplEmail
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
@Override
public void sendTplEmail(final String from, final List<String> to, final List<String> cc,
final String subject, final String tpl, final Map<String, Object> model) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(to.toArray(new String[to.size()]));
message.setCc(cc.toArray(new String[cc.size()]));
message.setFrom(from);
message.setSubject(subject);
VelocityContext context = new VelocityContext(model);
StringWriter w = new StringWriter();
Velocity.evaluate(context, w, "emailString", tpl);
message.setText(w.toString(), true);
}
};
JavaMailSenderImpl javaMailSenderImpl = (JavaMailSenderImpl) mailSender;
javaMailSenderImpl.send(preparator);
}
示例9: main
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception{
VelocityContext context=new VelocityContext();
context.put("zdt", new ZeusDateTool());
String s="abc${zdt.addDay(-1).format(\"yyyyMMdd\")} ${zdt.addDay(1).format(\"yyyyMMdd\")}";
Pattern pt = Pattern.compile("\\$\\{zdt.*\\}");
Matcher matcher=pt.matcher(s);
while(matcher.find()){
String m= s.substring(matcher.start(),matcher.end());
System.out.println(m);
StringWriter sw=new StringWriter();
Velocity.evaluate(context, sw, "", m);
System.out.println(sw.toString());
s=s.replace(m, sw.toString());
}
System.out.println("result:"+s);
}
示例10: parse
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
/**
* 替换的模板中的参数
*
* @param template
* @param parameters
* @param logTag
* @return 替换后的文本
*/
public static String parse(final String template, final Map<String, Object> parameters, final String logTag) {
try (StringWriter writer = new StringWriter()) {
Velocity.init();
final VelocityContext context = new VelocityContext();
for (final Entry<String, Object> kvset : parameters.entrySet()) {
context.put(kvset.getKey(), kvset.getValue());
}
context.put("Calendar", Calendar.getInstance());
context.put("DateUtils", DateUtils.class);
context.put("StringUtils", StringUtils.class);
Velocity.evaluate(context, writer, logTag, template);
return writer.toString();
} catch (final Exception ex) {
throw new TemplatePraseException(ex);
}
}
示例11: mergeTemplateAndSentPositiveResponseAndClose
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
/**
* Merges template with context, then sends given message to the client with
* the success headers and closes communication channel.
*
* @param exchange stands for client's representation
* @param templateName
* @param context
*/
public static void mergeTemplateAndSentPositiveResponseAndClose(HttpExchange exchange, String templateName, VelocityContext context) {
InputStream template = HttpExchangeUtils.class.getResourceAsStream(templatesFolder + templateName + ".vm");
InputStreamReader input = new InputStreamReader(template);
StringWriter w = new StringWriter();
Velocity.evaluate(context, w, "", input);
//Velocity.mergeTemplate(templateLocation, "UTF-8", context, w);
byte[] message = w.getBuffer().toString().getBytes();
try {
exchange.sendResponseHeaders(200, message.length);
//autoclosable handles closing
try (OutputStream responseBody = exchange.getResponseBody()) {
responseBody.write(message);
}
} catch (IOException e) {
log.log(Level.INFO, "Unable to send the results to the client", e);
}
}
示例12: mergeTemplate
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
private static String mergeTemplate(String templateContent, final VelocityContext context, boolean useSystemLineSeparators) throws IOException {
final StringWriter stringWriter = new StringWriter();
try {
Velocity.evaluate(context, stringWriter, "", templateContent);
}
catch (final VelocityException e) {
LOG.error("Error evaluating template:\n"+templateContent,e);
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
Messages.showErrorDialog(IdeBundle.message("error.parsing.file.template", e.getMessage()),
IdeBundle.message("title.velocity.error"));
}
});
}
final String result = stringWriter.toString();
if (useSystemLineSeparators) {
final String newSeparator = CodeStyleSettingsManager.getSettings(ProjectManagerEx.getInstanceEx().getDefaultProject()).getLineSeparator();
if (!"\n".equals(newSeparator)) {
return StringUtil.convertLineSeparators(result, newSeparator);
}
}
return result;
}
示例13: render
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
/**
* Processa o renderizador Velocity do conteúdo de vtl
*/
public static String render(String vtl, VelocityContext ctx, VelocityEngine velocityEngine) {
if (vtl == null) {
return null;
}
StringWriter sw = new StringWriter();
boolean success;
if (velocityEngine == null) {
success = Velocity.evaluate(ctx, sw, VelocityExtensionHTML2FO.class.getName(), vtl);
} else {
success = velocityEngine.evaluate(ctx, sw, VelocityExtensionHTML2FO.class.getName(), vtl);
}
return success? sw.toString(): vtl;
}
示例14: main
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) {
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.setProperty(RuntimeConstants.RUNTIME_LOG, "velocity.log");
Velocity.init();
StringWriter writer = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("a", 1);
context.put("b", 2);
context.put("c", 3);
context.put("sysTime", new Date());
context.put("dateUtil", new DateUtil());
String template = ""
+ "#set($timeString = $dateUtil.format($sysTime, 'yyyy-MM-dd HH:mm:ss') + 'ssss')\r\n"
+ "#set($value = ($a + $b) * $c)\r\n"
+ "$value\r\n"
+ "${($a + $b) * $c}\r\n"
+ "$dateUtil.format($sysTime, 'yyyy-MM-dd HH:mm:ss')\r\n"
+ "$timeString\r\n";
Velocity.evaluate(context, writer, "mergeTemplate", template);
System.out.println(writer.toString());
}
示例15: replace
import org.apache.velocity.app.Velocity; //导入方法依赖的package包/类
@Override
public void replace(Reader reader, Writer writer) throws Exception {
Velocity.init();
VelocityContext context = new VelocityContext();
//avoid having to copy the entire set of properties.
//see template.
if (model != null) {
context.put("project", this.model);
}
if (mapModel != null) {
context.put("model", this.mapModel);
}
boolean evaluate = Velocity.evaluate(context, writer, "velocity class rendering", reader);
if (evaluate == false) {
throw new Exception("Evaluation of the template failed.");
}
}