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/01 07:37:29 UTC

svn commit: r1367861 - /velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java

Author: dishara
Date: Wed Aug  1 05:37:29 2012
New Revision: 1367861

URL: http://svn.apache.org/viewvc?rev=1367861&view=rev
Log:
Refactoring comments

Modified:
    velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.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=1367861&r1=1367860&r2=1367861&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 Wed Aug  1 05:37:29 2012
@@ -22,6 +22,7 @@ package org.apache.velocity.script;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
+
 import javax.script.*;
 import java.io.*;
 import java.util.Properties;
@@ -29,13 +30,13 @@ import java.util.Properties;
 public class VelocityScriptEngine implements ScriptEngine {
 
     /**
-     *   ScriptEngineFactory reference from whom this engine got created
+     * ScriptEngineFactory reference from whom this engine got created
      */
     private ScriptEngineFactory scriptEngineFactory;
 
 
     /**
-     *  Velocity core engine reference
+     * Velocity core engine reference
      */
     private VelocityEngine velocityEngine;
 
@@ -47,14 +48,15 @@ public class VelocityScriptEngine implem
 
 
     /**
-     *  script context reference which belongs to this engine instance
+     * script context reference which belongs to this engine instance
      */
     private ScriptContext scriptContext;
 
 
     /**
-     *    Constructor which gets created engine factory reference as input
-     * @param scriptEngineFactory     ScriptEngineFactory reference from whom this engine got created
+     * Constructor which gets created engine factory reference as input
+     *
+     * @param scriptEngineFactory ScriptEngineFactory reference from whom this engine got created
      */
     public VelocityScriptEngine(ScriptEngineFactory scriptEngineFactory) {
         this.scriptEngineFactory = scriptEngineFactory;
@@ -62,15 +64,14 @@ public class VelocityScriptEngine implem
     }
 
     /**
-     *
-     * @param scriptEngineFactory    ScriptEngineFactory reference from whom this engine got created
-     * @param bindings  required binding needs to initialize this engine, unless it defaults to engine scope
+     * @param scriptEngineFactory ScriptEngineFactory reference from whom this engine got created
+     * @param bindings            required binding needs to initialize this engine, unless it defaults to engine scope
      */
-    public VelocityScriptEngine(ScriptEngineFactory scriptEngineFactory,Bindings bindings) {
+    public VelocityScriptEngine(ScriptEngineFactory scriptEngineFactory, Bindings bindings) {
         this.scriptEngineFactory = scriptEngineFactory;
         this.scriptContext = new VelocityScriptContext();
-        if(bindings != null ) {
-            this.scriptContext.setBindings(bindings,ScriptContext.ENGINE_SCOPE);
+        if (bindings != null) {
+            this.scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
         } else {
             throw new NullPointerException("Bindings cannot be null");
         }
@@ -82,18 +83,16 @@ public class VelocityScriptEngine implem
      */
     public ScriptEngineFactory getFactory() {
 
-  //        if null return a newly created one
+        //        if null return a newly created one
         if (scriptEngineFactory == null) {
             createNewFactory();
         }
         return scriptEngineFactory;
     }
 
-    /**
-     * Added creation inside sync block to avoid creating two factories from a engine by two parallel threads at the same time.
-     * Also the additional null check out from sync block is to avoid every  thread to get blocked inside it even there is an already created factory.
-     */
     private void createNewFactory() {
+//          Added creation inside sync block to avoid creating two factories from a engine by two parallel threads at the same time.
+//          Also the additional null check out from sync block is to avoid every  thread to get blocked inside it even there is an already created factory.
         synchronized (this) {
             if (scriptEngineFactory == null) {
                 scriptEngineFactory = new VelocityScriptEngineFactory();
@@ -102,7 +101,8 @@ public class VelocityScriptEngine implem
     }
 
     /**
-     *  Creates the velocity core engine by initializing it from reading property file/system properties
+     * Creates the velocity core engine by initializing it from reading property file/system properties
+     *
      * @param context
      */
     private void constructVelocityEngine(ScriptContext context) {
@@ -140,7 +140,8 @@ public class VelocityScriptEngine implem
     }
 
     /**
-     *  Initializes the velocity engine with pre defined properties
+     * Initializes the velocity engine with pre defined properties
+     *
      * @param props
      */
     private void initVelocityEngine(Properties props) {
@@ -154,6 +155,7 @@ public class VelocityScriptEngine implem
 
     /**
      * Obtain properties from a property file which is taken from a system property
+     *
      * @return
      */
     private Properties getPropertiesFromSystem() {
@@ -183,46 +185,45 @@ public class VelocityScriptEngine implem
     }
 
 
-
-  /**
+    /**
      * Causes the immediate execution of the script whose source is the String passed as the first argument. The script may be
      * 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 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.
+     *
+     * @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);
     }
 
 
-
     /**
-     *  Same as eval(String, ScriptContext) where the source of the script is read from a Reader.
-     * @param reader  The source of the script to be executed by the script engine.
-     * @param scriptContext  The ScriptContext passed to the script engine.
-     * @return  The value returned from the execution of the script.
-     * @throws
-     *  ScriptException  if an error occurrs in script.
-     * java.lang.NullPointerException - if either argument is null.
+     * Same as eval(String, ScriptContext) where the source of the script is read from a Reader.
+     *
+     * @param reader        The source of the script to be executed by the script engine.
+     * @param scriptContext The ScriptContext passed to the script engine.
+     * @return The value returned from the execution of the script.
+     * @throws ScriptException if an error occurrs in script.
+     *                         java.lang.NullPointerException - if either argument is null.
      */
     public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
 
-        if(reader == null) {
-         throw new NullPointerException("Reader passed cannot be null");
+        if (reader == 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();
+        Writer outPut;
+        if (scriptContext.getWriter() != null) {
+            outPut = scriptContext.getWriter();
         } else {
-         outPut = new StringWriter();
+            outPut = new StringWriter();
         }
         boolean result;
 
@@ -230,65 +231,69 @@ public class VelocityScriptEngine implem
             BufferedReader bf = new BufferedReader(reader);
             String vm = bf.readLine();
             //Check for velocity tools vm file
-            if(vm != null && vm.endsWith(".vm")) {
+            if (vm != null && vm.endsWith(".vm")) {
                 Template template = velocityEngine.getTemplate(vm);
-                template.merge(velocityContext,outPut);
+                template.merge(velocityContext, outPut);
             }
-           result = velocityEngine.evaluate(velocityContext,outPut,fileName,reader);
-        } catch(Exception exp) {
+            result = velocityEngine.evaluate(velocityContext, outPut, fileName, reader);
+        } catch (Exception exp) {
             return new ScriptException(exp);
         }
         return String.valueOf(result);
     }
 
     /**
-     *  Executes the specified script. The default ScriptContext for the ScriptEngine is used.
+     * Executes the specified script. The default ScriptContext for the ScriptEngine is used.
+     *
      * @param s The script language source to be executed.
-     * @return  The value returned from the execution of the script.
-     * @throws ScriptException   - if error occurrs in script.
-     * java.lang.NullPointerException - if either argument is null.
+     * @return The value returned from the execution of the script.
+     * @throws ScriptException - if error occurrs in script.
+     *                         java.lang.NullPointerException - if either argument is null.
      */
     public Object eval(String s) throws ScriptException {
-        return eval(s,scriptContext);
+        return eval(s, scriptContext);
     }
 
 
-  /**
-     *  Same as eval(String) except that the source of the script is provided as a Reader
-     * @param reader  The source of the script.
-     * @return  The value returned by the script.
+    /**
+     * Same as eval(String) except that the source of the script is provided as a Reader
+     *
+     * @param reader The source of the script.
+     * @return The value returned by the script.
      * @throws ScriptException
      */
     public Object eval(Reader reader) throws ScriptException {
-        return eval(reader,scriptContext);
+        return eval(reader, scriptContext);
     }
 
 
-   /**
-     *   Executes the script using the Bindings argument as the ENGINE_SCOPE Bindings of the ScriptEngine during the script
-     *   execution. The Reader, Writer and non-ENGINE_SCOPE Bindings of the default ScriptContext are used.
-     *   The ENGINE_SCOPE Bindings of the ScriptEngine is not changed, and its mappings are unaltered by the script execution.
-     * @param s  The source for the script.
-     * @param bindings  The Bindings of attributes to be used for script execution.
-     * @return  The value returned by the script.
+    /**
+     * Executes the script using the Bindings argument as the ENGINE_SCOPE Bindings of the ScriptEngine during the script
+     * execution. The Reader, Writer and non-ENGINE_SCOPE Bindings of the default ScriptContext are used.
+     * The ENGINE_SCOPE Bindings of the ScriptEngine is not changed, and its mappings are unaltered by the script execution.
+     *
+     * @param s        The source for the script.
+     * @param bindings The Bindings of attributes to be used for script execution.
+     * @return The value returned by the script.
      * @throws ScriptException
      */
     public Object eval(String s, Bindings bindings) throws ScriptException {
-         ScriptContext scriptContext = getGeneratedScriptContextFromBinding(bindings);
-         return eval(new StringReader(s),scriptContext);
+        ScriptContext scriptContext = getGeneratedScriptContextFromBinding(bindings);
+        return eval(new StringReader(s), scriptContext);
     }
 
 
     /**
-     *  Same as eval(String, Bindings) except that the source of the script is provided as a Reader.
-     * @param reader  The source of the script.
-     * @param bindings  The Bindings of attributes to be used for script execution.
-     * @return  The value returned by the script.
+     * Same as eval(String, Bindings) except that the source of the script is provided as a Reader.
+     *
+     * @param reader   The source of the script.
+     * @param bindings The Bindings of attributes to be used for script execution.
+     * @return The value returned by the script.
      * @throws ScriptException
      */
     public Object eval(Reader reader, Bindings bindings) throws ScriptException {
-       ScriptContext scriptContext = getGeneratedScriptContextFromBinding(bindings);
-        return eval(reader,scriptContext);
+        ScriptContext scriptContext = getGeneratedScriptContextFromBinding(bindings);
+        return eval(reader, scriptContext);
     }
 
     private ScriptContext getGeneratedScriptContextFromBinding(Bindings bindings) {
@@ -296,12 +301,12 @@ public class VelocityScriptEngine implem
         Bindings globalScope = scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE);
 
         //Setting global and engine scopes to context
-        if(globalScope != null) {
-            tmpContext.setBindings(globalScope,ScriptContext.GLOBAL_SCOPE);
+        if (globalScope != null) {
+            tmpContext.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
         }
 
-        if(bindings != null) {
-           tmpContext.setBindings(bindings,ScriptContext.ENGINE_SCOPE);
+        if (bindings != null) {
+            tmpContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
         } else {
             throw new NullPointerException("Engine scope Bindings cannot be null.");
         }
@@ -313,48 +318,50 @@ public class VelocityScriptEngine implem
         return tmpContext;
     }
 
-  /**
-     *  Sets a key/value pair in the state of the ScriptEngine that may either create a Java Language Binding to be used in the
-     *  execution of scripts or be used in some other way, depending on whether the key is reserved. Must have the same effect
-     *  as getBindings(ScriptContext.ENGINE_SCOPE).put.
+    /**
+     * Sets a key/value pair in the state of the ScriptEngine that may either create a Java Language Binding to be used in the
+     * execution of scripts or be used in some other way, depending on whether the key is reserved. Must have the same effect
+     * as getBindings(ScriptContext.ENGINE_SCOPE).put.
+     *
      * @param s The name of named value to add
-     * @param o  The value of named value to add.
-     * Throws:
-     * java.lang.NullPointerException - if key is null.
-     * java.lang.IllegalArgumentException - if key is empty.
+     * @param o The value of named value to add.
+     *          Throws:
+     *          java.lang.NullPointerException - if key is null.
+     *          java.lang.IllegalArgumentException - if key is empty.
      */
     public void put(String s, Object o) {
 
-        if(s == null) {
-          throw new NullPointerException("Name cannot be null");
+        if (s == null) {
+            throw new NullPointerException("Name cannot be null");
         }
 
-        if("".equals(s)) {
+        if ("".equals(s)) {
             throw new IllegalArgumentException("Name cannot be empty");
         }
 
-       Bindings engineScope = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
-       engineScope.put(s,o);
+        Bindings engineScope = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+        engineScope.put(s, o);
     }
 
 
-  /**
-     *  Retrieves a value set in the state of this engine. The value might be one which was set using setValue or some other value in
-     *  the state of the ScriptEngine, depending on the implementation. Must have the same effect as getBindings
-     *  (ScriptContext.ENGINE_SCOPE).get
-     * @param s  The key whose value is to be returned
+    /**
+     * Retrieves a value set in the state of this engine. The value might be one which was set using setValue or some other value in
+     * the state of the ScriptEngine, depending on the implementation. Must have the same effect as getBindings
+     * (ScriptContext.ENGINE_SCOPE).get
+     *
+     * @param s The key whose value is to be returned
      * @return the value for the given key
-     * Throws:
-     * java.lang.NullPointerException - if key is null.
-     * java.lang.IllegalArgumentException - if key is empty.
+     *         Throws:
+     *         java.lang.NullPointerException - if key is null.
+     *         java.lang.IllegalArgumentException - if key is empty.
      */
     public Object get(String s) {
 
-        if(s == null) {
-          throw new NullPointerException("Name cannot be null");
+        if (s == null) {
+            throw new NullPointerException("Name cannot be null");
         }
 
-        if("".equals(s)) {
+        if ("".equals(s)) {
             throw new IllegalArgumentException("Name cannot be empty");
         }
 
@@ -363,54 +370,57 @@ public class VelocityScriptEngine implem
     }
 
 
-  /**
-     *  The Bindings instances that are returned must be identical to those returned by the getBindings method of ScriptContext
-     *  called with corresponding arguments on the default ScriptContext of the ScriptEngine.
-     * @param i  scope
-     * @return  The Bindings with the specified scope.
+    /**
+     * The Bindings instances that are returned must be identical to those returned by the getBindings method of ScriptContext
+     * called with corresponding arguments on the default ScriptContext of the ScriptEngine.
+     *
+     * @param i scope
+     * @return The Bindings with the specified scope.
      */
     public Bindings getBindings(int i) {
         return scriptContext.getBindings(i);
     }
 
 
-  /**
-     *  Sets a scope of named values to be used by scripts. The possible scopes are:
+    /**
+     * Sets a scope of named values to be used by scripts. The possible scopes are:
      * ScriptContext.ENGINE_SCOPE - The specified Bindings replaces the engine scope of the ScriptEngine.
      * ScriptContext.GLOBAL_SCOPE - The specified Bindings must be visible as the GLOBAL_SCOPE.
      * Any other value of scope defined in the default ScriptContext of the ScriptEngine.
+     *
      * @param bindings The Bindings for the specified scope.
-     * @param i  The specified scope. Either ScriptContext.ENGINE_SCOPE, ScriptContext.GLOBAL_SCOPE, or any other valid value of scope.
+     * @param i        The specified scope. Either ScriptContext.ENGINE_SCOPE, ScriptContext.GLOBAL_SCOPE, or any other valid value of scope.
      */
     public void setBindings(Bindings bindings, int i) {
-        scriptContext.setBindings(bindings,i);
+        scriptContext.setBindings(bindings, i);
     }
 
 
-  /**
-     *
+    /**
      * @return A Bindings that can be used to replace the state of this ScriptEngine.
      */
     public Bindings createBindings() {
         return new VelocityBindings();
     }
 
-   /**
+    /**
      * Returns the default ScriptContext of the ScriptEngine whose Bindings, Reader and Writers are used for script executions when no ScriptContext is specified.
-     * @return  The default ScriptContext of the ScriptEngine.
+     *
+     * @return The default ScriptContext of the ScriptEngine.
      */
     public ScriptContext getContext() {
-        return  scriptContext;
+        return scriptContext;
     }
 
 
-  /**
-     *  Sets the default ScriptContext of the ScriptEngine whose Bindings, Reader and Writers are used for script executions when no ScriptContext is specified.
-     * @param scriptContext   A ScriptContext that will replace the default ScriptContext in the ScriptEngine.
-     * Throws: java.lang.NullPointerException - if context is null.
+    /**
+     * Sets the default ScriptContext of the ScriptEngine whose Bindings, Reader and Writers are used for script executions when no ScriptContext is specified.
+     *
+     * @param scriptContext A ScriptContext that will replace the default ScriptContext in the ScriptEngine.
+     *                      Throws: java.lang.NullPointerException - if context is null.
      */
     public void setContext(ScriptContext scriptContext) {
-        if(scriptContext == null) {
+        if (scriptContext == null) {
             throw new NullPointerException("script context cannot be null");
         }
 
@@ -420,18 +430,18 @@ public class VelocityScriptEngine implem
     private VelocityContext getVelocityContext(ScriptContext ctx) {
         Bindings engineScope = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
         if (ctx.getBindings(ScriptContext.GLOBAL_SCOPE) == null) {
-            return  new VelocityContext(engineScope);
+            return new VelocityContext(engineScope);
         } else {
             return new VelocityContext(engineScope, new VelocityContext(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)));
         }
     }
 
     private String getTargetFilename(ScriptContext ctx) {
-      Object fileName = ctx.getAttribute(ScriptEngine.FILENAME);
-        if(fileName != null) {
-         return fileName.toString();
+        Object fileName = ctx.getAttribute(ScriptEngine.FILENAME);
+        if (fileName != null) {
+            return fileName.toString();
         } else {
-         return "No-Such-File";
+            return "No-Such-File";
         }
     }
 }
\ No newline at end of file