1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
10 import org.xwt.translators.*;
11 import org.bouncycastle.util.encoders.Base64;
13 /** Singleton class that provides all functionality in the xwt.* namespace */
14 public final class XWT extends JS.Obj {
17 public XWT(Res rr) { this.rr = rr; }
19 private final JS xwtMath = new XWTMath();
20 private final JS xwtString = new XWTString();
22 /** each key is a string representing a filename which the user has already given XWT permission to write to */
23 private static Hashtable safeFiles = new Hashtable();
25 public Object get(Object name) {
26 if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
27 else if (name.equals("rr")) return rr;
28 else if (name.equals("box")) return new Box();
29 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
30 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
31 else if (name.equals("clipboard")) return Platform.getClipBoard();
32 else if (name.equals("origin")) return Main.origin;
33 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
34 else if (name.equals("altKeyName")) return Platform.altKeyName();
35 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
36 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
37 else if (name.equals("fileSeparator")) return File.separator;
38 else if (name.equals("homeDir")) return System.getProperty("user.home");
39 else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
40 else if (name.equals("math")) return xwtMath;
41 else if (name.equals("string")) return xwtString;
42 else if (name.equals("parseInt")) return xwtString.get("parseInt");
43 else if (name.equals("parseFloat")) return xwtString.get("parseFloat");
44 else if (name.equals("button")) {
45 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
46 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
47 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
48 else return new Integer(0);
50 else return super.get(name);
53 public void put(Object name, Object value) {
54 if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
55 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
56 else if (name.equals("proxyAuthorization")) {
57 HTTP.Proxy.Authorization.authorization = value.toString();
58 HTTP.Proxy.Authorization.waitingForUser.release();
59 } else super.put(name, value);
62 public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
64 if (method.equals("newBrowserWindow")) {
65 if (checkOnly) return Boolean.TRUE;
66 if (args.length() != 1 || args.elementAt(0) == null) return null;
67 Platform.newBrowserWindow(args.elementAt(0).toString());
70 } else if (method.equals("clone")) {
71 return new XWT((Res)args.elementAt(0));
73 } else if (method.equals("yield")) {
74 if (checkOnly) return Boolean.TRUE;
78 } else if (method.equals("load")) {
79 if (checkOnly) return Boolean.TRUE;
80 return Res.stringToRes(args.elementAt(0).toString());
82 } else if (method.equals("println")) {
83 if (checkOnly) return Boolean.TRUE;
84 if (args.length() != 1) return null;
85 if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
88 } else if (method.equals("date")) {
89 if (checkOnly) return Boolean.TRUE;
90 Log.log(XWT.class, "date not implemented");
93 } else if (method.equals("regexp")) {
94 if (checkOnly) return Boolean.TRUE;
95 return new Regexp(args);
97 } else if (method.equals("xmlrpc")) {
98 if (checkOnly) return Boolean.TRUE;
99 if (args.length() != 1 || args.elementAt(0) == null) return null;
100 return new XMLRPC(args.elementAt(0).toString(), "");
102 } else if (method.equals("soap")) {
103 if (checkOnly) return Boolean.TRUE;
104 if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
105 else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
106 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
107 else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
108 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
111 } else if (method.equals("sleep")) {
112 if (checkOnly) return Boolean.TRUE;
113 if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
114 int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
118 } else if (method.equals("openFile")) {
119 if (checkOnly) return Boolean.TRUE;
120 if (args.length() != 1) return null;
121 String file = Platform.fileDialog(args.elementAt(0).toString(), false);
122 return file == null ? null : Res.stringToRes("file:" + file);
124 } else if (method.equals("saveFile")) {
125 if (checkOnly) return Boolean.TRUE;
128 if (args.length() != 2) return null;
129 if (!(args.elementAt(1) instanceof ByteStream)) return null;
130 String file = args.elementAt(0).toString();
131 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
132 file = Platform.fileDialog(file, true);
133 if (file == null) return null;
134 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
137 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
139 } catch (IOException e) {
140 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
141 if (Log.on) Log.log(ByteStream.class, e);
142 throw new JS.Exn("error while writing a ByteStream to a file");
146 } else if (method.equals("saveFileAs")) {
149 if (checkOnly) return Boolean.TRUE;
150 if (args.length() != 2) return null;
151 if (!(args.elementAt(1) instanceof ByteStream)) return null;
152 String file = args.elementAt(0).toString();
153 file = Platform.fileDialog(file, true);
154 if (file == null) return null;
155 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
157 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
159 } catch (IOException e) {
160 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
161 if (Log.on) Log.log(ByteStream.class, e);
162 throw new JS.Exn("error while writing a ByteStream to a file");
166 } else if (method.equals("parseHTML")) {
167 if (checkOnly) return Boolean.TRUE;
168 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
170 if (args.elementAt(0) instanceof Res) {
171 return HTML.parseReader(new InputStreamReader(((Res)args.elementAt(0)).getInputStream()));
173 return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
175 } catch (IOException e) {
176 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
177 if (Log.on) Log.log(HTML.class, e);
178 throw new JS.Exn("error while parsing HTML");
181 } else if (method.equals("recursivePrintObject")) {
182 if (checkOnly) return Boolean.TRUE;
183 if (args.length() != 1) return null;
184 recurse("", "", args.elementAt(0));
188 if (checkOnly) return Boolean.FALSE;
192 private static void recurse(String indent, String name, Object o) {
193 if (!name.equals("")) name += " : ";
196 Log.logJS(indent + name + "<null>");
198 } else if (o instanceof JS.Array) {
199 Log.logJS(indent + name + "<array>");
200 JS.Array na = (JS.Array)o;
201 for(int i=0; i<na.length(); i++)
202 recurse(indent + " ", i + "", na.elementAt(i));
204 } else if (o instanceof JS) {
205 Log.logJS(indent + name + "<object>");
207 Object[] keys = s.keys();
208 for(int i=0; i<keys.length; i++)
210 recurse(indent + " ", keys[i].toString(),
211 (keys[i] instanceof Integer) ?
212 s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
215 Log.logJS(indent + name + o);
220 public static void sleep(int i) {
221 java.lang.Thread thread = java.lang.Thread.currentThread();
222 if (!(thread instanceof ThreadMessage)) {
223 if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
225 ThreadMessage mythread = (ThreadMessage)thread;
226 mythread.done.release();
227 if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
228 Message.Q.add(mythread);
233 private static class XWTMath extends JS.Obj {
235 JS gs = new JS.GlobalScope();
236 put("isNaN",gs.get("isNaN"));
237 put("isFinite",gs.get("isFinite"));
238 put("NaN",gs.get("NaN"));
239 put("Infinity",gs.get("Infinity"));
242 public Object get(Object key) {
243 Object ret = super.get(key);
244 if(ret == null) ret = JS.Math.get(key);
248 private static class XWTString extends JS.Obj {
250 JS gs = new JS.GlobalScope();
251 put("parseInt",gs.get("parseInt"));
252 put("parseFloat",gs.get("parseFloat"));
253 put("decodeURI",gs.get("decodeURI"));
254 put("decodeURIComponent",gs.get("decodeURIComponent"));
255 put("encodeURI",gs.get("encodeURI"));
256 put("encodeURIComponent",gs.get("encodeURIComponent"));
257 put("escape",gs.get("escape"));
258 put("unescape",gs.get("unescape"));
259 put("fromCharCode",gs.get("stringFromCharCode"));