当前位置: 首页>>代码示例>>Java>>正文


Java Velocity.evaluate方法代码示例

本文整理汇总了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);
}
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:19,代码来源:VelocityUsage.java

示例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;
}
 
开发者ID:apache,项目名称:incubator-openaz,代码行数:23,代码来源:ConfigurableLDAPResolver.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-openaz,代码行数:26,代码来源:ConfigurableLDAPResolver.java

示例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;
  }
}
 
开发者ID:dbarowy,项目名称:java-aws-mturk,代码行数:18,代码来源:VelocityUtil.java

示例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();
}
 
开发者ID:FINRAOS,项目名称:herd,代码行数:25,代码来源:VelocityHelper.java

示例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);
    }
}
 
开发者ID:kbase,项目名称:kb_sdk,代码行数:20,代码来源:TemplateFormatter.java

示例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));
	}
}
 
开发者ID:JD-Software,项目名称:JDeSurvey,代码行数:21,代码来源:HelpController.java

示例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);
}
 
开发者ID:edgar615,项目名称:javase-study,代码行数:20,代码来源:MailServiceImpl.java

示例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);
}
 
开发者ID:zogwei,项目名称:zeus3,代码行数:17,代码来源:RenderHierarchyProperties.java

示例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);
    }
}
 
开发者ID:xianrendzw,项目名称:EasyReport,代码行数:25,代码来源:VelocityUtils.java

示例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);
    }
}
 
开发者ID:arahusky,项目名称:performance_javadoc,代码行数:30,代码来源:HttpExchangeUtils.java

示例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;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:27,代码来源:FileTemplateUtil.java

示例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; 
}
 
开发者ID:lexml,项目名称:madoc,代码行数:21,代码来源:VelocityExtensionUtils.java

示例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());
}
 
开发者ID:xuesong123,项目名称:jsp-jstl-engine,代码行数:29,代码来源:Test1.java

示例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.");
    }

}
 
开发者ID:mulesoft,项目名称:mule-tooling-incubator,代码行数:23,代码来源:VelocityReplacer.java


注:本文中的org.apache.velocity.app.Velocity.evaluate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。