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;
}
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;
}
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");
}
}
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);
}
}
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);
}
Aggregations