org.apache.velocity.app.Velocity类的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(356)

本文整理了Java中org.apache.velocity.app.Velocity类的一些代码示例,展示了Velocity类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Velocity类的具体详情如下:
包路径:org.apache.velocity.app.Velocity
类名称:Velocity

Velocity介绍

[英]This class provides services to the application developer, such as :

  • Simple Velocity Runtime engine initialization methods.
  • Functions to apply the template engine to streams and strings to allow embedding and dynamic template generation.
  • Methods to access Velocimacros directly.

While the most common way to use Velocity is via templates, as Velocity is a general-purpose template engine, there are other uses that Velocity is well suited for, such as processing dynamically created templates, or processing content streams.

The methods herein were developed to allow easy access to the Velocity facilities without direct spelunking of the internals. If there is something you feel is necessary to add here, please, send a patch.
[中]此类为应用程序开发人员提供服务,例如:
*简单的Velocity运行时引擎初始化方法。
*函数将模板引擎应用于流和字符串,以允许嵌入和动态生成模板。
*方法直接访问Velocimacros。
虽然使用Velocity最常见的方式是通过模板,因为Velocity是一个通用模板引擎,但Velocity还可以用于其他用途,例如处理动态创建的模板或处理内容流。
本文所述方法的开发是为了在不直接探测内部构件的情况下方便地进入Velocity设施。如果您觉得有必要在这里添加一些内容,请发送一个补丁。

代码示例

代码示例来源:origin: jfinal/jfinal

