Java Code Examples for org.apache.velocity.app.VelocityEngine#mergeTemplate()

The following examples show how to use org.apache.velocity.app.VelocityEngine#mergeTemplate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VelocityTemplateProcessor.java    From fastquery with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(String templateReference, Viewable viewable, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
		OutputStream out) throws IOException {

	// 获取 模板引擎
	VelocityEngine velocityEngine = getVelocityEngine();

	// 实例化一个VelocityContext
	VelocityContext context = (VelocityContext) viewable.getModel();
	Enumeration<String> enums = request.getParameterNames();
	while (enums.hasMoreElements()) {
		String key = enums.nextElement();
		context.put(key, request.getParameter(key));
	}
	// 把request放进模板上下文里
	context.put("request", request);

	// 渲染并输出
	OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);
	velocityEngine.mergeTemplate(templateReference, "utf8", context, outputStreamWriter);
	outputStreamWriter.flush();
	outputStreamWriter.close(); // 有必要关闭吗? 关闭了是否对jax-rs拦截器,servlet有影响,需要继续学习,参考jsp模板实现
}
 
Example 2
Source File: Analyzer.java    From osmo with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Create the string for the report to write to disk about found invariants.
 * 
 * @return The report to write.
 */
public String createReport() {
  if (state.getTests().size() == 0) return "No failing tests found.";
  VelocityEngine velocity = new VelocityEngine();
  VelocityContext vc = new VelocityContext();
  vc.put("testCount", state.getTestCount());
  vc.put("lengths", state.getLengths());
  vc.put("finalLength", state.getTests().get(0).getAllStepNames().size());
  vc.put("shortests", state.getTests().size());
  vc.put("stepCounts", invariants.getUsedStepCounts());
  vc.put("missingSteps", invariants.getMissingSteps());
  vc.put("finalSteps", invariants.getLastSteps());
  vc.put("s_precedences", invariants.getStrictPrecedences());
  vc.put("f_precedences", invariants.getFlexPrecedences());
  vc.put("sequences", invariants.getSequences());

  velocity.setProperty("resource.loader", "class");
  velocity.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  StringWriter sw = new StringWriter();
  velocity.mergeTemplate("osmo/tester/optimizer/reducer/template.vm", "UTF8", vc, sw);
  return sw.toString();
}
 
Example 3
Source File: VelocityUtils.java    From DWSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 渲染模板文件.
 * 
 * @param velocityEngine velocityEngine, 需经过VelocityEngineFactory处理, 绑定Spring的ResourceLoader.
 * @param templateContent 模板文件名, loader会自动在前面加上velocityEngine的resourceLoaderPath.
 * @param context 变量Map.
 */
public static String renderFile(String templateFilePName, VelocityEngine velocityEngine, String encoding,
		Map<String, ?> context) {
	VelocityContext velocityContext = new VelocityContext(context);

	StringWriter result = new StringWriter();
	velocityEngine.mergeTemplate(templateFilePName, encoding, velocityContext, result);
	return result.toString();
}
 
Example 4
Source File: HelloVelocityScripter.java    From osmo with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String giefScript() {
  /** For template->script generation. */
  VelocityEngine velocity = new VelocityEngine();
  /** For storing template variables. */
  VelocityContext vc = new VelocityContext();
  vc.put("tests", tests);
  velocity.setProperty("resource.loader", "class");
  velocity.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  StringWriter sw = new StringWriter();
  velocity.mergeTemplate("osmo/tester/examples/tutorial/scripting/hello-template.vm", "UTF8", vc, sw);
  return sw.toString();
}
 
Example 5
Source File: VelocityHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static boolean mergeTemplate(VelocityEngine vengine, String vTemplate,
		Context context, PrintWriter out)
{
	boolean retval = false;
	try {
		vengine.mergeTemplate(vTemplate, context, out);
		retval = true;
	}

	finally
	{
		if ( retval == false) log.warn("Unable to process Template - "+vTemplate);
		return retval;
	}
}
 
Example 6
Source File: VelocityHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static boolean mergeTemplate(VelocityEngine vengine, String vTemplate,
		Context context, PrintWriter out)
{
	boolean retval = false;
	try {
		vengine.mergeTemplate(vTemplate, context, out);
		retval = true;
	}

	finally
	{
		if ( retval == false) log.warn("Unable to process Template - {}", vTemplate);
		return retval;
	}
}
 
Example 7
Source File: TagUtils.java    From ontopia with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 8
Source File: VelocityHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static boolean mergeTemplate(VelocityEngine vengine, String vTemplate,
		Context context, PrintWriter out)
{
	boolean retval = false;
	try {
		vengine.mergeTemplate(vTemplate, context, out);
		retval = true;
	}

	finally
	{
		if ( retval == false) log.warn("Unable to process Template - "+vTemplate);
		return retval;
	}
}
 
Example 9
Source File: VelocityHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static boolean mergeTemplate(VelocityEngine vengine, String vTemplate,
		Context context, PrintWriter out)
{
	boolean retval = false;
	try {
		vengine.mergeTemplate(vTemplate, context, out);
		retval = true;
	}

	finally
	{
		if ( retval == false) log.warn("Unable to process Template - {}", vTemplate);
		return retval;
	}
}
 
Example 10
Source File: VelocityEngineUtils.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
 * @deprecated Use {@link #mergeTemplate(VelocityEngine, String, String, Map, Writer)}
 * instead, following Velocity 1.6's corresponding deprecation in its own API.
 */
@Deprecated
public static void mergeTemplate(
		VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer)
		throws VelocityException {

	VelocityContext velocityContext = new VelocityContext(model);
	velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
}
 
Example 11
Source File: VelocityEngineUtils.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 12
Source File: VelocityEngineUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 * @deprecated Use {@link #mergeTemplate(VelocityEngine, String, String, Map, Writer)}
 * instead, following Velocity 1.6's corresponding deprecation in its own API.
 */
@Deprecated
public static void mergeTemplate(
		VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer)
		throws VelocityException {

	VelocityContext velocityContext = new VelocityContext(model);
	velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
}
 
Example 13
Source File: VelocityEngineUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: VelocityEngineUtils.java    From MaxKey with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 * @deprecated Use {@link #mergeTemplate(VelocityEngine, String, String, Map, Writer)}
 * 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)
		throws VelocityException {

	VelocityContext velocityContext = new VelocityContext(model);
	velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
}
 
Example 15
Source File: VelocityEngineUtils.java    From MaxKey with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}