You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2006/04/03 14:29:25 UTC
svn commit: r391033 - in /jakarta/commons/proper/jexl/trunk/src:
java/org/apache/commons/jexl/ScriptFactory.java
test/org/apache/commons/jexl/ScriptFactoryTest.java
test/org/apache/commons/jexl/ScriptTest.java
Author: dion
Date: Mon Apr 3 05:29:23 2006
New Revision: 391033
URL: http://svn.apache.org/viewcvs?rev=391033&view=rev
Log:
Support scripts from a file
Modified:
jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java
jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptFactoryTest.java
jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptTest.java
Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java?rev=391033&r1=391032&r2=391033&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java (original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java Mon Apr 3 05:29:23 2006
@@ -15,6 +15,10 @@
*/
package org.apache.commons.jexl;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
import java.io.StringReader;
import org.apache.commons.jexl.parser.ASTJexlScript;
@@ -86,6 +90,31 @@
return getInstance().newScript(scriptText);
}
+ /**
+ * Creates a Script from a String containing valid JEXL syntax.
+ * This method parses the script which validates the syntax.
+ *
+ * @param scriptText A String containing valid JEXL syntax
+ * @return A {@link Script} which can be executed with a {@link JexlContext}.
+ * @throws Exception An exception can be thrown if there is a problem parsing the script.
+ */
+ public static Script createScript(File scriptFile) throws Exception
+ {
+ if (scriptFile == null) {
+ throw new NullPointerException("scriptFile passed to ScriptFactory.createScript is null");
+ }
+ if (!scriptFile.canRead()) {
+ throw new IOException("Can't read scriptFile (" + scriptFile.getCanonicalPath() +")");
+ }
+ StringBuffer buffer = new StringBuffer();
+ BufferedReader reader = new BufferedReader(new FileReader(scriptFile));
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ buffer.append(line).append('\n');
+ }
+ reader.close();
+ return getInstance().newScript(buffer.toString());
+ }
/**
* Creates a new Script based on the string.
Modified: jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptFactoryTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptFactoryTest.java?rev=391033&r1=391032&r2=391033&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptFactoryTest.java (original)
+++ jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptFactoryTest.java Mon Apr 3 05:29:23 2006
@@ -15,6 +15,8 @@
*/
package org.apache.commons.jexl;
+import java.io.File;
+
import junit.framework.TestCase;
/**
@@ -38,5 +40,14 @@
public void testCreateFromString() throws Exception {
String code = ";";
assertNotNull("No script created", ScriptFactory.createScript(code));
+ }
+
+ /**
+ * Ensure the factory can create a script from a file.
+ * @throws Exception on a parse error.
+ */
+ public void testCreateFromFile() throws Exception {
+ File testScript = new File("src/test-scripts/test1.jexl");
+ assertNotNull("No script created", ScriptFactory.createScript(testScript));
}
}
Modified: jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptTest.java?rev=391033&r1=391032&r2=391033&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptTest.java (original)
+++ jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ScriptTest.java Mon Apr 3 05:29:23 2006
@@ -15,6 +15,8 @@
*/
package org.apache.commons.jexl;
+import java.io.File;
+
import junit.framework.TestCase;
/**
@@ -31,6 +33,9 @@
super(name);
}
+ /**
+ * Test creating a script from a string.
+ */
public void testSimpleScript() throws Exception {
String code = "while (x < 10) x = x + 1;";
Script s = ScriptFactory.createScript(code);
@@ -40,5 +45,15 @@
Object o = s.execute(jc);
assertEquals("Result is wrong", new Long(10), o);
assertEquals("getText is wrong", code, s.getText());
+ }
+
+ public void testScriptFromFile() throws Exception {
+ File testScript = new File("src/test-scripts/test1.jexl");
+ Script s = ScriptFactory.createScript(testScript);
+ JexlContext jc = JexlHelper.createContext();
+ jc.getVars().put("out", System.out);
+ Object result = s.execute(jc);
+ assertNotNull("No result", result);
+ assertEquals("Wrong result", new Long(7), result);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org