Search in sources :

Example 76 with Template

use of org.apache.velocity.Template in project lucene-solr by apache.

the class VelocityResponseWriter method getTemplate.

private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException {
    Template template;
    String templateName = request.getParams().get(TEMPLATE);
    String qt = request.getParams().get(CommonParams.QT);
    String path = (String) request.getContext().get("path");
    if (templateName == null && path != null) {
        templateName = path;
    }
    // TODO: path is never null, so qt won't get picked up  maybe special case for '/select' to use qt, otherwise use path?
    if (templateName == null && qt != null) {
        templateName = qt;
    }
    if (templateName == null)
        templateName = "index";
    try {
        template = engine.getTemplate(templateName + TEMPLATE_EXTENSION);
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
    return template;
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 77 with Template

use of org.apache.velocity.Template in project Gargoyle by callakrsos.

the class MailUtil method getTemplateFromFile.

/**
	 * 파일로부터 템플릿 정보를 얻어온다.
	 *
	 * @Date 2015. 9. 13.
	 * @param templateFileName
	 * @return
	 * @throws Exception
	 * @User KYJ
	 */
public static Template getTemplateFromFile(final String templateFileName) throws Exception {
    String readFileToString = "";
    if (templateFileName.startsWith("classpath:")) {
        String res = templateFileName.replace("classpath:", "");
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(res);
        readFileToString = ValueUtil.toString(resourceAsStream);
    } else
        readFileToString = FileUtils.readFileToString(new File(templateFileName));
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(readFileToString);
    SimpleNode node = runtimeServices.parse(reader, templateFileName);
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();
    return template;
}
Also used : RuntimeServices(org.apache.velocity.runtime.RuntimeServices) InputStream(java.io.InputStream) StringReader(java.io.StringReader) File(java.io.File) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template)

Example 78 with Template

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

the class VelocityTemplateEngine method render.

@Override
public String render(ModelAndView modelAndView) {
    Template template = velocityEngine.getTemplate(layout, "utf-8");
    Object model = modelAndView.getModel();
    if (model == null) {
        model = Collections.emptyMap();
    }
    if (model instanceof Map) {
        VelocityContext context = initialContextProvider.getVelocityContext((Map) model, controller, modelAndView.getViewName());
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        return writer.toString();
    } else {
        throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) Template(org.apache.velocity.Template)

Example 79 with Template

use of org.apache.velocity.Template in project hale by halestudio.

the class Templates method loadTemplate.

/**
 * Loads the specified template file and merges it with a Velocity context
 * instance created from the provided variables; the final output is
 * returned as an {@link InputStream} instance.
 *
 * <p>
 * NOTE: due to a problem with Velocity's classpath loader not working in an
 * OSGi context, the template file is first copied to a temp file and then
 * loaded from there. {@link File#deleteOnExit()} is invoked on the temp
 * file <code>File</code> object after the template is merged.
 * </p>
 *
 * @param templateResource the template to load
 * @param variables the template context
 * @return the result of merging the template with the provided context
 * @throws TemplateException if an error occurs processing the template
 */
public InputStream loadTemplate(String templateResource, Map<String, Object> variables) throws TemplateException {
    VelocityContext vc = new VelocityContext(variables);
    try {
        File templateFile = copyTemplateToFile(templateResource);
        Template template = ve.getTemplate(templateFile.getName(), "UTF-8");
        StringWriter sw = new StringWriter();
        template.merge(vc, sw);
        templateFile.deleteOnExit();
        return new ByteArrayInputStream(sw.toString().getBytes("UTF-8"));
    } catch (Exception e) {
        throw new TemplateException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 80 with Template

use of org.apache.velocity.Template in project janusgraph by JanusGraph.

the class AbstractJanusGraphAssemblyIT method parseTemplateAndRunExpect.

protected void parseTemplateAndRunExpect(String expectTemplateName, Map<String, String> contextVars) throws IOException, InterruptedException {
    VelocityContext context = new VelocityContext();
    for (Map.Entry<String, String> ent : contextVars.entrySet()) {
        context.put(ent.getKey(), ent.getValue());
    }
    Template template = Velocity.getTemplate(expectTemplateName);
    String inputPath = EXPECT_DIR + File.separator + expectTemplateName;
    String outputPath = inputPath.substring(0, inputPath.length() - 3);
    Writer output = new FileWriter(outputPath);
    template.merge(context, output);
    output.close();
    expect(ZIPFILE_EXTRACTED, outputPath);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) FileWriter(java.io.FileWriter) Writer(java.io.Writer) 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