use of org.apache.velocity.Template in project gerrit by GerritCodeReview.
the class OutgoingEmail method velocifyFile.
protected String velocifyFile(String name) throws EmailException {
try {
RuntimeInstance runtime = args.velocityRuntime;
if (runtime.getLoaderNameForResource(name) == null) {
name = "com/google/gerrit/server/mail/" + name;
}
Template template = runtime.getTemplate(name, UTF_8.name());
StringWriter w = new StringWriter();
template.merge(velocityContext, w);
return w.toString();
} catch (Exception e) {
throw new EmailException("Cannot format velocity template " + name, e);
}
}
use of org.apache.velocity.Template in project stanbol by apache.
the class BenchmarkServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final String path = request.getPathInfo() == null ? "" : request.getPathInfo();
if (path.endsWith(".css")) {
// Serve our css
final Template t = getTemplate("/velocity/benchmark.css");
response.setContentType("text/css");
response.setCharacterEncoding("UTF-8");
t.merge(getVelocityContext(request, null), response.getWriter());
} else if (path.length() < 2) {
// No benchmark specified -> redirect to default
response.sendRedirect(getExampleBenchmarkPath(request, DEFAULT_BENCHMARK));
} else {
// Benchmark input form pre-filled with selected example
final Template t = getTemplate("/velocity/benchmark-input.html");
final VelocityContext ctx = getVelocityContext(request, "Benchmark Input");
ctx.put("formAction", request.getContextPath() + mountPath);
ctx.put("benchmarkText", getBenchmarkText(path));
ctx.put("benchmarkPaths", getExampleBenchmarkPaths(request));
ctx.put("currentBenchmarkPath", path);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
t.merge(ctx, response.getWriter());
}
}
use of org.apache.velocity.Template in project stanbol by apache.
the class BenchmarkServlet method doPost.
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final String content = request.getParameter(PARAM_CONTENT);
if (content == null) {
throw new ServletException("Missing " + PARAM_CONTENT + " parameter");
}
String chainName = request.getParameter(PARAM_CHAIN);
final Template t = AccessController.doPrivileged(new PrivilegedAction<Template>() {
@Override
public Template run() {
return getTemplate("/velocity/benchmark-results.html");
}
});
final VelocityContext ctx = getVelocityContext(request, "Benchmark Results");
ctx.put("contentItemFactory", ciFactory);
ctx.put("jobManager", jobManager);
List<? extends Benchmark> benchmarks = parser.parse(new StringReader(content));
if (chainName != null && !chainName.isEmpty()) {
Chain chain = chainManager.getChain(chainName);
if (chain == null) {
response.setStatus(404);
PrintWriter w = response.getWriter();
w.println("Unable to perform benchmark on EnhancementChain '" + StringEscapeUtils.escapeHtml(chainName) + "' because no chain with that name is active!");
IOUtils.closeQuietly(w);
return;
}
for (Benchmark benchmark : benchmarks) {
benchmark.setChain(chain);
}
}
ctx.put("benchmarks", benchmarks);
ctx.put("graphFormatter", new GraphFormatter(graphSerializer));
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws IOException {
t.merge(ctx, response.getWriter());
return null;
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (e instanceof IOException) {
throw (IOException) e;
} else {
throw RuntimeException.class.cast(e);
}
}
}
use of org.apache.velocity.Template in project tomee by apache.
the class Container method copyTemplateTo.
private void copyTemplateTo(final File targetDir, final String filename) throws Exception {
final File file = new File(targetDir, filename);
if (file.exists()) {
return;
}
// don't break apps using Velocity facade
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
engine.setProperty(Velocity.RESOURCE_LOADER, "class");
engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename);
final VelocityContext context = new VelocityContext();
context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
final Writer writer = new FileWriter(file);
template.merge(context, writer);
writer.flush();
writer.close();
}
use of org.apache.velocity.Template in project ngAndroid by davityle.
the class SourceCreator method createSourceFiles.
public void createSourceFiles() {
Properties props = new Properties();
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SystemLogChute");
props.setProperty("resource.loader", "classpath");
props.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine ve = new VelocityEngine(props);
ve.init();
Template vtModel = ve.getTemplate("templates/ngmodel.vm");
Template vtScope = ve.getTemplate("templates/scope.vm");
Template vtLayout = ve.getTemplate("templates/layout.vm");
for (NgModelSourceLink ms : modelSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(ms.getSourceFileName(), ms.getElements());
Writer writer = jfo.openWriter();
vtModel.merge(ms.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.of(ms.getElements()[0]), e.getMessage());
}
}
for (ScopeSourceLink ss : scopeSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(ss.getSourceFileName(), ss.getElements());
Writer writer = jfo.openWriter();
vtScope.merge(ss.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.of(ss.getElements()[0]), e.getMessage());
}
}
for (LayoutSourceLink lsl : layoutSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(lsl.getSourceFileName(), lsl.getElements());
Writer writer = jfo.openWriter();
vtLayout.merge(lsl.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.<Element>absent(), e.getMessage());
}
}
}
Aggregations