Search in sources :

Example 6 with MethodInvocationException

use of org.apache.velocity.exception.MethodInvocationException in project dbflute-core by dbflute.

the class DfAbstractTexenTask method fireVelocityProcess.

protected void fireVelocityProcess() {
    assertBasicAntParameter();
    // set up the encoding of templates from DBFlute property
    setInputEncoding(getBasicProperties().getTemplateFileEncoding());
    setOutputEncoding(getBasicProperties().getSourceFileEncoding());
    try {
        initializeGeneratorInstance();
        final DfGenerator generator = setupGenerator();
        final Context ctx = setupControlContext();
        _log.info("generator.parse(\"" + controlTemplate + "\", ctx);");
        generator.parse(controlTemplate, ctx);
        generator.shutdown();
        cleanup();
    } catch (BuildException e) {
        throw e;
    } catch (MethodInvocationException e) {
        final String method = e.getReferenceName() + "." + e.getMethodName() + "()";
        String msg = "Exception thrown by " + method + ": control=" + controlTemplate;
        throw new IllegalStateException(msg, e.getWrappedThrowable());
    } catch (ParseErrorException e) {
        throw new IllegalStateException("Velocity syntax error: control=" + controlTemplate, e);
    } catch (ResourceNotFoundException e) {
        throw new IllegalStateException("Resource not found: control=" + controlTemplate, e);
    } catch (Exception e) {
        throw new IllegalStateException("Generation failed: control=" + controlTemplate, e);
    }
}
Also used : Context(org.apache.velocity.context.Context) ParseErrorException(org.apache.velocity.exception.ParseErrorException) BuildException(org.apache.tools.ant.BuildException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) DfGenerator(org.dbflute.friends.velocity.DfGenerator) SQLException(java.sql.SQLException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 7 with MethodInvocationException

use of org.apache.velocity.exception.MethodInvocationException in project dbflute-core by dbflute.

the class DfFlutistGenerator method throwTemplateParsingException.

protected void throwTemplateParsingException(String inputTemplate, String specifiedInputEncoding, Throwable e) {
    rethrowIfNestedException(inputTemplate, specifiedInputEncoding, e);
    final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
    br.addNotice("Failed to parse the input template.");
    br.addItem("Input Template");
    br.addElement(inputTemplate + " (" + specifiedInputEncoding + ")");
    final Throwable cause;
    if (e instanceof MethodInvocationException) {
        cause = ((MethodInvocationException) e).getWrappedThrowable();
    } else {
        cause = e;
    }
    final String msg = br.buildExceptionMessage();
    throw new DfTemplateParsingException(msg, cause);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) DfTemplateParsingException(org.dbflute.exception.DfTemplateParsingException)

Example 8 with MethodInvocationException

use of org.apache.velocity.exception.MethodInvocationException 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 9 with MethodInvocationException

use of org.apache.velocity.exception.MethodInvocationException 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)

Example 10 with MethodInvocationException

use of org.apache.velocity.exception.MethodInvocationException in project OpenOLAT by OpenOLAT.

the class VelocityHelper method merge.

/**
 * @param template e.g. org/olat/demo/_content/index.html
 * @param c the context
 * @param theme the theme e.g. "accessibility" or "printing". may be null if the default theme ("") should be taken
 * @return String the rendered template
 */
private void merge(String template, Context c, Writer wOut, String theme) {
    try {
        Template vtemplate = null;
        if (isLogDebugEnabled())
            logDebug("Merging template::" + template + " for theme::" + theme, null);
        if (theme != null) {
            // try the theme first, if resource not found exception, fallback to normal resource.
            // e.g. try /_accessibility/index.html first, if not found, try /index.html.
            // this allows for themes to only provide the delta to the default templates
            // todo we could avoid those string operations, if the performance gain is measureable
            int latestSlash = template.lastIndexOf('/');
            StringBuilder sb = new StringBuilder(template.substring(0, latestSlash));
            sb.append("/_").append(theme).append("/").append(template.substring(latestSlash + 1));
            String themedTemplatePath = sb.toString();
            // check cache
            boolean notFound = resourcesNotFound.contains(themedTemplatePath);
            if (!notFound) {
                // never tried before -> try to load it
                if (!ve.resourceExists(themedTemplatePath)) {
                    // this will happen once for each theme when a resource does not exist in its themed variant but only in the default theme.
                    if (!Settings.isDebuging()) {
                        resourcesNotFound.add(themedTemplatePath);
                    }
                // for debugging, allow introduction of themed files without restarting the application
                } else {
                    // template exists -> load it
                    vtemplate = ve.getTemplate(themedTemplatePath, VelocityModule.getInputEncoding());
                }
            }
            // if not found, fallback to standard
            if (vtemplate == null) {
                vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
            }
        } else {
            // no theme, load the standard template
            vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
        }
        vtemplate.merge(c, wOut);
    } catch (MethodInvocationException me) {
        throw new OLATRuntimeException(VelocityHelper.class, "MethodInvocationException occured while merging template: methName:" + me.getMethodName() + ", refName:" + me.getReferenceName(), me.getWrappedThrowable());
    } catch (Exception e) {
        throw new OLATRuntimeException(VelocityHelper.class, "exception occured while merging template: " + e.getMessage(), e);
    }
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) AssertException(org.olat.core.logging.AssertException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Template(org.apache.velocity.Template)

Aggregations

MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)18 ParseErrorException (org.apache.velocity.exception.ParseErrorException)13 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)13 IOException (java.io.IOException)9 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)8 StringWriter (java.io.StringWriter)7 FileOutputStream (java.io.FileOutputStream)3 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 Writer (java.io.Writer)2 DecimalFormat (java.text.DecimalFormat)2 Properties (java.util.Properties)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 NumberTool (org.apache.velocity.tools.generic.NumberTool)2 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)1 WComponent (com.github.bordertech.wcomponents.WComponent)1