Search in sources :

Example 11 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class JapidPlayRenderer method render.

/**
	 * a facet method to wrap implicit template binding. The default template is
	 * named as the class and method that immediately invoke this method. e.g.
	 * for an invocation scenario like this
	 * 
	 * <pre>
	 * package pack;
	 * 
	 * public class Foo {
	 * 	public String bar() {
	 * 		return JapidRender.render(p);
	 * 	}
	 * }
	 * </pre>
	 * 
	 * The template to use is "{templateRoot}/japidviews/pack/Foo/bar.html".
	 * 
	 * @param p
	 * @return
	 */
public static String render(Object... args) {
    String templateName = findTemplate();
    RenderResult r = JapidController.getRenderResultWith(templateName, args);
    return r.getText();
}
Also used : RenderResult(cn.bran.japid.template.RenderResult)

Example 12 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class RenderInvokerUtils method invokeNamedArgsRender.

public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeNamedArgsRender(Class<T> c, NamedArgRuntime[] args) {
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        StringBuilder sb = new StringBuilder(8000);
        JapidTemplateBaseWithoutPlay t = ctor.newInstance(sb);
        RenderResult rr = (RenderResult) renderWithNamedArgs(t, args);
        JapidFlags.logTimeLogs(t);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (InstantiationException e) {
        // e.printStackTrace();
        throw new RuntimeException("Could not instantiate the template object. Abstract?");
    } catch (InvocationTargetException e) {
        // e.printStackTrace();
        Throwable e1 = e.getTargetException();
        throw new RuntimeException("Could not invoke the template object:  ", e1);
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    // throw new RuntimeException(e);
    }
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class RenderInvokerUtils method invokeRender.

/**
	 * @param <T>
	 * @param c
	 * @param args
	 * @return
	 * @throws NoSuchMethodException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeRender(Class<T> c, Object... args) {
    // long start = System.nanoTime();
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        StringBuilder sb = new StringBuilder(8000);
        T t = ctor.newInstance(sb);
        RenderResult rr = (RenderResult) render(t, args);
        JapidFlags.logTimeLogs(t);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Could not match the arguments with the template: " + c.getName() + ". Reason: " + e.getMessage());
    } catch (InstantiationException e) {
        // e.printStackTrace();
        throw new RuntimeException("Could not instantiate the template object. Abstract?");
    } catch (InvocationTargetException e) {
        // e.printStackTrace();
        Throwable te = e.getTargetException();
        // if (te instanceof TemplateExecutionException)
        // throw (TemplateExecutionException) te;
        Throwable cause = te.getCause();
        if (cause != null)
            if (cause instanceof RuntimeException)
                throw (RuntimeException) cause;
            else
                throw new RuntimeException("error in running the renderer: " + cause.getMessage(), cause);
        else if (te instanceof RuntimeException)
            throw (RuntimeException) te;
        else
            throw new RuntimeException("error in running the renderer: " + te.getMessage(), te);
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    // throw new RuntimeException(e);
    } finally {
    // String howlong = StringUtils.durationInMsFromNanos(start,
    // System.nanoTime());
    // System.out.println("how long it takes to invoke invokeRender: " +
    // howlong);
    }
}
Also used : RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 14 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class JapidController method runWithCache.

/**
	 * run a piece of rendering code with cache check and refilling
	 * 
	 * @param runner
	 * @param ttl
	 * @param objects
	 * 
	 * @deprecated use CacheableRunner directly in actions
	 */
protected static void runWithCache(ActionRunner runner, String ttl, Object... objects) {
    if (ttl == null || ttl.trim().length() == 0)
        throw new RuntimeException("Cache expiration time must be defined.");
    ttl = ttl.trim();
    if (Character.isDigit(ttl.charAt(ttl.length() - 1))) {
        // assuming second
        ttl += "s";
    }
    String base = StackTraceUtils.getCaller();
    RenderResult rr = getFromCache(base, objects);
    if (rr == null) {
        rr = runner.run();
        cache(rr, ttl, base, objects);
    }
    // System.out.println("render show took ms: " + rr.getRenderTime());
    throw new JapidResult(rr);
}
Also used : RenderResult(cn.bran.japid.template.RenderResult)

Example 15 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class JapidController method renderText.

/**
	 * render a text in a RenderResult so it can work with invoke tag in
	 * templates.
	 * 
	 * @param s
	 */
protected static void renderText(String s) {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "text/plain; charset=utf-8");
    render(new RenderResult(headers, new StringBuilder(s), -1L));
}
Also used : HashMap(java.util.HashMap) RenderResult(cn.bran.japid.template.RenderResult)

Aggregations

RenderResult (cn.bran.japid.template.RenderResult)28 Test (org.junit.Test)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 InternetAddress (javax.mail.internet.InternetAddress)3 Email (org.apache.commons.mail.Email)3 EmailAttachment (org.apache.commons.mail.EmailAttachment)3 EmailException (org.apache.commons.mail.EmailException)3 HtmlEmail (org.apache.commons.mail.HtmlEmail)3 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)3 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)2 JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)2 JapidTemplateBaseWithoutPlay (cn.bran.japid.template.JapidTemplateBaseWithoutPlay)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 MailException (play.exceptions.MailException)2 UnexpectedException (play.exceptions.UnexpectedException)2