Search in sources :

Example 1 with RenderContext

use of org.apache.sling.scripting.sightly.render.RenderContext in project sling by apache.

the class SightlyCompiledScript method eval.

@Override
public Object eval(ScriptContext context) throws ScriptException {
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    Object oldBindings = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        RenderContext renderContext = new RenderContextImpl(context);
        PrintWriter out = new PrintWriter(context.getWriter());
        renderUnit.render(out, renderContext, new SimpleBindings());
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldBindings);
    }
    return null;
}
Also used : RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 2 with RenderContext

use of org.apache.sling.scripting.sightly.render.RenderContext 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 RenderContext

use of org.apache.sling.scripting.sightly.render.RenderContext 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 RenderContext

use of org.apache.sling.scripting.sightly.render.RenderContext 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)

Aggregations

Bindings (javax.script.Bindings)4 SimpleBindings (javax.script.SimpleBindings)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 StringWriter (java.io.StringWriter)3 CompilationUnit (org.apache.sling.scripting.sightly.compiler.CompilationUnit)3 SightlyCompiler (org.apache.sling.scripting.sightly.compiler.SightlyCompiler)3 ClassInfo (org.apache.sling.scripting.sightly.java.compiler.ClassInfo)3 JavaClassBackendCompiler (org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler)3 Test (org.junit.Test)3 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 RenderContextImpl (org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl)1