Author: dishara
Date: Sat Jun 9 19:23:20 2012
New Revision: 1348489
URL: http://svn.apache.org/viewvc?rev=1348489&view=rev
Log:
Fully implemented the VelocityScriptContext class according to jsr 223 spec
Modified:
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptContext.java
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java
Modified:
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptContext.java
URL:
http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptContext.java?rev=1348489&r1=1348488&r2=1348489&view=diff
==============================================================================
---
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptContext.java
(original)
+++
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptContext.java
Sat Jun 9 19:23:20 2012
@@ -19,66 +19,169 @@ package org.apache.velocity.script;
* under the License.
*/
+import org.apache.velocity.VelocityContext;
+
import javax.script.Bindings;
import javax.script.ScriptContext;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
public class VelocityScriptContext implements ScriptContext {
- public void setBindings(Bindings bindings, int i) {
- //To change body of implemented methods use File | Settings | File
Templates.
+
+ private Bindings engineScope;
+ private Bindings globalScope;
+ private Reader reader;
+ private Writer writer;
+ private Writer errorWriter;
+ private static List<Integer> scopes;
+
+
+ public VelocityScriptContext() {
+ init();
+ }
+
+ private void init() {
+ engineScope = new VelocityBindings();
+ globalScope = null;
+ reader = new InputStreamReader(System.in);
+ writer = new PrintWriter(System.out, true);
+ errorWriter = new PrintWriter(System.err, true);
+ }
+
+
+ public void setBindings(Bindings bindings, int i) {
+ switch (i){
+ case ENGINE_SCOPE:
+ if(bindings == null) {
+ throw new NullPointerException("Engine scope cannot be null");
+ }
+ engineScope = bindings;
+ break;
+
+ case GLOBAL_SCOPE:
+ globalScope=bindings;
+ break;
+
+ default:
+ throw new IllegalArgumentException("Invalid scope value");
+ }
+
}
public Bindings getBindings(int i) {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ switch (i) {
+ case ENGINE_SCOPE:
+ return engineScope;
+ case GLOBAL_SCOPE:
+ return globalScope;
+ default:
+ throw new IllegalArgumentException("Invalid scope value");
+ }
}
public void setAttribute(String s, Object o, int i) {
- //To change body of implemented methods use File | Settings | File
Templates.
+ switch (i) {
+ case ENGINE_SCOPE:
+ engineScope.put(s,o);
+ return;
+ case GLOBAL_SCOPE:
+ if(globalScope != null) {
+ globalScope.put(s,o);
+ }
+ return;
+ default:
+ throw new IllegalArgumentException("Invalid scope value");
+ }
+
}
public Object getAttribute(String s, int i) {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
- }
+ switch (i) {
+ case ENGINE_SCOPE:
+ return engineScope.get(s);
+ case GLOBAL_SCOPE:
+ if(globalScope != null) {
+ return globalScope.get(s);
+ }
+ return null;
+ default:
+ throw new IllegalArgumentException("Invalid scope value");
+ }
+}
public Object removeAttribute(String s, int i) {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ switch (i) {
+ case ENGINE_SCOPE:
+ return engineScope.remove(s);
+ case GLOBAL_SCOPE:
+ if(globalScope != null) {
+ return globalScope.remove(s);
+ }
+ return null;
+ default:
+ throw new IllegalArgumentException("Invalid scope value");
+ }
+
}
public Object getAttribute(String s) {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ if(engineScope.containsKey(s)) {
+ return getAttribute(s,ENGINE_SCOPE);
+ } else if (globalScope != null && (globalScope.containsKey(s))) {
+ return getAttribute(s,GLOBAL_SCOPE);
+ }
+ return null;
}
public int getAttributesScope(String s) {
- return 0; //To change body of implemented methods use File | Settings
| File Templates.
+ if(engineScope.containsKey(s)) {
+ return ENGINE_SCOPE;
+ } else if(globalScope != null && globalScope.containsKey(s)) {
+ return GLOBAL_SCOPE;
+ } else {
+ return -1;
+ }
}
public Writer getWriter() {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ return writer;
}
public Writer getErrorWriter() {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ return errorWriter;
}
public void setWriter(Writer writer) {
- //To change body of implemented methods use File | Settings | File
Templates.
+ this.writer = writer;
}
public void setErrorWriter(Writer writer) {
- //To change body of implemented methods use File | Settings | File
Templates.
+ this.errorWriter = writer;
}
public Reader getReader() {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ return reader;
}
public void setReader(Reader reader) {
- //To change body of implemented methods use File | Settings | File
Templates.
+ this.reader = reader;
}
public List<Integer> getScopes() {
- return null; //To change body of implemented methods use File |
Settings | File Templates.
+ return scopes;
+ }
+
+
+ //Adding the two scopes the api supports in to a unmodifiable list
+ static {
+ scopes = new ArrayList<Integer>(2);
+ scopes.add(ENGINE_SCOPE);
+ scopes.add(GLOBAL_SCOPE);
+ scopes = Collections.unmodifiableList(scopes);
}
}
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=1348489&r1=1348488&r2=1348489&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
Sat Jun 9 19:23:20 2012
@@ -22,50 +22,156 @@ package org.apache.velocity.script;
import org.apache.velocity.app.VelocityEngine;
import javax.script.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.io.Reader;
+import java.util.Properties;
-public class VelocityScriptEngine extends AbstractScriptEngine {
+public class VelocityScriptEngine implements ScriptEngine {
private ScriptEngineFactory scriptEngineFactory;
private VelocityEngine velocityEngine;
+ public static final String VELOCITY_PROPERTIES =
"org.apache.velocity.engine.properties";
public VelocityScriptEngine(ScriptEngineFactory scriptEngineFactory) {
- this.scriptEngineFactory=scriptEngineFactory;
+ this.scriptEngineFactory = scriptEngineFactory;
}
- public Object eval(String s, ScriptContext scriptContext) throws
ScriptException {
- return null;
+ /**
+ * @return a ScriptEngineFactory , if null return a newly created one .
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..
+ */
+ public ScriptEngineFactory getFactory() {
+ if (scriptEngineFactory == null) {
+ createNewFactory();
+ }
+ return scriptEngineFactory;
}
- public Object eval(Reader reader, ScriptContext scriptContext) throws
ScriptException {
- return null;
+ private void createNewFactory() {
+ synchronized (this) {
+ if (scriptEngineFactory == null) {
+ scriptEngineFactory = new VelocityScriptEngineFactory();
+ }
+ }
}
- public Bindings createBindings() {
+ private void constructVelocityEngine(ScriptContext context) {
+
+ Properties props = getPropertiesFromContext(context);
+ //Check if property exists in context
+ if (props != null) {
+ initVelocityEngine(props);
+ return;
+ } else {
+ props = getPropertiesFromSystem();
+ //Check if properties exists in System
+ if (props != null) {
+ initVelocityEngine(props);
+ return;
+ }
+ }
+ //Init velocity engine with default settings
+ initVelocityEngine();
+ }
+
+ private void initVelocityEngine() {
+ if (velocityEngine != null) {
+ synchronized (this) {
+ velocityEngine = new VelocityEngine();
+ velocityEngine.init();
+ }
+ }
+ }
+
+ private void initVelocityEngine(Properties props) {
+ if (velocityEngine != null) {
+ synchronized (this) {
+ velocityEngine = new VelocityEngine();
+ velocityEngine.init(props);
+ }
+ }
+ }
+
+ private Properties getPropertiesFromSystem() {
+ String propFileName = System.getProperty(VELOCITY_PROPERTIES);
+ File propFile = new File(propFileName);
+ if (propFile.exists()) {
+ Properties properties = new Properties();
+ try {
+ properties.load(new FileInputStream(propFile));
+ return properties;
+ } catch (IOException e) {
+ //TODO log error msg
+ return null;
+ }
+ }
+ //TODO log error msg saying no such property file
return null;
}
- /**
- *
- * @return a ScriptEngineFactory , if null return a newly created one .
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..
- */
- public ScriptEngineFactory getFactory() {
- if(scriptEngineFactory == null) {
- createNewFactory();
+ private Properties getPropertiesFromContext(ScriptContext context) {
+ Object props = context.getAttribute(VELOCITY_PROPERTIES);
+ if (props instanceof Properties) {
+ return (Properties) props;
+ } else {
+ return null;
}
- return scriptEngineFactory;
}
- private void createNewFactory(){
- synchronized (this) {
- if(scriptEngineFactory == null){
- scriptEngineFactory = new VelocityScriptEngineFactory();
+ public Object eval(String s, ScriptContext scriptContext) throws
ScriptException {
+
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Object eval(Reader reader, ScriptContext scriptContext) throws
ScriptException {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Object eval(String s) throws ScriptException {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Object eval(Reader reader) throws ScriptException {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Object eval(String s, Bindings bindings) throws ScriptException {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Object eval(Reader reader, Bindings bindings) throws
ScriptException {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public void put(String s, Object o) {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public Object get(String s) {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public Bindings getBindings(int i) {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
}
-}
-}
+ public void setBindings(Bindings bindings, int i) {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public Bindings createBindings() {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+ public ScriptContext getContext() {
+ return null; //To change body of implemented methods use File |
Settings | File Templates.
+ }
+
+ public void setContext(ScriptContext scriptContext) {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
}
\ No newline at end of file
Modified:
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java
URL:
http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java?rev=1348489&r1=1348488&r2=1348489&view=diff
==============================================================================
---
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java
(original)
+++
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java
Sat Jun 9 19:23:20 2012
@@ -167,10 +167,27 @@ public class VelocityScriptEngineFactory
/**
*
* @param s String to display
- * @return //TODO
+ * @return //TODO
*/
public String getOutputStatement(String s) {
- return null;
+ StringBuilder output = new StringBuilder();
+ output.append("${context.getWriter().write(\"");
+ for (int i = 0; i < s.length(); i++) {
+ char ch = s.charAt(i);
+ switch (ch) {
+ case '"':
+ output.append("\\\"");
+ break;
+ case '\\':
+ output.append("\\\\");
+ break;
+ default:
+ output.append(ch);
+ break;
+ }
+ }
+ output.append("\")}");
+ return output.toString();
}
/**