/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.wisdom.template.thymeleaf.impl;

import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wisdom.api.Controller;
import org.wisdom.api.asset.Assets;
import org.wisdom.api.bodies.RenderableString;
import org.wisdom.api.http.MimeTypes;
import org.wisdom.api.router.Router;
import org.wisdom.api.templates.Template;
import org.wisdom.template.thymeleaf.dialect.Routes;
import org.wisdom.template.thymeleaf.dialect.WisdomStandardDialect;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 * The main integration point of Thymeleaf in wisdom.
 */
public class WisdomTemplateEngine extends TemplateEngine {

    public WisdomTemplateEngine() {
        super();
        // We clear the dialects as we are using our own standard dialect.
        clearDialects();
        addDialect(new WisdomStandardDialect());
        addDialect(new LayoutDialect());
    }

    /**
     * Renders the given template.
     * <p/>
     * Variables from the session, flash and request parameters are added to the given parameters.
     *
     * @param template   the template
     * @param controller the template asking for the rendering
     * @param router     the router service
     * @param variables  the template parameters
     * @return the rendered HTML page
     */
    public RenderableString process(Template template, Controller controller, Router router, Assets assets, Map<String,
            Object> variables) {
        Context ctx = new Context();
        // Add session
        ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().session().getData());
        // Add flash
        ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().flash().getCurrentFlashCookieData());
        ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().flash().getOutgoingFlashCookieData());

        // Add parameter from request, flattened
        for (Map.Entry<String, List<String>> entry : org.wisdom.api.http.Context.CONTEXT.get()
                .parameters().entrySet()) {
            if (entry.getValue().size() == 1) {
                ctx.setVariable(entry.getKey(), entry.getValue().get(0));
            } else {
                ctx.setVariable(entry.getKey(), entry.getValue());
            }
        }

        // Add variable.
        ctx.setVariables(variables);
        ctx.setVariable(Routes.ROUTES_VAR, new Routes(router, assets, controller));

        StringWriter writer = new StringWriter();
        this.process(template.fullName(), ctx, writer);
        return new RenderableString(writer, MimeTypes.HTML);
    }

}

@@ -816,16 +816,77 @@
 ontext;%0A
+import org.thymeleaf.exceptions.TemplateProcessingException;%0A
 import o
@@ -907,24 +907,24 @@
 Controller;%0A
-
 import org.w
@@ -1732,17 +1732,16 @@
     * %3Cp
-/
 %3E%0A     *
@@ -3231,24 +3231,42 @@
 ngWriter();%0A
+        try %7B%0A    
         this
@@ -3309,16 +3309,603 @@
 riter);%0A
+        %7D catch (TemplateProcessingException e) %7B%0A            // If we have a nested cause having a nested cause, heuristics say that it's the useful message.%0A            // Rebuild an exception using this data.%0A            if (e.getCause() != null && e.getCause().getCause() != null) %7B%0A                throw new TemplateProcessingException(e.getCause().getCause().getMessage(),%0A                        e.getTemplateName(),%0A                        e.getLineNumber(),%0A                        e.getCause().getCause());%0A            %7D else %7B%0A                throw e;%0A            %7D%0A        %7D%0A
         

Improve thymeleaf template error