public void render() {
   if (notInit) {
     Velocity.init(properties);	// Velocity.init("velocity.properties");	// setup
     notInit = false;
    VelocityContext context = new VelocityContext();
      context.put(attrName, request.getAttribute(attrName));
    Template template = Velocity.getTemplate(view);

代码示例来源:origin: looly/hutool

/**
 * 融合模板和内容
 * 
 * @param templateContent 模板的内容字符串
 * @param context 上下文
 * @return 模板和内容匹配后的内容
 */
public static String merge(String templateContent, VelocityContext context) {
  final StringWriter writer = new StringWriter();
  try {
    Velocity.evaluate(context, writer, IdUtil.randomUUID(), templateContent);
  } catch (Exception e) {
    throw new UtilException(e);
  }
  return writer.toString();
}

代码示例来源:origin: looly/hutool

/**
 * 初始化Velocity全局属性
 * 
 * @param templateDir 模板所在目录,绝对路径
 * @param charset 编码
 */
synchronized public static void init(String templateDir, String charset) {
  Velocity.init(_newInitedProp(templateDir, charset));
  Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true); // 使用缓存
  
  isInited = true; // 标记完成初始化
}

代码示例来源:origin: shuzheng/zheng

/**
 * 根据模板生成文件
 * @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;
  }
}

代码示例来源:origin: OpenNMS/opennms

private String generateSnmpGraphInternal(Collection<Report> reports, String graphTemplate) {
  Velocity.init();
  VelocityContext context = new VelocityContext();
  context.put("reportsList", reports.iterator());
  context.put("reportsBody", reports.iterator());
  Template template = Velocity.getTemplate(graphTemplate);
  StringWriter sw = new StringWriter();
  if (template != null) {
    template.merge(context, sw);
  }
  return sw.toString();
}

代码示例来源:origin: laiweiwei/eweb4j-framework

public String render(Map<String, Object> datas, String template) {
    Velocity.init();
    VelocityContext context = new VelocityContext();
    for (Iterator<Entry<String, Object>> it = datas.entrySet().iterator(); it.hasNext(); ){
      Entry<String, Object> e = it.next();
      context.put(e.getKey(), e.getValue());
    }
    StringWriter writer = new StringWriter();
    Velocity.evaluate(context, writer, "", template);
    
    return writer.toString();
  }
}

代码示例来源:origin: net.oschina.durcframework/easymybatis

public static String generate(VelocityContext context, Reader reader) {
  StringWriter writer = new StringWriter();
  // 不用vm文件
  Velocity.evaluate(context, writer, LOG_TAG, reader);
  try {
    writer.close();
    reader.close();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return writer.toString();
}

代码示例来源:origin: com.atlassian.selenium/atlassian-visual-comparison

public static String render(VelocityContext context, String templateName) throws Exception
 {
   Template template = Velocity.getTemplate(templateName);
   StringWriter sw = new StringWriter();
   template.merge(context, sw);
   return sw.toString();
 }
}

代码示例来源:origin: ueboot/ueboot

private String getTemplateString(String templateName, VelocityContext context) {
  Properties p = new Properties();
  String path = Thread.currentThread().getContextClassLoader().getResource("velocity").getFile();
  p.setProperty(Velocity.RESOURCE_LOADER, "class");
  p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
  p.setProperty("class.resource.loader.path", path);
  p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
  p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
  Velocity.init(p);
  Template template = Velocity.getTemplate("velocity" + separator + "ueboot" + separator + templateName, "UTF-8");
  StringWriter writer = new StringWriter();
  template.merge(context, writer);
  return writer.toString();
}

代码示例来源:origin: com.github.yroffin/jbehaviour-engine

/**
 * render this template
 * @param ctx
 * @param template
 * @return
 */
public String render(IBehaviourReflexion ctx, String template) {
  context.put("context", ctx);
  writer  = new StringWriter();
  Velocity.evaluate(context, writer, "", template);
  return writer.toString();
}

代码示例来源:origin: DomoXian/elastic-demo

/**
 * velocity模板合并
 *
 * @param template 模板字符串 如 hello,${name}
 * @param paramMap 参数
 * @return 合并结果
 * @throws Exception 抛出错误
 */
private static String merge(String template, Map<String, Object> paramMap) throws Exception {
  VelocityContext vc = new VelocityContext(paramMap);
  StringWriter writer = new StringWriter();
  Velocity.evaluate(vc, writer, "mybatis_code_gen", template);
  return writer.getBuffer().toString();
}

代码示例来源:origin: brucexx/heisenberg

public static void main(String[] args) {
  // System.out.println(substring(crc32("123123123123123"), -3, -1));
  String s = "<person>121212:/aaa/bbb";
  VelocityContext context = new VelocityContext();
  Writer writer = new StringWriter();
  // //Pattern.compile("^[0-9a-fA-F]+$")
  Velocity.evaluate(context, writer, StringUtil.EMPTY, "$!Pattern.compile(\"^[0-9a-fA-F]+$\")");
  System.out.println(writer.toString());
  System.out.println(substring(s, s.indexOf(">") + 1, s.indexOf(":")));
  // $!stringUtil.substring($PARENT_PATH, $!stringUtil.indexOf($PARENT_PATH,">") + 1,
  // $!stringUtil.indexOf($PARENT_PATH,":"))
  // $!stringUtil.substring
}

代码示例来源:origin: com.github.yroffin/jbehaviour-engine

@Override
public String asString(String value) throws JBehaviourParsingError {
  writer = new StringWriter();
  Velocity.evaluate( context, writer, "", value);
  return writer.toString();
}

代码示例来源:origin: org.apache.ace/org.apache.ace.client.repository.helper.base

private byte[] process(byte[] input, PropertyResolver props) throws IOException {
  try {
    VelocityContext context = new VelocityContext();
    context.put("context", props);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(baos);
    Velocity.evaluate(context, writer, "", new InputStreamReader(new ByteArrayInputStream(input)));
    writer.flush();
    return baos.toByteArray();
  }
  catch (IOException ioe) {
    throw new IOException("Error processing the artifact: " + ioe.getMessage());
  }
}

代码示例来源:origin: paypal/SeLion

Velocity.init();
Velocity.setProperty("resource.loader", "class");
Velocity.setProperty("class.resource.loader.class",
    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
String fileName = dataFile.getName();
String className = fileName.substring(0, fileName.indexOf("."));
VelocityContext context = new VelocityContext();
context.put("class", className);
context.put("members", htmlObjectDetailsList);
context.put("control", set);
context.put("package", packageName);
Velocity.evaluate(context, writer, "code generator", br);
writer.flush();
writer.close();

代码示例来源:origin: com.github.yroffin/jbehaviour-engine

@Override
public void init() {
  Velocity.init();
  context = new VelocityContext();
}

代码示例来源:origin: com.fluxtion.extension/fluxtion-fu-text-builder

private void generate() throws IOException {
  Template template = Velocity.getTemplate(templateFile);
  Context ctx = new VelocityContext();
  ctx.put("package", packageName);
  ctx.put("className", className);
  ctx.put("functionName", functionName);
  ctx.put("functionClass", functionClass);
  ctx.put("functionClassFqn", functionClassFqn);
  ctx.put("isArrayFunction", isArrayFunction);
  ctx.put("isStatefulFunction", isStatefulFunction);
  File outFile = new File(srcPackageDirectory, className + ".java");
  FileWriter templateWriter = new FileWriter(outFile);
  template.merge(ctx, templateWriter);
  templateWriter.flush();
}

代码示例来源:origin: eclipse/winery

public BpelPlanArtefactWriter(ManagementFlow mangagementFlow) {
  this.mangagementFlow = mangagementFlow;
  Velocity.init();
}

代码示例来源:origin: yusuke/samurai

public VelocityHtmlRenderer(String style, String baseurl) {
  this.style = style;
  this.baseurl = baseurl;
  Velocity.setProperty("input.encoding", "UTF-8");
  Velocity.setProperty("resource.loader", "class");
  Velocity.setProperty("class.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  Velocity.setProperty("runtime.log.logsystem.class", "samurai.web.NullVelocityLogger");
  try {
    Velocity.init();
    tableView = Velocity.getTemplate("samurai/web/table.vm");
    threaddumpView = Velocity.getTemplate("samurai/web/threaddump.vm");
    sequenceView = Velocity.getTemplate("samurai/web/sequence.vm");
  } catch (Exception ex) {
    ex.printStackTrace();
  }
}

代码示例来源:origin: looly/hutool

/**
 * 生成文件,使用默认引擎
 * 
 * @param templateFileName 模板文件名
 * @param context 模板上下文
 * @param destPath 目标路径(绝对)
 */
public static void toFile(String templateFileName, VelocityContext context, String destPath) {
  assertInit();
  toFile(Velocity.getTemplate(templateFileName), context, destPath);
}

相关文章