commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r394713 - /jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java
Date Mon, 17 Apr 2006 15:45:17 GMT
Author: dion
Date: Mon Apr 17 08:45:16 2006
New Revision: 394713

URL: http://svn.apache.org/viewcvs?rev=394713&view=rev
Log:
Add readerToString method.
Checkstyle

Modified:
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.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=394713&r1=394712&r2=394713&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 17 08:45:16 2006
@@ -26,6 +26,7 @@
 
 import org.apache.commons.jexl.parser.ASTJexlScript;
 import org.apache.commons.jexl.parser.Parser;
+import org.apache.commons.jexl.parser.SimpleNode;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -109,14 +110,9 @@
         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 createScript(buffer.toString());
+        return createScript(readerToString(reader));
+            
     }
 
     /**
@@ -134,14 +130,8 @@
         }
         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());
+        return createScript(readerToString(reader));
     }
 
     /**
@@ -154,15 +144,16 @@
     protected Script newScript(String scriptText) throws Exception {
         String cleanText = cleanScript(scriptText);
         // Parse the Expression
-        ASTJexlScript tree;
         synchronized(parser)
         {
-            log.debug( "Parsing script: " + cleanText);
-            tree = (ASTJexlScript) parser.parse(new StringReader(cleanText));
+            log.debug("Parsing script: " + cleanText);
+            SimpleNode script = parser.parse(new StringReader(cleanText));
+            if (script instanceof ASTJexlScript) {
+                return new ScriptImpl(cleanText, (ASTJexlScript) script);
+            } else {
+                throw new IllegalStateException("Parsed script is not an ASTJexlScript");
+            }
         }
-
-        return new ScriptImpl(cleanText, tree);
-
     }
 
     /**
@@ -178,6 +169,25 @@
             expr += ";";
         }
         return expr;
+    }
+    
+    /**
+     * Read a buffered reader into a StringBuffer and return a String with the contents of
the reader.
+     * @return the contents of the reader as a String.
+     * @throws IOException on any error reading the reader.
+     */
+    private static String readerToString(BufferedReader reader) throws IOException {
+        StringBuffer buffer = new StringBuffer();
+        try {
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                buffer.append(line).append('\n');
+            }
+            return buffer.toString();
+        } finally {
+            reader.close();
+        }
+
     }
 
 }



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


Mime
View raw message