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 15:11:01 UTC

svn commit: r391040 - 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 06:10:59 2006
New Revision: 391040

URL: http://svn.apache.org/viewcvs?rev=391040&view=rev
Log:
Support scripts from URLs

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=391040&r1=391039&r2=391040&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 06:10:59 2006
@@ -19,7 +19,10 @@
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.StringReader;
+import java.net.URL;
+import java.net.URLConnection;
 
 import org.apache.commons.jexl.parser.ASTJexlScript;
 import org.apache.commons.jexl.parser.Parser;
@@ -113,7 +116,32 @@
             buffer.append(line).append('\n');
         }
         reader.close();
-        return getInstance().newScript(buffer.toString());
+        return createScript(buffer.toString());
+    }
+
+    /**
+     * Creates a Script from a {@link URL} containing valid JEXL syntax. 
+     * This method parses the script and validates the syntax.
+     * 
+     * @param scriptUrl A {@link URL} containing valid JEXL syntax. Must not be null. Must be a readable file.
+     * @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(URL scriptUrl) throws Exception
+    {
+        if (scriptUrl == null) {
+            throw new NullPointerException("scriptUrl passed to ScriptFactory.createScript is null");
+        }
+        URLConnection connection = scriptUrl.openConnection();
+        
+        StringBuffer buffer = new StringBuffer();
+        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+        String line = null;
+        while ((line = reader.readLine()) != null) {
+            buffer.append(line).append('\n');
+        }
+        reader.close();
+        return createScript(buffer.toString());
     }
 
     /**

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=391040&r1=391039&r2=391040&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 06:10:59 2006
@@ -16,6 +16,7 @@
 package org.apache.commons.jexl;
 
 import java.io.File;
+import java.net.URL;
 
 import junit.framework.TestCase;
 
@@ -49,5 +50,14 @@
     public void testCreateFromFile() throws Exception {
         File testScript = new File("src/test-scripts/test1.jexl");
         assertNotNull("No script created", ScriptFactory.createScript(testScript));
+    }
+
+    /**
+     * Ensure the factory can create a script from a URL.
+     * @throws Exception on a parse error.
+     */
+    public void testCreateFromURL() throws Exception {
+        URL testUrl = new File("src/test-scripts/test1.jexl").toURL();
+        assertNotNull("No script created", ScriptFactory.createScript(testUrl));
     }
 }

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=391040&r1=391039&r2=391040&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 06:10:59 2006
@@ -16,6 +16,7 @@
 package org.apache.commons.jexl;
 
 import java.io.File;
+import java.net.URL;
 
 import junit.framework.TestCase;
 
@@ -50,6 +51,16 @@
     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);
+    }
+
+    public void testScriptFromURL() throws Exception {
+        URL testUrl = new File("src/test-scripts/test1.jexl").toURL();
+        Script s = ScriptFactory.createScript(testUrl);
         JexlContext jc = JexlHelper.createContext();
         jc.getVars().put("out", System.out);
         Object result = s.execute(jc);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org