Search in sources :

Example 11 with ResourceNotFoundException

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));
}
Also used : Path(java.nio.file.Path) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) FileVisitResult(java.nio.file.FileVisitResult) StringOutput(org.olat.core.gui.render.StringOutput) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 12 with ResourceNotFoundException

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);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) SystemException(com.github.bordertech.wcomponents.util.SystemException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) PrintWriter(java.io.PrintWriter) Template(org.apache.velocity.Template)

Example 13 with ResourceNotFoundException

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);
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Map(java.util.Map)

Example 14 with ResourceNotFoundException

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");
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ParseErrorException(org.apache.velocity.exception.ParseErrorException) FileWriter(java.io.FileWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 15 with ResourceNotFoundException

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);
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) MailException(oap.mail.MailException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)31 ParseErrorException (org.apache.velocity.exception.ParseErrorException)21 VelocityContext (org.apache.velocity.VelocityContext)17 Template (org.apache.velocity.Template)16 IOException (java.io.IOException)15 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)14 StringWriter (java.io.StringWriter)11 File (java.io.File)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 Writer (java.io.Writer)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Context (org.apache.velocity.context.Context)3 VelocityException (org.apache.velocity.exception.VelocityException)3 WComponent (com.github.bordertech.wcomponents.WComponent)2 FileInputStream (java.io.FileInputStream)2