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("button")) {
57 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
58 else 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 return new Integer(0);
62 else if (name.equals("println")) return println;
63 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
64 else return super.get(name, start);
67 public void put(String name, Scriptable start, Object value) {
68 if (name == null) return;
69 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
70 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
71 else super.put(name, start, value);
75 // JSFunction Instances ///////////////////////////////////////////////////////////////////
77 /** Helper class for defining functions. */
78 private static abstract class JSFunction extends JSObject implements Function {
79 JSFunction() { setSeal(true); }
80 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
83 private static final JSFunction newBrowserWindow = new JSFunction() {
84 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
85 if (args.length != 1 || args[0] == null) return null;
86 Platform.newBrowserWindow(args[0].toString());
91 private static final JSFunction yield = new JSFunction() {
92 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
93 sleep.call(cx, null, null, null);
98 private static final JSFunction println = new JSFunction() {
99 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
100 if (args.length == 1)
102 Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
107 private static final JSFunction date = new JSFunction() {
108 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
109 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
110 } catch (Exception e) {
111 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
112 if (Log.on) Log.log(this, e);
118 private static final JSFunction regexp = new JSFunction() {
119 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
120 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
121 } catch (Exception e) {
122 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
123 if (Log.on) Log.log(this, e);
129 private static final JSFunction listfonts = new JSFunction() {
130 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
131 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
135 private static final JSFunction theme = new JSFunction() {
136 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
137 if (args.length != 2) return null;
138 if (args[0] == null || args[1] == null) return null;
139 Template.retheme(args[0].toString(), args[1].toString());
144 private static final JSFunction xmlrpc = new JSFunction() {
145 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
146 if (args.length != 1 || args[0] == null) return null;
147 return new XMLRPC(args[0].toString(), "");
151 private static final JSFunction soap = new JSFunction() {
152 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
153 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
154 else if (args.length == 2 && args[0] != null && args[1] != null)
155 return new SOAP(args[0].toString(), "", args[1].toString(), null);
156 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
157 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
162 private static final JSFunction textwidth = new JSFunction() {
163 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
164 if (args.length < 1 || args.length > 2) return null;
165 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
166 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
167 String text = args.length == 1 ? args[0].toString() : args[1].toString();
168 XWF xwf = XWF.getXWF(font);
169 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
170 else return new Integer(xwf.stringWidth(text));
175 private static final JSFunction textheight = new JSFunction() {
176 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
177 if (args.length > 1) return null;
178 if (args.length == 1 && args[0] == null) return null;
179 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
180 XWF xwf = XWF.getXWF(font);
181 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
182 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
186 private static final JSFunction newBox = new JSFunction() {
187 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
188 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
189 for(int i=1; i<args.length; i++)
190 if (args[i] instanceof Box)
191 ret.put(ret.numChildren(), null, (Box)args[i]);
192 for(int i=1; i<args.length; i++)
193 if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
194 Scriptable s = (Scriptable)args[i];
195 Object[] keys = s.getIds();
197 // FIXME: need to ensure that this is putGlobally(), but still run traps...
198 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
204 private static final JSFunction sleep = new JSFunction() {
205 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
206 if (args != null && (args.length != 1 || args[0] == null)) return null;
207 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
209 Thread thread = Thread.currentThread();
210 if (!(thread instanceof ThreadMessage)) {
211 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
214 ThreadMessage mythread = (ThreadMessage)thread;
215 mythread.done.release();
217 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
219 MessageQueue.add(mythread);
225 private static final JSFunction openFile = new JSFunction() {
226 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
227 if (args.length != 1) return null;
228 String file = Platform.fileDialog(args[0].toString(), false);
229 return file == null ? null : new ByteStream(file);
233 private static final JSFunction saveFile = new JSFunction() {
234 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
235 if (args.length != 2) return null;
236 if (!(args[1] instanceof ByteStream)) return null;
237 String file = args[0].toString();
238 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
239 file = Platform.fileDialog(file, true);
240 if (file == null) return null;
241 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
244 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
246 } catch (IOException e) {
247 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
248 if (Log.on) Log.log(ByteStream.class, e);
249 throw new JavaScriptException("error while writing a ByteStream to a file");
254 private static final JSFunction saveFileAs = new JSFunction() {
255 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
256 if (args.length != 2) return null;
257 if (!(args[1] instanceof ByteStream)) return null;
258 String file = args[0].toString();
259 file = Platform.fileDialog(file, true);
260 if (file == null) return null;
261 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
263 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
265 } catch (IOException e) {
266 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
267 if (Log.on) Log.log(ByteStream.class, e);
268 throw new JavaScriptException("error while writing a ByteStream to a file");
273 private static final JSFunction utfEncode = new JSFunction() {
274 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
275 if (args == null || args.length != 1) return null;
276 return new ByteStream(args[0].toString().getBytes());
281 private static void recurse(String indent, String name, Object o, Context cx) {
282 if (!name.equals("")) name += " : ";
285 Log.log(cx.interpreterSourceFile, indent + name + "<null>");
287 } else if (o instanceof NativeArray) {
288 Log.log(cx.interpreterSourceFile, indent + name + "<array>");
289 NativeArray na = (NativeArray)o;
290 for(int i=0; i<na.jsGet_length(); i++)
291 recurse(indent + " ", i + "", na.get(i, null), cx);
293 } else if (o instanceof JSObject || o instanceof ScriptableObject) {
294 Log.log(cx.interpreterSourceFile, indent + name + "<object>");
295 Scriptable s = (Scriptable)o;
296 Object[] keys = s.getIds();
297 for(int i=0; i<keys.length; i++)
298 recurse(indent + " ", keys[i].toString(),
299 keys[i] instanceof Integer ? s.get(((Integer)keys[i]).intValue(), null) : s.get(keys[i].toString(), null), cx);
302 Log.log(cx.interpreterSourceFile, indent + name + o);
307 private static final JSFunction recursivePrintObject = new JSFunction() {
308 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
309 if (args == null || args.length != 1) return null;
310 recurse("", "", args[0], cx);