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 if (checkOnly) return Boolean.TRUE;
72 return new XWT((Res)args.elementAt(0));
74 } else if (method.equals("watchProgress")) {
75 if (checkOnly) return Boolean.TRUE;
76 return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.Callable)args.elementAt(1));
78 } else if (method.equals("yield")) {
79 if (checkOnly) return Boolean.TRUE;
83 } else if (method.equals("load")) {
84 if (checkOnly) return Boolean.TRUE;
85 return Res.stringToRes(args.elementAt(0).toString());
87 } else if (method.equals("println")) {
88 if (checkOnly) return Boolean.TRUE;
89 if (args.length() != 1) return null;
90 if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
93 } else if (method.equals("date")) {
94 if (checkOnly) return Boolean.TRUE;
95 Log.log(XWT.class, "date not implemented");
98 } else if (method.equals("regexp")) {
99 if (checkOnly) return Boolean.TRUE;
100 return new Regexp(args);
102 } else if (method.equals("xmlrpc")) {
103 if (checkOnly) return Boolean.TRUE;
104 if (args.length() != 1 || args.elementAt(0) == null) return null;
105 return new XMLRPC(args.elementAt(0).toString(), "");
107 } else if (method.equals("soap")) {
108 if (checkOnly) return Boolean.TRUE;
109 if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
110 else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
111 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
112 else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
113 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
116 } else if (method.equals("sleep")) {
117 if (checkOnly) return Boolean.TRUE;
118 if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
119 int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
123 } else if (method.equals("openFile")) {
124 if (checkOnly) return Boolean.TRUE;
125 if (args.length() != 1) return null;
126 String file = Platform.fileDialog(args.elementAt(0).toString(), false);
127 return file == null ? null : Res.stringToRes("file:" + file);
129 } else if (method.equals("saveFile")) {
130 if (checkOnly) return Boolean.TRUE;
133 if (args.length() != 2) return null;
134 if (!(args.elementAt(1) instanceof ByteStream)) return null;
135 String file = args.elementAt(0).toString();
136 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
137 file = Platform.fileDialog(file, true);
138 if (file == null) return null;
139 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
142 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
144 } catch (IOException e) {
145 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
146 if (Log.on) Log.log(ByteStream.class, e);
147 throw new JS.Exn("error while writing a ByteStream to a file");
151 } else if (method.equals("saveFileAs")) {
154 if (checkOnly) return Boolean.TRUE;
155 if (args.length() != 2) return null;
156 if (!(args.elementAt(1) instanceof ByteStream)) return null;
157 String file = args.elementAt(0).toString();
158 file = Platform.fileDialog(file, true);
159 if (file == null) return null;
160 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
162 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
164 } catch (IOException e) {
165 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
166 if (Log.on) Log.log(ByteStream.class, e);
167 throw new JS.Exn("error while writing a ByteStream to a file");
171 } else if (method.equals("parseHTML")) {
172 if (checkOnly) return Boolean.TRUE;
173 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
175 if (args.elementAt(0) instanceof Res) {
176 return HTML.parseReader(new InputStreamReader(((Res)args.elementAt(0)).getInputStream()));
178 return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
180 } catch (IOException e) {
181 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
182 if (Log.on) Log.log(HTML.class, e);
183 throw new JS.Exn("error while parsing HTML");
186 } else if (method.equals("recursivePrintObject")) {
187 if (checkOnly) return Boolean.TRUE;
188 if (args.length() != 1) return null;
189 recurse("", "", args.elementAt(0));
193 if (checkOnly) return Boolean.FALSE;
197 private static void recurse(String indent, String name, Object o) {
198 if (!name.equals("")) name += " : ";
201 Log.logJS(indent + name + "<null>");
203 } else if (o instanceof JS.Array) {
204 Log.logJS(indent + name + "<array>");
205 JS.Array na = (JS.Array)o;
206 for(int i=0; i<na.length(); i++)
207 recurse(indent + " ", i + "", na.elementAt(i));
209 } else if (o instanceof JS) {
210 Log.logJS(indent + name + "<object>");
212 Object[] keys = s.keys();
213 for(int i=0; i<keys.length; i++)
215 recurse(indent + " ", keys[i].toString(),
216 (keys[i] instanceof Integer) ?
217 s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
220 Log.logJS(indent + name + o);
225 public static void sleep(int i) {
226 java.lang.Thread thread = java.lang.Thread.currentThread();
227 if (!(thread instanceof ThreadMessage)) {
228 if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
230 ThreadMessage mythread = (ThreadMessage)thread;
231 mythread.done.release();
232 if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
233 Message.Q.add(mythread);
238 private static class XWTMath extends JS.Obj {
240 JS gs = new JS.GlobalScope();
241 put("isNaN",gs.get("isNaN"));
242 put("isFinite",gs.get("isFinite"));
243 put("NaN",gs.get("NaN"));
244 put("Infinity",gs.get("Infinity"));
247 public Object get(Object key) {
248 Object ret = super.get(key);
249 if(ret == null) ret = JS.Math.get(key);
253 private static class XWTString extends JS.Obj {
255 JS gs = new JS.GlobalScope();
256 put("parseInt",gs.get("parseInt"));
257 put("parseFloat",gs.get("parseFloat"));
258 put("decodeURI",gs.get("decodeURI"));
259 put("decodeURIComponent",gs.get("decodeURIComponent"));
260 put("encodeURI",gs.get("encodeURI"));
261 put("encodeURIComponent",gs.get("encodeURIComponent"));
262 put("escape",gs.get("escape"));
263 put("unescape",gs.get("unescape"));
264 put("fromCharCode",gs.get("stringFromCharCode"));