/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hudson.plugins.emailext;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.content.ScriptContent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.kohsuke.stapler.bind.JavaScriptMethod;

/**
 *
 * @author acearl
 */
public class EmailExtTemplateAction implements Action {
    
    private AbstractProject<?,?> project;

    public EmailExtTemplateAction(AbstractProject<?,?> project) {
        this.project = project;        
    }
    
    public String getIconFileName() {
        return "/plugin/email-ext/images/template-debugger.png";
    }

    public String getDisplayName() {
        return Messages.EmailExtTemplateAction_DisplayName();
    }

    public String getUrlName() {
        return "templateTest";
    }   
    
    private String renderError(Exception ex) {
        return "";
    }
    
    @JavaScriptMethod
    public String renderTemplate(String templateFile, String buildId) {
        String result;
        
        try {
            ScriptContent content = new ScriptContent();
            content.template = templateFile;
            AbstractBuild<?,?> build = project.getBuild(buildId);
            result = content.evaluate(build, TaskListener.NULL, "SCRIPT");
        } catch (Exception ex) {
            result = renderError(ex);
        } 
        
        return result;
    }
    
    public AbstractProject<?, ?> getProject() {
        return project;
    }
}

@@ -257,24 +257,91 @@
 skListener;%0A
+import hudson.plugins.emailext.plugins.content.JellyScriptContent;%0A
 import hudso
@@ -1203,17 +1203,344 @@
     
-return %22%22
+StringBuilder builder = new StringBuilder();%0A        builder.append(%22%3Ch3%3EAn error occured trying to render the template:%3C/h3%3E%3Cbr/%3E%22);%0A        builder.append(%22%3Cspan style=%5C%22color:red; font-weight:bold%5C%22%3E%22);%0A        builder.append(ex.toString().replace(%22%5Cn%22, %22%3Cbr/%3E%22));%0A        builder.append(%22%3C/span%3E%22);%0A        return builder.toString()
 ;%0A  
@@ -1700,16 +1700,141 @@
         
+AbstractBuild%3C?,?%3E build = project.getBuild(buildId);%0A            if(templateFile.endsWith(%22.jelly%22)) %7B%0A                Jelly
 ScriptCo
@@ -1839,17 +1839,22 @@
 Content 
-c
+jellyC
 ontent =
@@ -1858,16 +1858,21 @@
 t = new 
+Jelly
 ScriptCo
@@ -1892,17 +1892,26 @@
         
-c
+    jellyC
 ontent.t
@@ -1950,62 +1950,247 @@
     
-AbstractBuild%3C?,?%3E build = project.getBuild(buildId);%0A
+    result = jellyContent.evaluate(build, TaskListener.NULL, %22JELLY_SCRIPT%22);%0A            %7D else %7B%0A                ScriptContent scriptContent = new ScriptContent();%0A                scriptContent.template = templateFile;                %0A  
     
@@ -2189,32 +2189,34 @@
  %0A              
+  
 result = content
@@ -2208,17 +2208,23 @@
 esult = 
-c
+scriptC
 ontent.e
@@ -2260,32 +2260,46 @@
 ULL, %22SCRIPT%22);%0A
+            %7D%0A
         %7D catch 

Add support for jelly template testing