Search in sources :

Example 6 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method refreshClasses.

static synchronized void refreshClasses() {
    if (!classesInited) {
        classesInited = true;
    } else {
        if (!timeToRefresh())
            return;
    }
    if (templateRoots == null)
        return;
    if (!JapidRenderer.keepJavaFiles) {
        refreshClassesInMemory();
        return;
    }
    try {
        // there are two passes of directory scanning. XXX
        String[] allTemps = DirUtil.getAllTemplateFileNames(templateRoots);
        Set<String> currentClassesOnDir = createClassNameSet(allTemps);
        Set<String> allScriptNames = new HashSet<String>(currentClassesOnDir);
        Set<String> keySet = japidClasses.keySet();
        if (!keySet.equals(currentClassesOnDir)) {
            Set<String> classNamesRegistered = new HashSet<String>(keySet);
            Set<String> classNamesDir = new HashSet<String>(currentClassesOnDir);
            if (classNamesRegistered.containsAll(classNamesDir)) {
                classNamesRegistered.removeAll(classNamesDir);
                if (!classNamesRegistered.isEmpty()) {
                    for (String n : classNamesRegistered) {
                        if (!n.contains("$")) {
                            // if (!japidClasses.get(n).fromJar()) {
                            if (!specialClasses.contains(n)) {
                                touch();
                                break;
                            }
                        }
                    }
                }
            } else {
                touch();
            }
        } else {
        // no name changes
        }
        // got new templates
        allScriptNames.removeAll(keySet);
        removeRemoved(currentClassesOnDir, keySet);
        for (String c : allScriptNames) {
            RendererClass rc = newRendererClass(c);
            japidClasses.put(c, rc);
        }
        // now all the class set size is up to date
        // now update any Java source code
        // second disk scanning.
        List<File> gen = gen(templateRoots);
        // this would include both new and updated java
        Set<String> updatedClasses = new HashSet<String>();
        if (gen.size() > 0) {
            for (File f : gen) {
                String className = getClassName(f);
                updatedClasses.add(className);
                RendererClass rendererClass = japidClasses.get(className);
                if (rendererClass == null) {
                    // this should not happen, since
                    throw new RuntimeException("any new class names should have been in the classes container: " + className);
                // rendererClass = newRendererClass(className);
                // japidClasses.put(className, rendererClass);
                }
                setSources(rendererClass, f);
                removeInnerClasses(className);
                cleanByteCode(rendererClass);
            }
        }
        // find all render class without bytecode
        for (Iterator<String> i = japidClasses.keySet().iterator(); i.hasNext(); ) {
            String k = i.next();
            RendererClass rc = japidClasses.get(k);
            if (rc.getJavaSourceCode() == null) {
                if (!rc.getClassName().contains("$")) {
                    try {
                        setSources(rc, k);
                    } catch (Exception e) {
                        JapidFlags.log("Cannot find the source Java file for " + rc.getClassName() + ". Dropped.");
                        i.remove();
                        continue;
                    }
                    cleanByteCode(rc);
                    updatedClasses.add(k);
                } else {
                    rc.setLastUpdated(0);
                }
            } else {
                if (rc.getBytecode() == null) {
                    cleanByteCode(rc);
                    updatedClasses.add(k);
                }
            }
        }
        // compile all
        if (updatedClasses.size() > 0) {
            dynamicClasses.clear();
            String[] names = new String[updatedClasses.size()];
            int i = 0;
            for (String s : updatedClasses) {
                names[i++] = s;
            }
            long t = System.currentTimeMillis();
            // newly compiled class bytecode bodies are set in the global
            // classes set ready for defining
            compiler.compile(names);
            howlong("compile time for " + names.length + " classes", t);
            for (String k : japidClasses.keySet()) {
                japidClasses.get(k).setClz(null);
            }
            TemplateClassLoader loader = getClassLoader();
            for (String cname : updatedClasses) {
                loader.loadClass(cname);
            }
        }
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) HashSet(java.util.HashSet)

Example 7 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method process.

// compile japid script to Java code from an inputstream
private static RendererClass process(String name, InputStream input) throws IOException {
    InputStreamReader isr = new InputStreamReader(input, "UTF-8");
    BufferedReader reader = new BufferedReader(isr);
    String content = "";
    String line;
    while ((line = reader.readLine()) != null) content += line + "\n";
    reader.close();
    String fqName = DirUtil.deriveClassName(name);
    try {
        String javaCode = JapidTemplateTransformer.generateInMemory(content, (fqName), MimeTypeEnum.inferFromName(name), usePlay);
        // System.out.println(javaCode);
        RendererClass rc = newRendererClass(fqName);
        rc.setJapidSourceCode(content);
        rc.setJavaSourceCode(javaCode);
        removeInnerClasses(fqName);
        cleanByteCode(rc);
        // remember the current impl of class
        japidClasses.put(fqName, rc);
        // class from this container.
        return rc;
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException)

Example 8 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method compileDevErroView.

/**
	 * transform a Japid script file to Java code which is then compiled to
	 * bytecode stored in the global japidClasses object.
	 * 
	 * @author Bing Ran (bing.ran@gmail.com)
	 * @param srcFileName
	 * @param scriptSrc
	 */
static synchronized void compileDevErroView(String srcFileName, String scriptSrc) {
    try {
        String c = DirUtil.deriveClassName(srcFileName);
        RendererClass rc = newRendererClass(c);
        japidClasses.put(c, rc);
        String javaCode = JapidTemplateTransformer.generateInMemory(scriptSrc, srcFileName, usePlay);
        rc.setJavaSourceCode(javaCode);
        rc.setJapidSourceCode(scriptSrc);
        removeInnerClasses(c);
        cleanByteCode(rc);
        compiler.compile(new String[] { DEV_ERROR });
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException)

Aggregations

JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)8 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)7 RendererClass (cn.bran.japid.rendererloader.RendererClass)7 IOException (java.io.IOException)5 TemplateClassLoader (cn.bran.japid.rendererloader.TemplateClassLoader)3 File (java.io.File)2 HashSet (java.util.HashSet)2 JarFile (java.util.jar.JarFile)2 RenderResult (cn.bran.japid.template.RenderResult)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1