Search in sources :

Example 1 with TemplateException

use of org.craftercms.core.exception.TemplateException in project core by craftercms.

the class FreeMarkerStringTemplateCompiler method compile.

public CompiledTemplate compile(IdentifiableStringTemplateSource templateSource) throws TemplateException {
    // Replace '/' with any other char because Freemarker messes up IDs with '/' because it thinks they're paths.
    String id = templateSource.getId().replace('/', ';');
    String source = templateSource.getSource();
    StringTemplateSource currentTemplateSource = (StringTemplateSource) templateLoader.findTemplateSource(id);
    if (currentTemplateSource == null || !currentTemplateSource.getSource().equals(source)) {
        templateLoader.putTemplateSource(id, source);
    }
    try {
        return new FreeMarkerCompiledTemplate(templateConfiguration.getTemplate(id));
    } catch (Exception e) {
        throw new TemplateException("Unable to compile Freemarker template:\n" + source, e);
    }
}
Also used : TemplateException(org.craftercms.core.exception.TemplateException) StringTemplateSource(org.craftercms.core.util.template.impl.freemarker.ConcurrentStringTemplateLoader.StringTemplateSource) IdentifiableStringTemplateSource(org.craftercms.core.util.template.impl.IdentifiableStringTemplateSource) TemplateException(org.craftercms.core.exception.TemplateException)

Example 2 with TemplateException

use of org.craftercms.core.exception.TemplateException in project core by craftercms.

the class SpELStringTemplateCompiler method compile.

@Override
public CompiledTemplate compile(IdentifiableStringTemplateSource templateSource) throws TemplateException {
    String id = templateSource.getId();
    String source = templateSource.getSource();
    try {
        Expression expression = expressionCache.get(id);
        if (expression == null || !expression.getExpressionString().equals(source)) {
            expression = parser.parseExpression(source, parserContext);
            expressionCache.put(id, expression);
        }
        return new SpElCompiledTemplate(expression, evalContext);
    } catch (Exception e) {
        throw new TemplateException("Unable to compile SpEL template:\n" + source, e);
    }
}
Also used : Expression(org.springframework.expression.Expression) TemplateException(org.craftercms.core.exception.TemplateException) TemplateException(org.craftercms.core.exception.TemplateException) BeansException(org.springframework.beans.BeansException)

Example 3 with TemplateException

use of org.craftercms.core.exception.TemplateException in project core by craftercms.

the class SpElCompiledTemplate method process.

@Override
public void process(Object model, Writer output) throws TemplateException {
    try {
        String result = expression.getValue(evaluationContext, model, String.class);
        output.write(result);
        output.flush();
    } catch (IOException e) {
        throw new TemplateException("An I/O error occurred while writing to output", e);
    } catch (Exception e) {
        throw new TemplateException("Unable to process SpEL template:\n" + expression.getExpressionString(), e);
    }
}
Also used : TemplateException(org.craftercms.core.exception.TemplateException) IOException(java.io.IOException) TemplateException(org.craftercms.core.exception.TemplateException) IOException(java.io.IOException)

Example 4 with TemplateException

use of org.craftercms.core.exception.TemplateException in project core by craftercms.

the class TemplateProcessor method process.

/**
 * Processes the content of certain nodes (found by the {@code NodeScanner} in the item's descriptor as templates,
 * by compiling the node text templates through the {@code templateCompiler} and then processing the compiled
 * template with a model returned by {@code modelFactory}.
 *
 * @throws ItemProcessingException if an error occurred while processing a template
 */
public Item process(Context context, CachingOptions cachingOptions, Item item) throws ItemProcessingException {
    String descriptorUrl = item.getDescriptorUrl();
    Document descriptorDom = item.getDescriptorDom();
    if (descriptorDom != null) {
        List<Node> templateNodes = templateNodeScanner.scan(descriptorDom);
        if (CollectionUtils.isNotEmpty(templateNodes)) {
            for (Node templateNode : templateNodes) {
                String templateNodePath = templateNode.getUniquePath();
                if (logger.isDebugEnabled()) {
                    logger.debug("Template found in " + descriptorUrl + " at " + templateNodePath);
                }
                String templateId = templateNodePath + "@" + descriptorUrl;
                String template = templateNode.getText();
                IdentifiableStringTemplateSource templateSource = new IdentifiableStringTemplateSource(templateId, template);
                Object model = modelFactory.getModel(item, templateNode, template);
                StringWriter output = new StringWriter();
                try {
                    CompiledTemplate compiledTemplate = templateCompiler.compile(templateSource);
                    compiledTemplate.process(model, output);
                } catch (TemplateException e) {
                    throw new ItemProcessingException("Unable to process the template " + templateId, e);
                }
                templateNode.setText(output.toString());
            }
        }
    }
    return item;
}
Also used : StringWriter(java.io.StringWriter) TemplateException(org.craftercms.core.exception.TemplateException) Node(org.dom4j.Node) IdentifiableStringTemplateSource(org.craftercms.core.util.template.impl.IdentifiableStringTemplateSource) ItemProcessingException(org.craftercms.core.exception.ItemProcessingException) Document(org.dom4j.Document) CompiledTemplate(org.craftercms.core.util.template.CompiledTemplate)

Aggregations

TemplateException (org.craftercms.core.exception.TemplateException)4 IdentifiableStringTemplateSource (org.craftercms.core.util.template.impl.IdentifiableStringTemplateSource)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ItemProcessingException (org.craftercms.core.exception.ItemProcessingException)1 CompiledTemplate (org.craftercms.core.util.template.CompiledTemplate)1 StringTemplateSource (org.craftercms.core.util.template.impl.freemarker.ConcurrentStringTemplateLoader.StringTemplateSource)1 Document (org.dom4j.Document)1 Node (org.dom4j.Node)1 BeansException (org.springframework.beans.BeansException)1 Expression (org.springframework.expression.Expression)1