Search in sources :

Example 11 with RendererClass

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;
}
Also used : RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 12 with RendererClass

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;
    }
}
Also used : RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 13 with RendererClass

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);
    }
}
Also used : TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) RendererClass(cn.bran.japid.rendererloader.RendererClass) TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader)

Example 14 with RendererClass

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;
}
Also used : RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 15 with RendererClass

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;
}
Also used : JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException)

Aggregations

RendererClass (cn.bran.japid.rendererloader.RendererClass)19 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)9 JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)9 IOException (java.io.IOException)9 File (java.io.File)6 TemplateClassLoader (cn.bran.japid.rendererloader.TemplateClassLoader)5 HashSet (java.util.HashSet)5 JarFile (java.util.jar.JarFile)4 JapidTemplateBaseWithoutPlay (cn.bran.japid.template.JapidTemplateBaseWithoutPlay)1 RenderResult (cn.bran.japid.template.RenderResult)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 ObjectInputStream (java.io.ObjectInputStream)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 ApplicationClass (play.classloading.ApplicationClasses.ApplicationClass)1