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("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("clipboard")) return Platform.getClipBoard();
31 else if (name.equals("origin")) return Main.origin;
32 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
33 else if (name.equals("altKeyName")) return Platform.altKeyName();
34 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
35 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
36 else if (name.equals("fileSeparator")) return File.separator;
37 else if (name.equals("homeDir")) return System.getProperty("user.home");
38 else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
39 else if (name.equals("math")) return xwtMath;
40 else if (name.equals("string")) return xwtString;
41 else if (name.equals("parseInt")) return xwtString.get("parseInt");
42 else if (name.equals("parseFloat")) return xwtString.get("parseFloat");
43 else if (name.equals("button")) {
44 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
45 else 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 return new Integer(0);
49 else return super.get(name);
52 public void put(Object name, Object value) {
53 if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
54 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
55 else if (name.equals("proxyAuthorization")) {
56 HTTP.Proxy.Authorization.authorization = value.toString();
57 HTTP.Proxy.Authorization.waitingForUser.release();
58 } else super.put(name, value);
61 public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
63 if (method.equals("newBrowserWindow")) {
64 if (checkOnly) return Boolean.TRUE;
65 if (args.length() != 1 || args.elementAt(0) == null) return null;
66 Platform.newBrowserWindow(args.elementAt(0).toString());
69 } else if (method.equals("clone")) {
70 return new XWT((Res)args.elementAt(0));
72 } else if (method.equals("yield")) {
73 if (checkOnly) return Boolean.TRUE;
77 } else if (method.equals("load")) {
78 if (checkOnly) return Boolean.TRUE;
79 return Res.stringToRes(args.elementAt(0).toString());
81 } else if (method.equals("println")) {
82 if (checkOnly) return Boolean.TRUE;
83 if (args.length() != 1) return null;
84 if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
87 } else if (method.equals("date")) {
88 if (checkOnly) return Boolean.TRUE;
89 Log.log(XWT.class, "date not implemented");
92 } else if (method.equals("regexp")) {
93 if (checkOnly) return Boolean.TRUE;
94 return new Regexp(args);
96 } else if (method.equals("xmlrpc")) {
97 if (checkOnly) return Boolean.TRUE;
98 if (args.length() != 1 || args.elementAt(0) == null) return null;
99 return new XMLRPC(args.elementAt(0).toString(), "");
101 } else if (method.equals("soap")) {
102 if (checkOnly) return Boolean.TRUE;
103 if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
104 else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
105 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
106 else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
107 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
110 } else if (method.equals("newBox")) {
111 if (checkOnly) return Boolean.TRUE;
114 } else if (method.equals("sleep")) {
115 if (checkOnly) return Boolean.TRUE;
116 if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
117 int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
121 } else if (method.equals("openFile")) {
122 if (checkOnly) return Boolean.TRUE;
123 if (args.length() != 1) return null;
124 String file = Platform.fileDialog(args.elementAt(0).toString(), false);
125 return file == null ? null : new Res.stringToResource("file:" + file);
127 } else if (method.equals("saveFile")) {
128 if (checkOnly) return Boolean.TRUE;
131 if (args.length() != 2) return null;
132 if (!(args.elementAt(1) instanceof ByteStream)) return null;
133 String file = args.elementAt(0).toString();
134 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
135 file = Platform.fileDialog(file, true);
136 if (file == null) return null;
137 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
140 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
142 } catch (IOException e) {
143 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
144 if (Log.on) Log.log(ByteStream.class, e);
145 throw new JS.Exn("error while writing a ByteStream to a file");
149 } else if (method.equals("saveFileAs")) {
152 if (checkOnly) return Boolean.TRUE;
153 if (args.length() != 2) return null;
154 if (!(args.elementAt(1) instanceof ByteStream)) return null;
155 String file = args.elementAt(0).toString();
156 file = Platform.fileDialog(file, true);
157 if (file == null) return null;
158 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
160 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
162 } catch (IOException e) {
163 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
164 if (Log.on) Log.log(ByteStream.class, e);
165 throw new JS.Exn("error while writing a ByteStream to a file");
169 } else if (method.equals("parseHTML")) {
170 if (checkOnly) return Boolean.TRUE;
171 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
173 if (args.elementAt(0) instanceof Res) {
174 return HTML.parseReader(new InputStreamReader(((Res)args.elementAt(0)).getInputStream()));
176 return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
178 } catch (IOException e) {
179 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
180 if (Log.on) Log.log(HTML.class, e);
181 throw new JS.Exn("error while parsing HTML");
184 } else if (method.equals("recursivePrintObject")) {
185 if (checkOnly) return Boolean.TRUE;
186 if (args.length() != 1) return null;
187 recurse("", "", args.elementAt(0));
191 if (checkOnly) return Boolean.FALSE;
195 private static void recurse(String indent, String name, Object o) {
196 if (!name.equals("")) name += " : ";
199 Log.logJS(indent + name + "<null>");
201 } else if (o instanceof JS.Array) {
202 Log.logJS(indent + name + "<array>");
203 JS.Array na = (JS.Array)o;
204 for(int i=0; i<na.length(); i++)
205 recurse(indent + " ", i + "", na.elementAt(i));
207 } else if (o instanceof JS) {
208 Log.logJS(indent + name + "<object>");
210 Object[] keys = s.keys();
211 for(int i=0; i<keys.length; i++)
213 recurse(indent + " ", keys[i].toString(),
214 (keys[i] instanceof Integer) ?
215 s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
218 Log.logJS(indent + name + o);
223 public static void sleep(int i) {
224 java.lang.Thread thread = java.lang.Thread.currentThread();
225 if (!(thread instanceof ThreadMessage)) {
226 if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
228 ThreadMessage mythread = (ThreadMessage)thread;
229 mythread.done.release();
230 if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
231 Message.Q.add(mythread);
236 private static class XWTMath extends JS.Obj {
238 JS gs = new JS.GlobalScope();
239 put("isNaN",gs.get("isNaN"));
240 put("isFinite",gs.get("isFinite"));
241 put("NaN",gs.get("NaN"));
242 put("Infinity",gs.get("Infinity"));
245 public Object get(Object key) {
246 Object ret = super.get(key);
247 if(ret == null) ret = JS.Math.get(key);
251 private static class XWTString extends JS.Obj {
253 JS gs = new JS.GlobalScope();
254 put("parseInt",gs.get("parseInt"));
255 put("parseFloat",gs.get("parseFloat"));
256 put("decodeURI",gs.get("decodeURI"));
257 put("decodeURIComponent",gs.get("decodeURIComponent"));
258 put("encodeURI",gs.get("encodeURI"));
259 put("encodeURIComponent",gs.get("encodeURIComponent"));
260 put("escape",gs.get("escape"));
261 put("unescape",gs.get("unescape"));
262 put("fromCharCode",gs.get("stringFromCharCode"));