Search in sources :

Example 1 with CompilationUnit

use of org.apache.sling.scripting.sightly.compiler.CompilationUnit in project sling by apache.

the class TestUtils method readScriptFromClasspath.

public static CompilationUnit readScriptFromClasspath(final String scriptResource) {
    InputStream stream = TestUtils.class.getResourceAsStream(scriptResource);
    final Reader reader = new InputStreamReader(stream);
    return new CompilationUnit() {

        @Override
        public String getScriptName() {
            return scriptResource;
        }

        @Override
        public Reader getScriptReader() {
            return reader;
        }
    };
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 2 with CompilationUnit

use of org.apache.sling.scripting.sightly.compiler.CompilationUnit in project sling by apache.

the class JavaClassBackendCompilerTest method testScript.

@Test
public void testScript() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/test.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("testScript");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/test-output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Example 3 with CompilationUnit

use of org.apache.sling.scripting.sightly.compiler.CompilationUnit in project sling by apache.

the class JavaClassBackendCompilerTest method sling_6094_2.

@Test
public void sling_6094_2() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/SLING-6094.2.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("sling_6094_2");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/SLING-6094.2.output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Example 4 with CompilationUnit

use of org.apache.sling.scripting.sightly.compiler.CompilationUnit in project sling by apache.

the class JavaClassBackendCompilerTest method sling_6094_1.

@Test
public void sling_6094_1() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/SLING-6094.1.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("sling_6094_1");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    bindings.put("img", new HashMap<String, Object>() {

        {
            put("attributes", new HashMap<String, String>() {

                {
                    put("v-bind:src", "replaced");
                }
            });
        }
    });
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/SLING-6094.1.output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Example 5 with CompilationUnit

use of org.apache.sling.scripting.sightly.compiler.CompilationUnit in project sling by apache.

the class SightlyScriptEngine method internalCompile.

private SightlyCompiledScript internalCompile(final Reader script, ScriptContext scriptContext) throws ScriptException {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(((SightlyScriptEngineFactory) getFactory()).getClassLoader());
    try {
        String sName = NO_SCRIPT;
        if (script instanceof ScriptNameAware) {
            sName = ((ScriptNameAware) script).getScriptName();
        }
        if (sName.equals(NO_SCRIPT)) {
            sName = getScriptName(scriptContext);
        }
        final String scriptName = sName;
        CompilationUnit compilationUnit = new CompilationUnit() {

            @Override
            public String getScriptName() {
                return scriptName;
            }

            @Override
            public Reader getScriptReader() {
                return script;
            }
        };
        JavaClassBackendCompiler javaClassBackendCompiler = new JavaClassBackendCompiler();
        GlobalShadowCheckBackendCompiler shadowCheckBackendCompiler = null;
        if (scriptContext != null) {
            Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
            Set<String> globals = bindings.keySet();
            shadowCheckBackendCompiler = new GlobalShadowCheckBackendCompiler(javaClassBackendCompiler, globals);
        }
        CompilationResult result = shadowCheckBackendCompiler == null ? sightlyCompiler.compile(compilationUnit, javaClassBackendCompiler) : sightlyCompiler.compile(compilationUnit, shadowCheckBackendCompiler);
        if (result.getWarnings().size() > 0) {
            for (CompilerMessage warning : result.getWarnings()) {
                LOGGER.warn("Script {} {}:{}: {}", new Object[] { warning.getScriptName(), warning.getLine(), warning.getColumn(), warning.getMessage() });
            }
        }
        if (result.getErrors().size() > 0) {
            CompilerMessage error = result.getErrors().get(0);
            throw new ScriptException(error.getMessage(), error.getScriptName(), error.getLine(), error.getColumn());
        }
        SourceIdentifier sourceIdentifier = new SourceIdentifier(configuration, scriptName);
        String javaSourceCode = javaClassBackendCompiler.build(sourceIdentifier);
        Object renderUnit = javaCompilerService.compileSource(sourceIdentifier, javaSourceCode);
        if (renderUnit instanceof RenderUnit) {
            return new SightlyCompiledScript(this, (RenderUnit) renderUnit);
        } else {
            throw new SightlyException("Expected a RenderUnit.");
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) ScriptNameAware(org.apache.sling.scripting.api.ScriptNameAware) CompilerMessage(org.apache.sling.scripting.sightly.compiler.CompilerMessage) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) GlobalShadowCheckBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.GlobalShadowCheckBackendCompiler) SourceIdentifier(org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) ScriptException(javax.script.ScriptException) SightlyException(org.apache.sling.scripting.sightly.SightlyException) CompilationResult(org.apache.sling.scripting.sightly.compiler.CompilationResult)

Aggregations

CompilationUnit (org.apache.sling.scripting.sightly.compiler.CompilationUnit)6 Bindings (javax.script.Bindings)4 JavaClassBackendCompiler (org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler)4 StringWriter (java.io.StringWriter)3 SimpleBindings (javax.script.SimpleBindings)3 SightlyCompiler (org.apache.sling.scripting.sightly.compiler.SightlyCompiler)3 ClassInfo (org.apache.sling.scripting.sightly.java.compiler.ClassInfo)3 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)1 ScriptException (javax.script.ScriptException)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 ScriptNameAware (org.apache.sling.scripting.api.ScriptNameAware)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 CompilationResult (org.apache.sling.scripting.sightly.compiler.CompilationResult)1 CompilerMessage (org.apache.sling.scripting.sightly.compiler.CompilerMessage)1 SourceIdentifier (org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier)1