Source code: com/vinculum/javascript/Shell.java
1 /* * ** ** BEGIN LICENSE BLOCK * ** **
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Vinculum Open Source.
15 *
16 * The Initial Developer of the Original Code is
17 * Gerard Toonstra.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ** ** * END LICENSE BLOCK * ** **
36 */
37
38 /***************************************************************************
39 $RCSfile: Shell.java,v $ - description
40 -------------------
41 begin : $Date: 2003/08/27 19:11:48 $
42 copyright : Vinculum (C) 2002
43 author : $Author: chiraz $
44 ***************************************************************************/
45
46 /**
47 * $Log: Shell.java,v $
48 * Revision 1.3 2003/08/27 19:11:48 chiraz
49 * Changed classpath, making it loadable directly by eclipse
50 *
51 * Revision 1.2 2003/08/03 09:06:32 chiraz
52 * Changes required to make JavaScript processing work.
53 *
54 * Revision 1.1.1.1 2003/07/08 07:41:47 chiraz
55 * egg
56 **/
57
58 package com.vinculum.javascript;
59
60 import org.mozilla.javascript.*;
61
62 import java.io.*;
63
64 /**
65 * @author chilan
66 *
67 */
68 public class Shell extends ScriptableObject
69 {
70 // If anything is wrong here, it must be the context that
71 // I enter and exit.
72 private static Shell global = null;
73 private static Context globalcx = null;
74 private static Scriptable globalScope = null;
75
76 public static Shell getInstance()
77 {
78 if ( global == null )
79 {
80 globalcx = Context.enter();
81 global = new Shell();
82 globalScope = getScope();
83 }
84
85 return global;
86 }
87
88 public static void attachThread()
89 {
90 Context blah = Context.enter();
91 }
92
93 public static Scriptable getScope()
94 {
95 Scriptable scope = null;
96
97 try
98 {
99 scope = globalcx.initStandardObjects(null);
100 }
101 finally
102 {
103
104 }
105 return scope;
106 }
107
108 public String getClassName()
109 {
110 return "global";
111 }
112
113 public static void putInScope( Scriptable scope, String name, Object obj )
114 {
115 scope.put(name, scope, Context.toObject( obj, scope ) );
116 }
117
118 public boolean handleExpression(Scriptable scope, String sourceName, String expression, boolean isScript )
119 throws JavaScriptException
120 {
121 Object result;
122
123 if ( isScript )
124 {
125 readScriptInScope(scope, sourceName);
126 Object[] args = new Object[0];
127 result = callFunction( scope, expression, args );
128 }
129 else
130 {
131 result = globalcx.evaluateString(scope, expression, sourceName, 1, null);
132 }
133 if ( result instanceof org.mozilla.javascript.Undefined )
134 {
135 return true;
136 }
137 if ( result.equals(Boolean.TRUE))
138 {
139 return true;
140 }
141 else
142 {
143 return false;
144 }
145 }
146
147 private void readScriptInScope(Scriptable scope, String fileName)
148 {
149 FileReader in = null;
150 try
151 {
152 in = new FileReader(fileName);
153 }
154 catch (FileNotFoundException ex)
155 {
156 Context.reportError("Couldn't open file \"" + fileName + "\".");
157 return;
158 }
159
160 try
161 {
162 // Here we evalute the entire contents of the file as
163 // a script. Text is printed only if the print() function
164 // is called.
165 globalcx.evaluateReader(scope, in, fileName, 1, null);
166 }
167 catch (WrappedException we)
168 {
169 System.err.println(we.getWrappedException().toString());
170 we.printStackTrace();
171 }
172 catch (EvaluatorException ee)
173 {
174 System.err.println("js: " + ee.getMessage());
175 }
176 catch (JavaScriptException jse)
177 {
178 System.err.println("js: " + jse.getMessage());
179 }
180 catch (IOException ioe)
181 {
182 System.err.println(ioe.toString());
183 }
184 finally
185 {
186 try
187 {
188 in.close();
189 }
190 catch (IOException ioe)
191 {
192 System.err.println(ioe.toString());
193 }
194 }
195 }
196
197 private Object callFunction( Scriptable scope, String functionName, Object[] args )
198 {
199 Function function = null;
200 Object result = null;
201
202 function = (Function)scope.get(functionName, scope );
203 try
204 {
205 result = function.call(globalcx, scope, scope, args );
206 }
207 catch (JavaScriptException e)
208 {
209 e.printStackTrace();
210 }
211
212 return result;
213 }
214 }