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();
}
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);
}
}
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);
}
}
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);
}
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));
}
Aggregations