Search in sources :

Example 1 with TemplateNotFoundException

use of freemarker.template.TemplateNotFoundException in project ninja by ninjaframework.

the class TemplateEngineFreemarker method throwRenderingException.

public void throwRenderingException(Context context, Result result, Exception cause, String knownTemplateSourcePath) {
    // a more useful ParseException
    if (cause instanceof IOException && cause.getCause() != null && cause.getCause() instanceof ParseException) {
        cause = (ParseException) cause.getCause();
    }
    if (cause instanceof TemplateNotFoundException) {
        // inner cause will be better to display
        throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker template not found", knownTemplateSourcePath, -1);
    } else if (cause instanceof TemplateException) {
        TemplateException te = (TemplateException) cause;
        String templateSourcePath = te.getTemplateSourceName();
        if (templateSourcePath == null) {
            templateSourcePath = knownTemplateSourcePath;
        }
        throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker render exception", templateSourcePath, te.getLineNumber());
    } else if (cause instanceof ParseException) {
        ParseException pe = (ParseException) cause;
        String templateSourcePath = pe.getTemplateName();
        if (templateSourcePath == null) {
            templateSourcePath = knownTemplateSourcePath;
        }
        throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker parser exception", templateSourcePath, pe.getLineNumber());
    }
    // fallback to throwing generic rendering exception
    throw new RenderingException(cause.getMessage(), cause, result, knownTemplateSourcePath, -1);
}
Also used : TemplateException(freemarker.template.TemplateException) RenderingException(ninja.exceptions.RenderingException) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) IOException(java.io.IOException) ParseException(freemarker.core.ParseException)

Example 2 with TemplateNotFoundException

use of freemarker.template.TemplateNotFoundException in project greenDAO by greenrobot.

the class DaoGenerator method getConfiguration.

private Configuration getConfiguration(String probingTemplate) throws IOException {
    Configuration config = new Configuration(Configuration.VERSION_2_3_23);
    config.setClassForTemplateLoading(getClass(), "/");
    try {
        config.getTemplate(probingTemplate);
    } catch (TemplateNotFoundException e) {
        // When running from an IDE like IntelliJ, class loading resources may fail for some reason (Gradle is OK)
        // Working dir is module dir
        File dir = new File("src/main/resources/");
        if (!dir.exists()) {
            // Working dir is base module dir
            dir = new File("DaoGenerator/src/main/resources/");
        }
        if (dir.exists() && new File(dir, probingTemplate).exists()) {
            config.setDirectoryForTemplateLoading(dir);
            config.getTemplate(probingTemplate);
        } else {
            throw e;
        }
    }
    return config;
}
Also used : Configuration(freemarker.template.Configuration) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) File(java.io.File)

Aggregations

TemplateNotFoundException (freemarker.template.TemplateNotFoundException)2 ParseException (freemarker.core.ParseException)1 Configuration (freemarker.template.Configuration)1 TemplateException (freemarker.template.TemplateException)1 File (java.io.File)1 IOException (java.io.IOException)1 RenderingException (ninja.exceptions.RenderingException)1