Author: cbrisson
Date: Sat Jul 16 18:36:42 2016
New Revision: 1752989

URL: http://svn.apache.org/viewvc?rev=1752989&view=rev
Log:
[jsr223] make VelocityScriptEngine implement the Compilable interface ; by 
convention, choose to always return the writer

Added:
    
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityCompiledScript.java
Modified:
    
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
    
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
    
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java

Added: 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityCompiledScript.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityCompiledScript.java?rev=1752989&view=auto
==============================================================================
--- 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityCompiledScript.java
 (added)
+++ 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityCompiledScript.java
 Sat Jul 16 18:36:42 2016
@@ -0,0 +1,70 @@
+package org.apache.velocity.script;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+
+import javax.script.CompiledScript;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+public class VelocityCompiledScript extends CompiledScript
+{
+    protected VelocityScriptEngine engine;
+    protected Template template;
+
+    public VelocityCompiledScript(VelocityScriptEngine e, Template t)
+    {
+        engine = e;
+        template = t;
+    }
+
+    @Override
+    public Object eval(ScriptContext scriptContext) throws ScriptException
+    {
+        VelocityContext velocityContext = 
VelocityScriptEngine.getVelocityContext(scriptContext);
+        Writer out = scriptContext.getWriter();
+        if (out == null)
+        {
+            out = new StringWriter();
+            scriptContext.setWriter(out);
+        }
+        try
+        {
+            template.merge(velocityContext, out);
+            out.flush();
+        }
+        catch (Exception exp)
+        {
+            throw new ScriptException(exp);
+        }
+        return out;
+    }
+
+    @Override
+    public ScriptEngine getEngine()
+    {
+        return engine;
+    }
+}

Modified: 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java?rev=1752989&r1=1752988&r2=1752989&view=diff
==============================================================================
--- 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
 (original)
+++ 
velocity/engine/trunk/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
 Sat Jul 16 18:36:42 2016
@@ -43,28 +43,28 @@ import java.util.Properties;
 
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeInstance;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.resource.loader.ResourceLoader2;
 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
 
 import javax.script.AbstractScriptEngine;
 import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
 import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
 import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 
-public class VelocityScriptEngine extends AbstractScriptEngine
+public class VelocityScriptEngine extends AbstractScriptEngine implements 
Compilable
 {
-
-    public static final String STRING_OUTPUT_MODE_KEY = 
"org.apache.velocity.stringOutput";
     public static final String VELOCITY_PROPERTIES_KEY = 
"org.apache.velocity.script.properties";
 
     // my factory, may be null
     private volatile ScriptEngineFactory factory;
-    private volatile VelocityEngine vengine;
+    private volatile RuntimeInstance velocityEngine;
 
     public VelocityScriptEngine(ScriptEngineFactory factory)
     {
@@ -76,9 +76,9 @@ public class VelocityScriptEngine extend
         this(null);
     }
 
-    public VelocityEngine getVelocityEngine()
+    public RuntimeInstance getVelocityEngine()
     {
-        return vengine;
+        return velocityEngine;
     }
        
     // ScriptEngine methods
@@ -94,26 +94,22 @@ public class VelocityScriptEngine extend
         initVelocityEngine(ctx);
         String fileName = getFilename(ctx);
         VelocityContext vctx = getVelocityContext(ctx);
-        boolean outputAsString = isStringOutputMode(ctx);
-        Writer out;
-        if (outputAsString)
+        Writer out = ctx.getWriter();
+        if (out == null)
         {
             out = new StringWriter();
-        }
-        else
-        {
-            out = ctx.getWriter();
+            ctx.setWriter(out);
         }
         try
         {
-            vengine.evaluate(vctx, out, fileName, reader);
+            velocityEngine.evaluate(vctx, out, fileName, reader);
             out.flush();
         }
         catch (Exception exp)
         {
             throw new ScriptException(exp);
         }
-        return outputAsString ? out.toString() : null;
+        return out;
     }
 
     public ScriptEngineFactory getFactory()
@@ -139,14 +135,18 @@ public class VelocityScriptEngine extend
     // internals only below this point
     private void initVelocityEngine(ScriptContext ctx)
     {
-        if (vengine == null)
+        if (ctx == null)
+        {
+            ctx = getContext();
+        }
+        if (velocityEngine == null)
         {
             synchronized (this)
             {
-                if (vengine != null) return;
+                if (velocityEngine != null) return;
 
                 Properties props = getVelocityProperties(ctx);
-                VelocityEngine tmpEngine = new VelocityEngine();
+                RuntimeInstance tmpEngine = new RuntimeInstance();
                 try
                 {
                     if (props != null)
@@ -166,7 +166,7 @@ public class VelocityScriptEngine extend
                 {
                     throw new RuntimeException(exp);
                 }
-                vengine = tmpEngine;
+                velocityEngine = tmpEngine;
             }
         }
     }
@@ -192,12 +192,6 @@ public class VelocityScriptEngine extend
         return fileName != null? fileName.toString() : "<unknown>";
     }
 
