Search in sources :

Example 21 with JapidTemplate

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

the class CompilerTests method testTagBlock.

@Test
public void testTagBlock() throws IOException {
    String srcFile = "JapidSample/app/japidviews/templates/tagBody.html";
    String src = readFile(srcFile);
    JapidTemplate bt = new JapidTemplate("japidviews/templates/tagBody.html", src);
    JapidAbstractCompiler cp = new JapidTemplateCompiler();
    cp.compile(bt);
    System.out.println(bt.javaSource);
    assertTrue("invalid java code", JavaSyntaxTool.isValid(bt.javaSource));
    //		assertTrue(bt.javaSource.contains("((anotherTag)(new anotherTag(getOut())).setActionRunners(getActionRunners())).render(echo, new anotherTag.DoBody<String>(){"));
    assertTrue(bt.javaSource.contains("new moreTag(tagBody.this)"));
}
Also used : JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidTemplateCompiler(cn.bran.japid.compiler.JapidTemplateCompiler) Test(org.junit.Test)

Example 22 with JapidTemplate

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

the class CompilerTests method testOpenFor.

@Test
public void testOpenFor() throws IOException {
    String src = readFile("tests/openFor.html");
    JapidTemplate bt = new JapidTemplate("tests/openFor.html", src);
    JapidAbstractCompiler cp = new JapidLayoutCompiler();
    cp.compile(bt);
    System.out.println(bt.javaSource);
    assertTrue("invalid java code", JavaSyntaxTool.isValid(bt.javaSource));
}
Also used : JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidLayoutCompiler(cn.bran.japid.compiler.JapidLayoutCompiler) Test(org.junit.Test)

Example 23 with JapidTemplate

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

the class CompilerTests method testEachDirective.

@Test
public void testEachDirective() throws IOException {
    String srcFile = "tests/eachTag.html";
    String src = readFile(srcFile);
    JapidTemplate bt = new JapidTemplate("eachTag.html", src);
    JapidAbstractCompiler cp = new JapidTemplateCompiler();
    cp.compile(bt);
    System.out.println(bt.javaSource);
    assertTrue("invalid java code", JavaSyntaxTool.isValid(bt.javaSource));
    assertFalse(bt.javaSource.contains("setActionRunners"));
}
Also used : JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidTemplateCompiler(cn.bran.japid.compiler.JapidTemplateCompiler) Test(org.junit.Test)

Example 24 with JapidTemplate

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

the class CompilerTests method testLayoutWithArgs.

@Test
public void testLayoutWithArgs() throws IOException {
    String src = readFile("JapidSample/app/japidviews/more/Perf/perfmain.html");
    JapidTemplate bt = new JapidTemplate("more/Perf/perfmain.html", src);
    JapidAbstractCompiler cp = new JapidLayoutCompiler();
    cp.compile(bt);
    System.out.println(bt.javaSource);
    assertTrue("invalid java code", JavaSyntaxTool.isValid(bt.javaSource));
}
Also used : JapidAbstractCompiler(cn.bran.japid.compiler.JapidAbstractCompiler) JapidTemplate(cn.bran.japid.template.JapidTemplate) JapidLayoutCompiler(cn.bran.japid.compiler.JapidLayoutCompiler) Test(org.junit.Test)

Example 25 with JapidTemplate

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

the class CompilerRequestor method acceptResult.

@Override
public void acceptResult(CompilationResult result) {
    // If error
    if (result.hasErrors()) {
        // bran: sort the problems and report the first one
        CategorizedProblem[] errors = result.getErrors();
        Arrays.sort(errors, new Comparator<CategorizedProblem>() {

            @Override
            public int compare(CategorizedProblem o1, CategorizedProblem o2) {
                return o1.getSourceLineNumber() - o2.getSourceLineNumber();
            }
        });
        for (IProblem problem : errors) {
            String className = new String(problem.getOriginatingFileName()).replace("/", ".");
            className = className.substring(0, className.length() - 5);
            String message = problem.getMessage();
            if (problem.getID() == IProblem.CannotImportPackage) {
                // Non sense !
                message = problem.getArguments()[0] + " cannot be resolved";
            }
            RendererClass rc = this.rendererCompiler.japidClasses.get(className);
            if (rc.getSrcFile() == null)
                throw new RuntimeException("no source file for compiling " + className);
            if (rc.getOriSourceCode() == null)
                throw new RuntimeException("no original source code for compiling " + className);
            JapidTemplate tmpl = new JapidTemplate(rc.getSrcFile().getPath(), rc.getOriSourceCode());
            throw new JapidCompilationException(tmpl, DirUtil.mapJavaLineToSrcLine(rc.getSourceCode(), problem.getSourceLineNumber()), message + " while compiling " + className);
        }
    }
    // Something has been compiled
    ClassFile[] clazzFiles = result.getClassFiles();
    for (int i = 0; i < clazzFiles.length; i++) {
        final ClassFile clazzFile = clazzFiles[i];
        final char[][] compoundName = clazzFile.getCompoundName();
        final StringBuffer clazzName = new StringBuffer();
        for (int j = 0; j < compoundName.length; j++) {
            if (j != 0) {
                clazzName.append('.');
            }
            clazzName.append(compoundName[j]);
        }
        byte[] bytes = clazzFile.getBytes();
        JapidFlags.debug("Bytecode generated for " + clazzName);
        // XXX address anonymous inner class issue!! ....$1...
        String cname = clazzName.toString();
        RendererClass rc = this.rendererCompiler.japidClasses.get(cname);
        if (rc == null) {
            if (cname.contains("$")) {
                // inner class
                rc = new RendererClass();
                rc.setClassName(cname);
                this.rendererCompiler.japidClasses.put(cname, rc);
            } else {
                throw new RuntimeException("name not in the classes container: " + cname);
            }
        }
        rc.setBytecode(bytes);
    }
}
Also used : JapidTemplate(cn.bran.japid.template.JapidTemplate) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IProblem(org.eclipse.jdt.core.compiler.IProblem) CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem)

Aggregations

JapidTemplate (cn.bran.japid.template.JapidTemplate)44 JapidAbstractCompiler (cn.bran.japid.compiler.JapidAbstractCompiler)39 Test (org.junit.Test)39 JapidTemplateCompiler (cn.bran.japid.compiler.JapidTemplateCompiler)29 CompilationUnit (japa.parser.ast.CompilationUnit)12 JapidLayoutCompiler (cn.bran.japid.compiler.JapidLayoutCompiler)10 TagIf (cn.bran.japid.compiler.Tag.TagIf)2 TagInTag (cn.bran.japid.compiler.Tag.TagInTag)2 File (java.io.File)2 Matcher (java.util.regex.Matcher)2 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)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