You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by di...@apache.org on 2012/08/07 19:45:11 UTC

svn commit: r1370389 - in /velocity/sandbox/jsr223/velocity-engine-scripting/src: main/java/org/apache/velocity/script/ test/java/org/apache/velocity/script/test/tools/

Author: dishara
Date: Tue Aug  7 17:45:11 2012
New Revision: 1370389

URL: http://svn.apache.org/viewvc?rev=1370389&view=rev
Log:
Adding ability to pass a script itself to eval and obtain then filename from context param

Modified:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
    velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java?rev=1370389&r1=1370388&r2=1370389&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java Tue Aug  7 17:45:11 2012
@@ -48,6 +48,12 @@ public class VelocityScriptEngine implem
 
 
     /**
+     * Default velocity log tag
+     */
+    public static final String DEFAULT_LOG_TAG = "default_log_tag";
+
+
+    /**
      * script context reference which belongs to this engine instance
      */
     private ScriptContext scriptContext;
@@ -190,17 +196,47 @@ public class VelocityScriptEngine implem
      * re-parsed or recompiled before execution. State left in the engine from previous executions, including variable values and
      * compiled procedures may be visible during this execution.
      *
-     * @param s             The script to be executed by the script engine.
+     * @param s  The script to be executed by the script engine.
      * @param scriptContext A ScriptContext exposing sets of attributes in different scopes. The meanings of the
      *                      scopes ScriptContext.GLOBAL_SCOPE, and ScriptContext.ENGINE_SCOPE are defined in the specification.
      * @return The value returned from the execution of the script.
      * @throws ScriptException
      */
     public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
-        return eval(new StringReader(s), scriptContext);
+//        return eval(new StringReader(s), scriptContext);
+      if (s == null) {
+            throw new NullPointerException("Reader passed cannot be null");
+        }
+        constructVelocityEngine(scriptContext);
+//        String fileName = getTargetFilename(scriptContext);
+        VelocityContext velocityContext = getVelocityContext(scriptContext);
+
+        Writer outPut;
+        if (scriptContext.getWriter() != null) {
+            outPut = scriptContext.getWriter();
+        } else {
+            outPut = new StringWriter();
+        }
+        boolean result;
+
+        try {
+//            Check for velocity tools vm file
+            if (scriptContext.getAttribute(VelocityScriptEngine.FILENAME) != null) {
+                Template template = velocityEngine.getTemplate(
+                        scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString());
+                template.merge(velocityContext, outPut);
+            }
+            result = velocityEngine.evaluate(velocityContext, outPut, VelocityScriptEngine.DEFAULT_LOG_TAG, s);
+        } catch (Exception exp) {
+            return new ScriptException(exp);
+        }
+        return String.valueOf(result);
+
     }
 
 
+
+
     /**
      * Same as eval(String, ScriptContext) where the source of the script is read from a Reader.
      *
@@ -228,11 +264,10 @@ public class VelocityScriptEngine implem
         boolean result;
 
         try {
-            BufferedReader bf = new BufferedReader(reader);
-            String vm = bf.readLine();
             //Check for velocity tools vm file
-            if (vm != null && vm.endsWith(".vm")) {
-                Template template = velocityEngine.getTemplate(vm);
+            if (scriptContext.getAttribute(VelocityScriptEngine.FILENAME) != null) {
+                Template template = velocityEngine.getTemplate(
+                        scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString());
                 template.merge(velocityContext, outPut);
             }
             result = velocityEngine.evaluate(velocityContext, outPut, fileName, reader);

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java?rev=1370389&r1=1370388&r2=1370389&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/CustomEvent.java Tue Aug  7 17:45:11 2012
@@ -38,6 +38,10 @@ public class CustomEvent {
         return date;
     }
 
+    public long getID(){
+      return date.getTime();
+    }
+
     @Override
     public String toString(){
         return "This is a test event template: created by " + name + " on " + date.toString();

Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java?rev=1370389&r1=1370388&r2=1370389&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/tools/EventToolTest.java Tue Aug  7 17:45:11 2012
@@ -44,9 +44,14 @@ public class EventToolTest extends Abstr
         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.FILENAME, "eventtool.vm", ScriptContext.ENGINE_SCOPE);
         Writer writer = new StringWriter();
         context.setWriter(writer);
-        engine.eval("eventtool.vm");
+        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);
     }