1 16 package org.apache.commons.jexl; 17 18 import java.io.BufferedReader ; 19 import java.io.File ; 20 import java.io.FileReader ; 21 import java.io.IOException ; 22 import java.io.InputStreamReader ; 23 import java.io.StringReader ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 27 import org.apache.commons.jexl.parser.ASTJexlScript; 28 import org.apache.commons.jexl.parser.ParseException; 29 import org.apache.commons.jexl.parser.Parser; 30 import org.apache.commons.jexl.parser.SimpleNode; 31 import org.apache.commons.jexl.parser.TokenMgrError; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 53 public class ScriptFactory { 54 55 56 protected static Log log = 57 LogFactory.getLog("org.apache.commons.jexl.ScriptFactory"); 58 59 64 protected static Parser parser = new Parser(new StringReader (";")); 65 66 70 protected static ScriptFactory factory = new ScriptFactory(); 71 72 76 private ScriptFactory() { 77 78 } 79 80 84 protected static ScriptFactory getInstance() { 85 return factory; 86 } 87 88 98 public static Script createScript(String scriptText) throws Exception { 99 return getInstance().createNewScript(scriptText); 100 } 101 102 113 public static Script createScript(File scriptFile) throws Exception { 114 if (scriptFile == null) { 115 throw new NullPointerException ("scriptFile is null"); 116 } 117 if (!scriptFile.canRead()) { 118 throw new IOException ("Can't read scriptFile (" 119 + scriptFile.getCanonicalPath() + ")"); 120 } 121 BufferedReader reader = new BufferedReader (new FileReader (scriptFile)); 122 return createScript(readerToString(reader)); 123 124 } 125 126 137 public static Script createScript(URL scriptUrl) throws Exception { 138 if (scriptUrl == null) { 139 throw new NullPointerException ("scriptUrl is null"); 140 } 141 URLConnection connection = scriptUrl.openConnection(); 142 143 BufferedReader reader = new BufferedReader ( 144 new InputStreamReader (connection.getInputStream())); 145 return createScript(readerToString(reader)); 146 } 147 148 155 protected Script createNewScript(String scriptText) throws Exception { 156 String cleanText = cleanScript(scriptText); 157 SimpleNode script; 158 synchronized (parser) { 160 log.debug("Parsing script: " + cleanText); 161 try { 162 script = parser.parse(new StringReader (cleanText)); 163 } catch (TokenMgrError tme) { 164 throw new ParseException(tme.getMessage()); 165 } 166 } 167 if (script instanceof ASTJexlScript) { 168 return new ScriptImpl(cleanText, (ASTJexlScript) script); 169 } else { 170 throw new IllegalStateException ("Parsed script is not " 171 + "an ASTJexlScript"); 172 } 173 } 174 175 181 private String cleanScript(String script) { 182 String expr = script.trim(); 183 if (!expr.endsWith(";")) { 184 expr += ";"; 185 } 186 return expr; 187 } 188 189 196 private static String readerToString(BufferedReader reader) 197 throws IOException { 198 StringBuffer buffer = new StringBuffer (); 199 try { 200 String line = null; 201 while ((line = reader.readLine()) != null) { 202 buffer.append(line).append('\n'); 203 } 204 return buffer.toString(); 205 } finally { 206 reader.close(); 207 } 208 209 } 210 211 } 212 | Popular Tags |