Search in sources :

Example 36 with TemplateException

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

the class LdRenderer method render.

/**
	 * Renders a GraphNode with a template located in the templates
	 * folder of any active bundle
	 * 
	 * @param node the GraphNode to be rendered
	 * @param templatePath the freemarker path to the template
	 * @param out where the result is written to
	 */
public void render(GraphNode node, final String templatePath, Writer out) {
    //A GraphNode backend could be graph unspecific, so the same engine could be
    //reused, possibly being signifantly more performant (caching, etc.)
    RDFBackend<RDFTerm> backend = new ClerezzaBackend(node.getGraph());
    RDFTerm context = node.getNode();
    TemplateEngine<RDFTerm> engine = new TemplateEngine<RDFTerm>(backend);
    engine.setTemplateLoader(templateLoader);
    try {
        engine.processFileTemplate(context, templatePath, null, out);
        out.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (TemplateException e) {
        throw new RuntimeException(e);
    }
}
Also used : TemplateEngine(org.apache.marmotta.ldpath.template.engine.TemplateEngine) TemplateException(freemarker.template.TemplateException) ClerezzaBackend(org.apache.stanbol.commons.ldpath.clerezza.ClerezzaBackend) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) IOException(java.io.IOException)

Example 37 with TemplateException

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

the class RenderComponentDirective method executeScripts.

protected Map<String, Object> executeScripts(SiteItem component, Map<String, Object> additionalModel, Environment env) throws TemplateException {
    List<String> scriptUrls = scriptResolver.getScriptUrls(component);
    if (CollectionUtils.isNotEmpty(scriptUrls)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Scripts associated to component " + component.getStoreUrl() + ": " + scriptUrls);
        }
        Map<String, Object> templateModel = new HashMap<>();
        Map<String, Object> scriptVariables = createScriptVariables(component, templateModel, additionalModel);
        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() + "'");
            }
            for (String scriptUrl : scriptUrls) {
                Script script;
                try {
                    script = scriptFactory.getScript(scriptUrl);
                } catch (Exception e) {
                    throw new TemplateException("Unable to load script at '" + scriptUrl + "'", e, env);
                }
                executeScript(script, scriptVariables, env);
            }
        } else {
            throw new IllegalStateException("No current site context found");
        }
        return templateModel;
    } else {
        return null;
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 38 with TemplateException

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

the class RenderComponentDirective method execute.

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;
    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }
    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }
    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();
    processComponentTemplate(template, model, output, env);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 39 with TemplateException

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

the class CrafterTemplateExceptionHandler method handleTemplateException.

@Override
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
    if (displayTemplateExceptionsInView) {
        String error = ERROR_FORMAT.replace("{errorId}", createErrorId());
        error = error.replace("{error}", getExceptionStackTrace(te));
        try {
            out.write(error);
        } catch (IOException e) {
            throw new TemplateException("Failed to print error. Cause: " + e, env);
        }
    }
}
Also used : TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

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