use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.
the class JapidPlayRenderer method newRendererClass.
/**
* @param c
* @return
*/
static RendererClass newRendererClass(String c) {
RendererClass rc = new RendererClass();
rc.setClassName(c);
// the source code of the Java file might not be available yet
// rc.setSourceCode(getSouceCode(c));
rc.setLastUpdated(0);
return rc;
}
use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.
the class JapidPlainController method getRenderResultWith.
/**
* render parameters to the prescribed template and return the RenderResult
*
* @param template
* relative path from japidviews folder. if empty, use implicit
* naming pattern to match the template
* @param args
*/
public static String getRenderResultWith(String template, Object... args) {
checkJapidInit();
if (template == null || template.length() == 0) {
template = template();
}
if (template.endsWith(HTML)) {
template = template.substring(0, template.length() - HTML.length());
}
String templateClassName = template.startsWith(JAPIDVIEWS_ROOT) ? template : JAPIDVIEWS_ROOT + File.separator + template;
templateClassName = templateClassName.replace('/', DOT).replace('\\', DOT);
Class<? extends JapidTemplateBaseWithoutPlay> tClass = null;
// tClass = JapidRenderer.getClass(templateClassName);
//
// if (tClass == null) {
// String templateFileName = templateClassName.replace(DOT, '/') + HTML;
// throw new
// RuntimeException("Could not find a Japid template with the name of: "
// + templateFileName);
// } else {
// // render(tClass, args);
// String rr = invokeRender(tClass, args);
// return rr;
// }
RendererClass rc = JapidRenderer.getFunctionalRendererClass(templateClassName);
if (rc == null) {
String templateFileName = templateClassName.replace(DOT, '/') + HTML;
throw new RuntimeException("Could not find a Japid template with the name of: " + templateFileName);
} else {
String rr = invokeRender(rc, args);
return rr;
}
}
use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.
the class JapidRenderer method loadRendererClass.
private static Class<? extends JapidTemplateBaseWithoutPlay> loadRendererClass(RendererClass rc) {
// the template may refer to model classes etc available only from the
// parent class loader
ClassLoader cl = _parentClassLoader == null ? JapidRenderer.class.getClassLoader() : _parentClassLoader;
// do I need to new instance of TemplateClassLoader for each invocation?
// likely...
TemplateClassLoader classReloader = new TemplateClassLoader(cl);
try {
Class<JapidTemplateBaseWithoutPlay> loadClass = (Class<JapidTemplateBaseWithoutPlay>) classReloader.loadClass(rc.getClassName());
rc.setClz(loadClass);
return loadClass;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.
the class JapidRenderer method newRendererClass.
/**
* @param c
* @return
*/
static RendererClass newRendererClass(String c) {
RendererClass rc = new RendererClass();
rc.setClassName(c);
// the source code of the Java file might not be available yet
// rc.setSourceCode(getSouceCode(c));
rc.setLastUpdated(0);
return rc;
}
use of cn.bran.japid.rendererloader.RendererClass in project japid42 by branaway.
the class JapidRenderer method registerTemplate.
/**
* To register a template in the Japid engine. Once a template is
* registered, the template class can be retrieved with the name by invoking
* {@link #getClass(String)} or {@link #getRendererClass(String)}.
*
* This method can be used at runtime to compile a Japid script and later
* use it to render data by invoking
* JapidController.renderJapidWith(className, args...);
*
* @author Bing Ran (bing.ran@gmail.com)
* @param mimeType
* the MIME type of content generated by this template.
* @param source
* the script source of the Japid template
* @param key
* the key that the source script is registered under. must be in
* the form of a valid Java class name
* @return returns the key, which is prefixed with "japidviews." if it was
* not so
*/
public static String registerTemplate(MimeTypeEnum mimeType, String source, String key) {
refreshClasses();
if (!key.startsWith(JAPIDVIEWS)) {
key = JAPIDVIEWS + "." + key.replace('-', '_').replace(' ', '_');
}
RendererClass cl = dynamicClasses.get(key);
if (cl != null) {
if (source.equals(cl.getJapidSourceCode())) {
return key;
}
}
try {
String javaCode = JapidTemplateTransformer.generateInMemory(source, key, mimeType, usePlay);
JapidFlags.log("converted: " + key);
// System.out.println(javaCode);
RendererClass rc = newRendererClass(key);
rc.setJapidSourceCode(source);
rc.setJavaSourceCode(javaCode);
removeInnerClasses(key);
cleanByteCode(rc);
// remember the current impl of class
japidClasses.put(key, rc);
// refresh will erase dynamic template
// class from this container.
compiler.compile(new String[] { key });
dynamicClasses.put(key, rc);
TemplateClassLoader loader = getClassLoader();
loader.loadClass(key);
} catch (Exception e) {
if (e instanceof JapidTemplateException)
throw (JapidTemplateException) e;
throw new RuntimeException(e);
}
return key;
}
Aggregations