Search in sources :

Example 16 with TemplateException

use of freemarker.template.TemplateException in project SpringStepByStep by JavaProgrammerLB.

the class FreeMarkerTemplateEngine method render.

@Override
public String render(ModelAndView modelAndView) {
    try {
        StringWriter stringWriter = new StringWriter();
        Template template = configuration.getTemplate(modelAndView.getViewName());
        template.process(modelAndView.getModel(), stringWriter);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException();
    } catch (TemplateException e) {
        throw new IllegalArgumentException();
    }
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 17 with TemplateException

use of freemarker.template.TemplateException in project stanbol by apache.

the class ViewableWriter method renderPojo.

/**
     * Old school classical freemarker rendering, no LD here
     */
public void renderPojo(Object pojo, final String templatePath, Writer out) {
    Configuration freemarker = new Configuration();
    freemarker.setDefaultEncoding("utf-8");
    freemarker.setOutputEncoding("utf-8");
    freemarker.setLocalizedLookup(false);
    freemarker.setObjectWrapper(new DefaultObjectWrapper());
    freemarker.setTemplateLoader(templateLoader);
    try {
        //should root be a map instead?
        freemarker.getTemplate(templatePath).process(pojo, out);
        out.flush();
    } catch (IOException e) {
        throw new RuntimeException("IOException while processing Template '" + templatePath + "' with Object '" + pojo + "' (class: " + pojo != null ? pojo.getClass().getName() : null + ")!", e);
    } catch (TemplateException e) {
        throw new RuntimeException(e);
    }
}
Also used : Configuration(freemarker.template.Configuration) TemplateException(freemarker.template.TemplateException) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) IOException(java.io.IOException)

Example 18 with TemplateException

use of freemarker.template.TemplateException in project engine by craftercms.

the class ExecuteControllerDirective method executeController.

protected void executeController(String path, Environment env) throws TemplateException {
    Map<String, Object> scriptVariables = createScriptVariables(env);
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        ScriptFactory scriptFactory = siteContext.getScriptFactory();
        if (scriptFactory == null) {
            throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
        }
        Script script;
        try {
            script = scriptFactory.getScript(path);
        } catch (Exception e) {
            throw new TemplateException("Unable to load controller at '" + path + "'", e, env);
        }
        executeController(script, scriptVariables, env);
    } else {
        throw new IllegalStateException("No current site context found");
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) TemplateException(freemarker.template.TemplateException) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 19 with TemplateException

use of freemarker.template.TemplateException in project engine by craftercms.

the class RenderComponentDirective method getComponent.

protected SiteItem getComponent(String componentPath, Environment env) throws TemplateException {
    SiteItem currentPage = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
    if (currentPage != null) {
        String basePath = FilenameUtils.getFullPath(currentPage.getStoreUrl());
        URI baseUri = URI.create(basePath);
        try {
            componentPath = baseUri.resolve(componentPath).toString();
        } catch (IllegalArgumentException e) {
            throw new TemplateException("Invalid relative component URL " + componentPath, e, env);
        }
    }
    SiteItem component;
    try {
        component = siteItemService.getSiteItem(componentPath);
    } catch (Exception e) {
        throw new TemplateException("Unable to load component " + componentPath + " from the underlying repository", e, env);
    }
    if (component == null) {
        throw new TemplateException("No component found at path '" + componentPath + "'", env);
    }
    return component;
}
Also used : TemplateException(freemarker.template.TemplateException) URI(java.net.URI) SiteItem(org.craftercms.engine.model.SiteItem) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 20 with TemplateException

use of freemarker.template.TemplateException in project moco by dreamhead.

the class TemplateResourceReader method readFor.

@Override
public MessageContent readFor(final Optional<? extends Request> request) {
    if (!request.isPresent()) {
        throw new IllegalStateException("Request is required to render template");
    }
    MessageContent content = this.template.readFor(request);
    try {
        Template targetTemplate = createTemplate(content);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(stream);
        targetTemplate.process(variables(request.get()), writer);
        return content().withContent(stream.toByteArray()).build();
    } catch (ParseException e) {
        logger.error("Fail to parse template: {}", content.toString());
        throw new MocoException(e);
    } catch (IOException e) {
        throw new MocoException(e);
    } catch (TemplateException e) {
        throw new MocoException(e);
    }
}
Also used : MessageContent(com.github.dreamhead.moco.model.MessageContent) TemplateException(freemarker.template.TemplateException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(freemarker.core.ParseException) MocoException(com.github.dreamhead.moco.MocoException) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

TemplateException (freemarker.template.TemplateException)39 IOException (java.io.IOException)34 Template (freemarker.template.Template)20 HashMap (java.util.HashMap)16 Writer (java.io.Writer)15 StringWriter (java.io.StringWriter)12 Map (java.util.Map)8 Configuration (freemarker.template.Configuration)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)6 File (java.io.File)5 OutputStreamWriter (java.io.OutputStreamWriter)4 MediaType (org.restlet.data.MediaType)4 Representation (org.restlet.representation.Representation)4 BufferedWriter (java.io.BufferedWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileWriter (java.io.FileWriter)3 JSONException (org.json.JSONException)3 StringRepresentation (org.restlet.representation.StringRepresentation)3 ParseException (freemarker.core.ParseException)2 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)2