Search in sources :

Example 91 with Template

use of org.apache.velocity.Template in project traccar by traccar.

the class NotificationFormatter method getTemplate.

public static Template getTemplate(Event event, String path) {
    String templateFilePath;
    Template template;
    try {
        templateFilePath = Paths.get(path, event.getType() + ".vm").toString();
        template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
    } catch (ResourceNotFoundException error) {
        Log.warning(error);
        templateFilePath = Paths.get(path, "unknown.vm").toString();
        template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
    }
    return template;
}
Also used : ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 92 with Template

use of org.apache.velocity.Template in project motech by motech.

the class VelocityTemplateParser method mergeTemplateIntoString.

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);
    }
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) VelocityContext(org.apache.velocity.VelocityContext) StringReader(java.io.StringReader) ParseException(org.apache.velocity.runtime.parser.ParseException) IOException(java.io.IOException) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template) VelocityTemplateParsingException(org.motechproject.security.exception.VelocityTemplateParsingException)

Example 93 with Template

use of org.apache.velocity.Template in project bioformats by openmicroscopy.

the class VelocityTools method processTemplate.

public static void processTemplate(VelocityEngine ve, VelocityContext context, String inFile, String outFile) throws // NB: No choice, as VelocityEngine.getTemplate(String) throws Exception
Exception {
    System.out.print("Writing " + outFile + ": ");
    Template t = ve.getTemplate(inFile);
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    PrintWriter out = new PrintWriter(new File(outFile), "UTF-8");
    out.print(writer.toString());
    out.close();
    System.out.println("done.");
}
Also used : StringWriter(java.io.StringWriter) File(java.io.File) Template(org.apache.velocity.Template) PrintWriter(java.io.PrintWriter)

Example 94 with Template

use of org.apache.velocity.Template in project alien4cloud by alien4cloud.

the class VelocityUtil method generate.

public static void generate(String path, Writer outputWriter, Map<String, ?> properties) throws IOException {
    Template template = VELOCITY_ENGINE.getTemplate(path, "UTF-8");
    VelocityContext context = new VelocityContext();
    for (Entry<String, ?> contextEntry : properties.entrySet()) {
        context.put(contextEntry.getKey(), contextEntry.getValue());
    }
    putIfAbsent(context, "utils", new ToscaSerializerUtils());
    putIfAbsent(context, "propertyUtils", new ToscaPropertySerializerUtils());
    putIfAbsent(context, "importsUtils", new ToscaImportsUtils());
    try {
        template.merge(context, outputWriter);
    } finally {
        outputWriter.close();
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 95 with Template

use of org.apache.velocity.Template in project hutool by looly.

the class VelocityUtil method toWriter.

/**
 * 生成内容写入流<br>
 * 会自动关闭Writer
 *
 * @param templateFileName 模板文件名
 * @param context 上下文
 * @param writer 流
 */
public static void toWriter(String templateFileName, VelocityContext context, Writer writer) {
    assertInit();
    final Template template = Velocity.getTemplate(templateFileName);
    merge(template, context, writer);
}
Also used : Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)160 VelocityContext (org.apache.velocity.VelocityContext)118 StringWriter (java.io.StringWriter)76 VelocityEngine (org.apache.velocity.app.VelocityEngine)39 Test (org.junit.Test)33 File (java.io.File)21 IOException (java.io.IOException)19 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)17 Writer (java.io.Writer)14 FileWriter (java.io.FileWriter)12 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)12 ParseErrorException (org.apache.velocity.exception.ParseErrorException)11 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)9 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)9 FileOutputStream (java.io.FileOutputStream)8 PrintWriter (java.io.PrintWriter)8 Map (java.util.Map)8 Properties (java.util.Properties)8 VelocityException (org.apache.velocity.exception.VelocityException)7 OutputStreamWriter (java.io.OutputStreamWriter)6