1 package org.openlaszlo.iv.flash.js; 2 3 import java.io.*; 4 import java.net.URL ; 5 import java.net.MalformedURLException ; 6 import java.util.*; 7 import java.lang.reflect.*; 8 import org.mozilla.javascript.*; 9 import org.mozilla.javascript.tools.ToolErrorReporter; 10 11 12 import org.openlaszlo.iv.flash.util.*; 13 14 17 public class JSHelper { 18 19 private static Context init( String args[], PrintStream out, org.openlaszlo.iv.flash.context.Context genContext ) { 20 Context cx = Context.enter(); 21 global = getGlobal(genContext); 23 global.setOut(out); 24 errorReporter = new IVErrorReporter(false); 25 cx.setErrorReporter(errorReporter); 26 cx.setLanguageVersion(Context.VERSION_1_5); 27 cx.setOptimizationLevel(-1); 28 29 if( args == null ) args = new String [0]; 31 Object [] array = args; 32 Scriptable argsObj = cx.newArray(global, args); 33 global.defineProperty("arguments", argsObj, 34 ScriptableObject.DONTENUM); 35 return cx; 36 } 37 38 41 public static int execFile(String fileName, String args[], PrintStream out) { 42 43 Context cx = init(args, out, null); 44 45 processFile(cx, global, fileName); 46 47 cx.exit(); 48 49 return exitCode; 50 } 51 52 55 public static int execFile(String fileName, String args[], PrintStream out, org.openlaszlo.iv.flash.context.Context genContext) { 56 57 Context cx = init(args, out, genContext); 58 59 processFile(cx, global, fileName); 60 61 cx.exit(); 62 63 return exitCode; 64 } 65 66 69 public static int execString( String js_text, String args[], 70 PrintStream out, org.openlaszlo.iv.flash.context.Context genContext ) 71 { 72 Context cx = init(args, out, genContext); 73 74 StringReader in = new StringReader(js_text); 75 String source = js_text.length()>20? js_text.substring(0,20)+"...": js_text; 76 evaluateReader(cx, global, in, source, 1); 77 78 cx.exit(); 79 return exitCode; 80 } 81 82 public static Global getGlobal() { 83 return getGlobal(null); 84 } 85 86 public static Global getGlobal(org.openlaszlo.iv.flash.context.Context genContext) { 87 if (global == null) { 88 try { 89 global = new Global(Context.enter(), genContext); 90 } finally { 91 Context.exit(); 92 } 93 } 94 if( global != null ) { 95 global.genContext = genContext; 96 } 97 return global; 98 } 99 100 public static void processFile(Context cx, Scriptable scope, 101 String filename) 102 { 103 Reader in = null; 104 try { 106 URL url = new URL (filename); 107 InputStream is = url.openStream(); 108 in = new BufferedReader(new InputStreamReader(is)); 109 } catch (MalformedURLException mfex) { 110 in = null; 112 } catch (IOException ioex) { 113 Context.reportError(IVErrorReporter.getMessage( 114 "msg.couldnt.open.url", filename, ioex.toString())); 115 exitCode = EXITCODE_FILE_NOT_FOUND; 116 return; 117 } 118 119 if (in == null) { 120 try { 122 in = new FileReader(filename); 123 filename = new java.io.File (filename).getCanonicalPath(); 124 } catch (FileNotFoundException ex) { 125 Context.reportError(IVErrorReporter.getMessage( 126 "msg.couldnt.open", 127 filename)); 128 exitCode = EXITCODE_FILE_NOT_FOUND; 129 return; 130 } catch (IOException ioe) { 131 Log.logRB(Resource.JSERROR, ioe); 132 } 133 } 134 evaluateReader(cx, scope, in, filename, 1); 138 } 139 140 public static Object evaluateReader(Context cx, Scriptable scope, 141 Reader in, String sourceName, 142 int lineno) 143 { 144 Object result = cx.getUndefinedValue(); 145 try { 146 result = cx.evaluateReader(scope, in, sourceName, lineno, null); 147 } catch (WrappedException we) { 148 Log.logRB(Resource.JSERROR, we); 149 } catch (EcmaError ee) { 150 String msg = IVErrorReporter.getMessage( 151 "msg.uncaughtJSException", ee.toString()); 152 exitCode = EXITCODE_RUNTIME_ERROR; 153 if (ee.getSourceName() != null) { 154 Context.reportError(msg, ee.getSourceName(), 155 ee.getLineNumber(), 156 ee.getLineSource(), 157 ee.getColumnNumber()); 158 } else { 159 Context.reportError(msg); 160 } 161 } catch (EvaluatorException ee) { 162 exitCode = EXITCODE_RUNTIME_ERROR; 164 } catch (JavaScriptException jse) { 165 Object value = jse.getValue(); 167 if (value instanceof ThreadDeath ) 168 throw (ThreadDeath ) value; 169 exitCode = EXITCODE_RUNTIME_ERROR; 170 Context.reportError(IVErrorReporter.getMessage( 171 "msg.uncaughtJSException", 172 jse.getMessage())); 173 } catch (IOException ioe) { 174 Log.logRB(Resource.JSERROR, ioe); 175 } finally { 176 try { 177 in.close(); 178 } catch (IOException ioe) { 179 Log.logRB(Resource.JSERROR, ioe); 180 } 181 } 182 return result; 183 } 184 185 231 public static ScriptableObject getScope() { 232 return global; 233 } 234 235 public static InputStream getIn() { 236 return Global.getInstance(getGlobal()).getIn(); 237 } 238 239 public static void setIn(InputStream in) { 240 Global.getInstance(getGlobal()).setIn(in); 241 } 242 243 public static PrintStream getOut() { 244 return Global.getInstance(getGlobal()).getOut(); 245 } 246 247 public static void setOut(PrintStream out) { 248 Global.getInstance(getGlobal()).setOut(out); 249 } 250 251 public static PrintStream getErr() { 252 return Global.getInstance(getGlobal()).getErr(); 253 } 254 255 public static void setErr(PrintStream err) { 256 Global.getInstance(getGlobal()).setErr(err); 257 } 258 259 static protected IVErrorReporter errorReporter; 260 static protected Global global; 261 static protected int exitCode = 0; 262 static private final int EXITCODE_RUNTIME_ERROR = 3; 263 static private final int EXITCODE_FILE_NOT_FOUND = 4; 264 } 266 267 | Popular Tags |