use of org.apache.velocity.exception.ResourceNotFoundException in project openolat by klemens.
the class VelocityTemplatesPreWarm method run.
@Override
public void run() {
long start = System.nanoTime();
log.info("Start filling the velocity template cache");
final VelocityContext context = new VelocityContext();
final AtomicInteger numOfTemplates = new AtomicInteger(0);
final File root = new File(WebappHelper.getContextRoot(), "WEB-INF/classes");
final Path fPath = root.toPath();
try {
if (Files.exists(fPath)) {
Files.walkFileTree(fPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
String path = fPath.relativize(file).toString();
if (path.endsWith(".html") && path.contains("/_content/")) {
StringOutput writer = new StringOutput();
VelocityHelper.getInstance().mergeContent(path, context, writer, null);
numOfTemplates.incrementAndGet();
}
} catch (ResourceNotFoundException e) {
log.error("", e);
} catch (ParseErrorException e) {
log.error("", e);
}
return FileVisitResult.CONTINUE;
}
});
}
} catch (IOException e) {
log.error("", e);
}
log.info("Velocity cache filled with " + numOfTemplates + " templates in (ms): " + CodeHelper.nanoToMilliTime(start));
}
use of org.apache.velocity.exception.ResourceNotFoundException in project wcomponents by BorderTech.
the class VelocityInterceptor method paint.
/**
* Renders the component using the velocity template which has been provided.
*
* @param renderContext the context for rendering.
*/
@Override
public void paint(final RenderContext renderContext) {
if (!(renderContext instanceof WebXmlRenderContext)) {
throw new SystemException("Unable to render to " + renderContext);
}
PrintWriter writer = ((WebXmlRenderContext) renderContext).getWriter();
Template template = null;
try {
template = VelocityEngineFactory.getVelocityEngine().getTemplate(templateUrl);
} catch (Exception ex) {
String message = "Could not open velocity template \"" + templateUrl + "\" for \"" + this.getClass().getName() + "\"";
LOG.error(message, ex);
writer.println(message);
return;
}
try {
VelocityContext context = new VelocityContext();
fillContext(context);
template.merge(context, writer);
} catch (ResourceNotFoundException rnfe) {
LOG.error("Could not find template " + templateUrl, rnfe);
} catch (ParseErrorException pee) {
// syntax error : problem parsing the template
LOG.error("Parse problems", pee);
} catch (MethodInvocationException mie) {
// something invoked in the template
// threw an exception
Throwable wrapped = mie.getWrappedThrowable();
LOG.error("Problems with velocity", mie);
if (wrapped != null) {
LOG.error("Wrapped exception...", wrapped);
}
} catch (Exception e) {
LOG.error("Problems with velocity", e);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project wcomponents by BorderTech.
the class VelocityRendererImpl method renderTemplate.
/**
* {@inheritDoc}
*/
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering velocity template [" + templateName + "].");
// Velocity uses a ClassLoader so dont use an absolute path.
String name = templateName.startsWith("/") ? templateName.substring(1) : templateName;
try {
// Load template
Template template = getVelocityEngine().getTemplate(name);
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Setup context
VelocityContext velocityContext = new VelocityContext();
for (Map.Entry<String, Object> entry : context.entrySet()) {
velocityContext.put(entry.getKey(), entry.getValue());
}
// Write template
UIContext uic = UIContextHolder.getCurrent();
try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
template.merge(velocityContext, velocityWriter);
}
} catch (ResourceNotFoundException e) {
throw new SystemException("Could not find velocity template [" + templateName + "]. " + e.getMessage(), e);
} catch (Exception e) {
throw new SystemException("Problems with velocity template [" + templateName + "]. " + e.getMessage(), e);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project bazel by bazelbuild.
the class Page method write.
/**
* Renders the template and writes the output to the given file.
*
* Strips all trailing whitespace before writing to file.
*/
public void write(File outputFile) throws IOException {
StringWriter stringWriter = new StringWriter();
try {
engine.mergeTemplate(template, "UTF-8", context, stringWriter);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
stringWriter.close();
String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
try (FileWriter fileWriter = new FileWriter(outputFile)) {
for (String line : lines) {
// Strip trailing whitespace then append newline before writing to file.
fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
}
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project oap by oaplatform.
the class VelocityTemplateTransformer method transform.
public synchronized String transform(Template template) {
try {
VelocityContext context = new VelocityContext();
Map<String, Object> parameters = template.getParameters();
for (String key : parameters.keySet()) context.put(key, parameters.get(key));
StringWriter writer = new StringWriter();
engine.evaluate(context, writer, "mail", template.getContent());
writer.close();
return writer.toString();
} catch (ParseErrorException | MethodInvocationException | ResourceNotFoundException | IOException e) {
throw new MailException(e);
}
}
Aggregations