Search in sources :

Example 66 with SimpleBindings

use of javax.script.SimpleBindings in project sling by apache.

the class JsEnvironment method buildBindings.

private Bindings buildBindings(Resource scriptResource, Bindings local, Bindings arguments, CommonJsModule commonJsModule) {
    Bindings bindings = new SimpleBindings();
    bindings.putAll(engineBindings);
    DependencyResolver dependencyResolver = new DependencyResolver(scriptResource, this, local);
    UseFunction useFunction = new UseFunction(dependencyResolver, arguments);
    bindings.put(Variables.JS_USE, useFunction);
    bindings.put(Variables.MODULE, commonJsModule);
    bindings.put(Variables.EXPORTS, commonJsModule.getExports());
    bindings.put(Variables.CONSOLE, new Console(LoggerFactory.getLogger(scriptResource.getName())));
    bindings.putAll(local);
    return bindings;
}
Also used : SimpleBindings(javax.script.SimpleBindings) UseFunction(org.apache.sling.scripting.sightly.js.impl.use.UseFunction) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) DependencyResolver(org.apache.sling.scripting.sightly.js.impl.use.DependencyResolver)

Example 67 with SimpleBindings

use of javax.script.SimpleBindings in project sling by apache.

the class BindingsUtils method merge.

/**
     * Combine two bindings objects. Priority goes to latter bindings.
     *
     * @param former first map of bindings
     * @param latter second map of bindings, which can override the fist one
     * @return the merging of the two maps
     */
public static Bindings merge(Bindings former, Bindings latter) {
    Bindings bindings = new SimpleBindings();
    bindings.putAll(former);
    bindings.putAll(latter);
    return bindings;
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 68 with SimpleBindings

use of javax.script.SimpleBindings 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 69 with SimpleBindings

use of javax.script.SimpleBindings 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 70 with SimpleBindings

use of javax.script.SimpleBindings 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

SimpleBindings (javax.script.SimpleBindings)102 Bindings (javax.script.Bindings)65 ScriptEngine (javax.script.ScriptEngine)23 Test (org.junit.Test)22 ScriptContext (javax.script.ScriptContext)17 ScriptException (javax.script.ScriptException)17 ScriptEngineManager (javax.script.ScriptEngineManager)16 SimpleScriptContext (javax.script.SimpleScriptContext)15 Test (org.testng.annotations.Test)11 CompiledScript (javax.script.CompiledScript)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 File (java.io.File)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLEventWriter (javax.xml.stream.XMLEventWriter)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4