1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
9 import org.mozilla.javascript.*;
11 /** Singleton class that provides all functionality in the xwt.* namespace */
12 public final class XWT extends JSObject {
14 public static final XWT singleton = new XWT();
16 /** each key is a string representing a filename which the user has already given XWT permission to write to */
17 private static Hashtable safeFiles = new Hashtable();
19 public String getClassName() { return "XWT"; }
20 private XWT() { setSeal(true); }
22 public Object get(String name, Scriptable start) {
23 if (name == null) return null;
24 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
25 else if (name.equals("parseFloat")) return JSObject.defaultObjects.get("parseFloat", null);
26 else if (name.equals("parseInt")) return JSObject.defaultObjects.get("parseInt", null);
27 else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
28 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
29 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
30 else if (name.equals("date")) return date;
31 else if (name.equals("listfonts")) return listfonts;
32 else if (name.equals("regexp")) return regexp;
33 else if (name.equals("sleep")) return sleep;
34 else if (name.equals("yield")) return yield;
35 else if (name.equals("newBrowserWindow")) return newBrowserWindow;
36 else if (name.equals("textwidth")) return textwidth;
37 else if (name.equals("textheight")) return textheight;
38 else if (name.equals("newBox")) return newBox;
39 else if (name.equals("soap")) return soap;
40 else if (name.equals("xmlrpc")) return xmlrpc;
41 else if (name.equals("origin")) return Main.origin;
42 else if (name.equals("clipboard")) return Platform.getClipBoard();
43 else if (name.equals("altKeyName")) return Platform.altKeyName();
44 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
45 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
46 else if (name.equals("static")) return Static.getStatic("");
47 else if (name.equals("theme")) return theme;
48 else if (name.equals("openFile")) return openFile;
49 else if (name.equals("saveFile")) return saveFile;
50 else if (name.equals("saveFileAs")) return saveFileAs;
51 else if (name.equals("utfEncode")) return utfEncode;
52 else if (name.equals("fileSeparator")) return File.separator;
53 else if (name.equals("homeDir")) return System.getProperty("user.home");
54 else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
55 else if (name.equals("recursivePrintObject")) return recursivePrintObject;
56 else if (name.equals("parseHTML")) return parseHTML;
57 else if (name.equals("button")) {
58 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
59 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
60 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
61 else return new Integer(0);
63 else if (name.equals("println")) return println;
64 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
65 else return super.get(name, start);
68 public void put(String name, Scriptable start, Object value) {
69 if (name == null) return;
70 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
71 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
72 else super.put(name, start, value);
76 // JSFunction Instances ///////////////////////////////////////////////////////////////////
78 /** Helper class for defining functions. */
79 private static abstract class JSFunction extends JSObject implements Function {
80 JSFunction() { setSeal(true); }
81 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
84 private static final JSFunction newBrowserWindow = new JSFunction() {
85 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
86 if (args.length != 1 || args[0] == null) return null;
87 Platform.newBrowserWindow(args[0].toString());
92 private static final JSFunction yield = new JSFunction() {
93 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
94 sleep.call(cx, null, null, null);
99 private static final JSFunction println = new JSFunction() {
100 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
101 if (args.length == 1)
103 Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
108 private static final JSFunction date = new JSFunction() {
109 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
110 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
111 } catch (Exception e) {
112 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
113 if (Log.on) Log.log(this, e);
119 private static final JSFunction regexp = new JSFunction() {
120 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
121 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
122 } catch (Exception e) {
123 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
124 if (Log.on) Log.log(this, e);
130 private static final JSFunction listfonts = new JSFunction() {
131 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
132 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
136 private static final JSFunction theme = new JSFunction() {
137 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
138 if (args.length != 2) return null;
139 if (args[0] == null || args[1] == null) return null;
140 Template.retheme(args[0].toString(), args[1].toString());
145 private static final JSFunction xmlrpc = new JSFunction() {
146 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
147 if (args.length != 1 || args[0] == null) return null;
148 return new XMLRPC(args[0].toString(), "");
152 private static final JSFunction soap = new JSFunction() {
153 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
154 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
155 else if (args.length == 2 && args[0] != null && args[1] != null)
156 return new SOAP(args[0].toString(), "", args[1].toString(), null);
157 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
158 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
163 private static final JSFunction textwidth = new JSFunction() {
164 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
165 if (args.length < 1 || args.length > 2) return null;
166 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
167 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
168 String text = args.length == 1 ? args[0].toString() : args[1].toString();
169 XWF xwf = XWF.getXWF(font);
170 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
171 else return new Integer(xwf.stringWidth(text));
176 private static final JSFunction textheight = new JSFunction() {
177 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
178 if (args.length > 1) return null;
179 if (args.length == 1 && args[0] == null) return null;
180 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
181 XWF xwf = XWF.getXWF(font);
182 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
183 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
187 private static final JSFunction newBox = new JSFunction() {
188 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
189 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
190 for(int i=1; i<args.length; i++)
191 if (args[i] instanceof Box)
192 ret.put(ret.numChildren(), null, (Box)args[i]);
193 for(int i=1; i<args.length; i++)
194 if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
195 Scriptable s = (Scriptable)args[i];
196 Object[] keys = s.getIds();
198 // FIXME: need to ensure that this is putGlobally(), but still run traps...
199 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
205 private static final JSFunction sleep = new JSFunction() {
206 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
207 if (args != null && (args.length != 1 || args[0] == null)) return null;
208 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
210 Thread thread = Thread.currentThread();
211 if (!(thread instanceof ThreadMessage)) {
212 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
215 ThreadMessage mythread = (ThreadMessage)thread;
216 mythread.done.release();
218 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
220 MessageQueue.add(mythread);
226 private static final JSFunction openFile = new JSFunction() {
227 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
228 if (args.length != 1) return null;
229 String file = Platform.fileDialog(args[0].toString(), false);
230 return file == null ? null : new ByteStream(file);
234 private static final JSFunction saveFile = new JSFunction() {
235 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
236 if (args.length != 2) return null;
237 if (!(args[1] instanceof ByteStream)) return null;
238 String file = args[0].toString();
239 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
240 file = Platform.fileDialog(file, true);
241 if (file == null) return null;
242 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
245 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
247 } catch (IOException e) {
248 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
249 if (Log.on) Log.log(ByteStream.class, e);
250 throw new JavaScriptException("error while writing a ByteStream to a file");
255 private static final JSFunction saveFileAs = new JSFunction() {
256 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
257 if (args.length != 2) return null;
258 if (!(args[1] instanceof ByteStream)) return null;
259 String file = args[0].toString();
260 file = Platform.fileDialog(file, true);
261 if (file == null) return null;
262 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
264 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
266 } catch (IOException e) {
267 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
268 if (Log.on) Log.log(ByteStream.class, e);
269 throw new JavaScriptException("error while writing a ByteStream to a file");
274 private static final JSFunction utfEncode = new JSFunction() {
275 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
276 if (args == null || args.length != 1) return null;
277 return new ByteStream(args[0].toString().getBytes());
282 private static final JSFunction parseHTML = new JSFunction() {
283 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
284 if (args == null || args.length != 1) return null;
286 if (args[0] instanceof ByteStream) {
287 return HTML.parseReader(new InputStreamReader(((ByteStream)args[0]).getInputStream()));
289 return HTML.parseReader(new StringReader(args[0].toString()));
291 } catch (IOException e) {
292 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
293 if (Log.on) Log.log(HTML.class, e);
294 throw new JavaScriptException("error while parsing HTML");
299 private static void recurse(String indent, String name, Object o, Context cx) {
300 if (!name.equals("")) name += " : ";
303 Log.log(cx.interpreterSourceFile, indent + name + "<null>");
305 } else if (o instanceof NativeArray) {
306 Log.log(cx.interpreterSourceFile, indent + name + "<array>");
307 NativeArray na = (NativeArray)o;
308 for(int i=0; i<na.jsGet_length(); i++)
309 recurse(indent + " ", i + "", na.get(i, null), cx);
311 } else if (!(o instanceof NativeDate) && (o instanceof JSObject || o instanceof ScriptableObject)) {
312 Log.log(cx.interpreterSourceFile, indent + name + "<object>");
313 Scriptable s = (Scriptable)o;
314 Object[] keys = s.getIds();
315 for(int i=0; i<keys.length; i++)
316 recurse(indent + " ", keys[i].toString(),
317 keys[i] instanceof Integer ? s.get(((Integer)keys[i]).intValue(), null) : s.get(keys[i].toString(), null), cx);
320 Log.log(cx.interpreterSourceFile, indent + name + o);
325 private static final JSFunction recursivePrintObject = new JSFunction() {
326 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
327 if (args == null || args.length != 1) return null;
328 recurse("", "", args[0], cx);