KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > js > JSHelper


1 package org.openlaszlo.iv.flash.js;
2
3 import java.io.*;
4 import java.net.URL JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
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 /**
15  * @author Norris Boyd
16  */

17 public class JSHelper {
18
19     private static Context init( String JavaDoc args[], PrintStream out, org.openlaszlo.iv.flash.context.Context genContext ) {
20         Context cx = Context.enter();
21         // Create the top-level scope object.
22
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         // define "arguments" array in the top-level object
30
if( args == null ) args = new String JavaDoc[0];
31         Object JavaDoc[] array = args;
32         Scriptable argsObj = cx.newArray(global, args);
33         global.defineProperty("arguments", argsObj,
34                               ScriptableObject.DONTENUM);
35         return cx;
36     }
37
38     /**
39      * Execute the given arguments
40      */

41     public static int execFile(String JavaDoc fileName, String JavaDoc 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     /**
53      * Execute the given arguments
54      */

55     public static int execFile(String JavaDoc fileName, String JavaDoc 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     /**
67      * Execute the given arguments
68      */

69     public static int execString( String JavaDoc js_text, String JavaDoc 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 JavaDoc 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 JavaDoc filename)
102     {
103         Reader in = null;
104         // Try filename first as URL
105
try {
106             URL JavaDoc url = new URL JavaDoc(filename);
107             InputStream is = url.openStream();
108             in = new BufferedReader(new InputStreamReader(is));
109         } catch (MalformedURLException JavaDoc mfex) {
110             // fall through to try it as a file
111
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 filename as file
121
try {
122                 in = new FileReader(filename);
123                 filename = new java.io.File JavaDoc(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         // Here we evalute the entire contents of the file as
135
// a script. Text is printed only if the print() function
136
// is called.
137
evaluateReader(cx, scope, in, filename, 1);
138     }
139
140     public static Object JavaDoc evaluateReader(Context cx, Scriptable scope,
141                                         Reader in, String JavaDoc sourceName,
142                                         int lineno)
143     {
144         Object JavaDoc 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 JavaDoc 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             // Already printed message.
163
exitCode = EXITCODE_RUNTIME_ERROR;
164         } catch (JavaScriptException jse) {
165             // Need to propagate ThreadDeath exceptions.
166
Object JavaDoc value = jse.getValue();
167             if (value instanceof ThreadDeath JavaDoc)
168                 throw (ThreadDeath JavaDoc) 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 /* public static Node parseReader(Context cx, Scriptable scope, Reader in, String sourceName, int lineno) {
186         try {
187             TokenStream ts = new TokenStream(in, scope, sourceName, lineno);
188             IRFactory irf = new IRFactory(ts, scope);
189             Parser p = new Parser(irf);
190             Node tree = (Node) p.parse(ts);
191             if( tree == null ) {
192                 System.out.println(tree.toStringTree());
193                 return tree;
194             }
195             return null;
196         } catch (WrappedException we) {
197             Log.logRB(Resource.JSERROR, we);
198         } catch (EcmaError ee) {
199             String msg = IVErrorReporter.getMessage(
200                 "msg.uncaughtJSException", ee.toString());
201             exitCode = EXITCODE_RUNTIME_ERROR;
202             if (ee.getSourceName() != null) {
203                 Context.reportError(msg, ee.getSourceName(),
204                                     ee.getLineNumber(),
205                                     ee.getLineSource(),
206                                     ee.getColumnNumber());
207             } else {
208                 Context.reportError(msg);
209             }
210         } catch (JavaScriptException jse) {
211             // Need to propagate ThreadDeath exceptions.
212             Object value = jse.getValue();
213             if (value instanceof ThreadDeath)
214                 throw (ThreadDeath) value;
215             exitCode = EXITCODE_RUNTIME_ERROR;
216             Context.reportError(IVErrorReporter.getMessage(
217                 "msg.uncaughtJSException",
218                 jse.getMessage()));
219         } catch (IOException ioe) {
220             Log.logRB(Resource.JSERROR, ioe);
221         } finally {
222             try {
223                 in.close();
224             } catch (IOException ioe) {
225                 Log.logRB(Resource.JSERROR, ioe);
226             }
227         }
228         return null;
229     }
230 */

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     //static private DebugShell debugShell;
265
}
266
267
Popular Tags