-    protected static boolean isStringOutputMode(ScriptContext ctx)
-    {
-        Object flag = ctx.getAttribute(STRING_OUTPUT_MODE_KEY);
-        return Boolean.parseBoolean(String.valueOf(flag));
-    }
-
     protected static Properties getVelocityProperties(ScriptContext ctx)
     {
         try
@@ -228,4 +222,45 @@ public class VelocityScriptEngine extend
         }            
         return null;
     }
+
+    public CompiledScript compile(String script) throws ScriptException
+    {
+        return compile(new StringReader(script));
+    }
+
+    public CompiledScript compile(Reader script) throws ScriptException
+    {
+        initVelocityEngine(null);
+        ResourceLoader2 resourceLoader = new SingleResourceReader(script);
+        Template template = new Template();
+        template.setRuntimeServices(velocityEngine);
+        template.setResourceLoader(resourceLoader);
+        try
+        {
+            template.process();
+        }
+        catch(Exception e)
+        {
+            // CB TODO - exception may have line/col informations, that 
ScriptException can exploit
+            throw new ScriptException(e);
+        }
+        return new VelocityCompiledScript(this, template);
+    }
+
+    // a dummy resource reader class, serving a single resource given the 
resource reader
+    protected static class SingleResourceReader extends StringResourceLoader
+    {
+        private Reader reader;
+
+        public SingleResourceReader(Reader r)
+        {
+            reader = r;
+        }
+
+        @Override
+        public Reader getResourceReader(String source, String encoding) throws 
ResourceNotFoundException
+        {
+            return reader;
+        }
+    }
 }

Modified: 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java?rev=1752989&r1=1752988&r2=1752989&view=diff
==============================================================================
--- 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
 (original)
+++ 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/ScriptEngineTest.java
 Sat Jul 16 18:36:42 2016
@@ -21,6 +21,8 @@ package org.apache.velocity.script.test;
 import org.apache.velocity.script.VelocityScriptEngine;
 
 import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
 import javax.script.ScriptContext;
 import javax.script.ScriptException;
 import java.io.StringWriter;
@@ -66,12 +68,22 @@ public class ScriptEngineTest extends Ab
 
     public void testEngineEvals() throws ScriptException {
         String path = System.getProperty("test.resources.dir");
-        Writer writer = new StringWriter();
-        engine.getContext().setWriter(writer);
+        engine.getContext().setWriter(new StringWriter());
         
engine.getContext().setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, 
path + "/test-classes/velocity.properties", ScriptContext.ENGINE_SCOPE);
-        
engine.getContext().setAttribute(VelocityScriptEngine.STRING_OUTPUT_MODE_KEY, 
"true", ScriptContext.ENGINE_SCOPE);
         String script = "<html><body>#set( $foo = 'Velocity' )Hello $foo 
World!</body><html>";
         Object result = engine.eval(script);
-        assertEquals(String.valueOf(result), "<html><body>Hello Velocity 
World!</body><html>");
+        assertEquals(result.toString(), "<html><body>Hello Velocity 
World!</body><html>");
+    }
+
+    public void testCompilable() throws ScriptException
+    {
+        String path = System.getProperty("test.resources.dir");
+        engine.getContext().setWriter(new StringWriter());
+        
engine.getContext().setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, 
path + "/test-classes/velocity.properties", ScriptContext.ENGINE_SCOPE);
+        String script = "$foo";
+        engine.put("foo", "bar");
+        CompiledScript compiled = ((Compilable)engine).compile(script);
+        Object result = compiled.eval();
+        assertEquals(result.toString(), "bar");
     }
 }

Modified: 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java?rev=1752989&r1=1752988&r2=1752989&view=diff
==============================================================================
--- 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
 (original)
+++ 
velocity/engine/trunk/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
 Sat Jul 16 18:36:42 2016
@@ -43,16 +43,17 @@ public class EventToolTest extends Abstr
                 
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
         CustomEvent event = new CustomEvent("MyEvent");
         context.getBindings(ScriptContext.ENGINE_SCOPE).put("event", event);
-        context.setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES, 
properties, ScriptContext.ENGINE_SCOPE);
+        context.setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES_KEY, 
properties, ScriptContext.ENGINE_SCOPE);
         context.setAttribute(VelocityScriptEngine.FILENAME, "eventtool.vm", 
ScriptContext.ENGINE_SCOPE);
         Writer writer = new StringWriter();
         context.setWriter(writer);
         engine.eval("$event;\n" +
-                "\n" +
                 "Event Created by $event.getName()\n" +
                 "Event Created on $event.getDate()\n" +
                 "Event ID is $event.getID()");
-        System.out.println("####### Tools output #########\n"+writer);
+        // check string start
+        String check = "This is a test event template: created by MyEvent on ";
+        assertEquals(writer.toString().substring(0, check.length()), check);
     }
 
 


Reply via email to