- // Prefs Object //////////////////////////////////////////////////////////////////////////
-
- static Scriptable prefsRPC = new XMLRPC("http://megacz:mypassword@localhost/RPC2", "prefs");
-
- private static final JSObject prefs = new JSObject(false, true) {
- public Object get(String name, Scriptable start) {
- if (name.equals("get")) return prefsGet;
- else if (name.equals("list")) return prefsList;
- else if (name.equals("put")) return prefsPut;
- else return null;
- }
- };
-
- private static final JSObject.JSFunction prefsGet = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1 || args[0] == null) return null;
- try {
- return ((Function)prefsRPC.get("get", null)).call(cx, null, null, new Object[] { args[0] });
- } catch (JavaScriptException jse) {
- Object val = jse.getValue();
- if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
- return null;
- throw jse;
- } catch (Exception e) {
- // FIXME
- throw new JavaScriptException(e.toString());
- }
- }
- };
-
- private static final JSObject.JSFunction prefsPut = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length < 2 || args[0] == null) return null;
- try {
- return ((Function)prefsRPC.get("put", null)).call(cx, null, null, new Object[] { args[0].toString(), args[1] });
- } catch (JavaScriptException jse) {
- Object val = jse.getValue();
- if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
- return null;
- throw jse;
- } catch (Exception e) {
- // FIXME
- throw new JavaScriptException(e.toString());
- }
- }
- };
-
- private static final JSObject.JSFunction prefsList = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length < 2 || args[0] == null) return null;
- try {
- return ((Function)prefsRPC.get("list", null)).call(cx, null, null, new Object[] { args[0].toString(), args[1] });
- } catch (JavaScriptException jse) {
- Object val = jse.getValue();
- if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
- return null;
- throw jse;
- } catch (Exception e) {
- // FIXME
- throw new JavaScriptException(e.toString());
- }
- }
- };
-
- private static final JSObject.JSFunction prefsInvoke = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length < 2 || args[0] == null) return null;
- try {
- return ((Function)prefs.get("invoke")).call(cx, null, null, new Object[] { args[0].toString(), args[1], "megacz", "mypassword" });
- } catch (JavaScriptException jse) {
- Object val = jse.getValue();
- if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
- return null;
- throw jse;
- } catch (Exception e) {
- // FIXME
- throw new JavaScriptException(e.toString());
- }
- }
- };
-
-
-
- // JSFunction Instances ///////////////////////////////////////////////////////////////////
-
- private static final JSObject.JSFunction newBrowserWindow = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1 || args[0] == null) return null;
- Platform.newBrowserWindow(args[0].toString());
- return null;
- }
- };
-
- public static final JSObject.JSFunction yield = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- sleep.call(cx, null, null, null);
- return null;
- }
- };
-
- private static final JSObject.JSFunction println = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length == 1)
- if (Log.on) {
- String source = cx.interpreterSourceFile;
- if (source.endsWith("._")) source = source.substring(0, source.length() - 2);
- Log.log(source, args[0] == null ? "null" : args[0].toString());
- }
- return null;
- }
- };
-
- private static final JSObject.JSFunction date = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
- } catch (Exception e) {
- if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
- if (Log.on) Log.log(this, e);
- return null;
- }
- }
- };
-
- private static final JSObject.JSFunction regexp = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
- } catch (Exception e) {
- if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
- if (Log.on) Log.log(this, e);
- return null;
- }
- }
- };
-
- private static final JSObject.JSFunction listfonts = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
- }
- };
-
- private static final JSObject.JSFunction theme = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 2) return null;
- if (args[0] == null || args[1] == null) return null;
- Template.retheme(args[0].toString(), args[1].toString());
- return null;
- }
- };
-
- private static final JSObject.JSFunction xmlrpc = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1 || args[0] == null) return null;
- return new XMLRPC(args[0].toString(), "");
- }
- };
-
- private static final JSObject.JSFunction soap = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
- else if (args.length == 2 && args[0] != null && args[1] != null)
- return new SOAP(args[0].toString(), "", args[1].toString(), null);
- else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
- return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
- else return null;
- }
- };
-
- private static final JSObject.JSFunction textwidth = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
- if (args.length < 1 || args.length > 2) return null;
- if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
- String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
- String text = args.length == 1 ? args[0].toString() : args[1].toString();
- XWF xwf = XWF.getXWF(font);
- if (xwf == null) return new Integer(Platform.stringWidth(font, text));
- else return new Integer(xwf.stringWidth(text));
- }
- };
-
-
- private static final JSObject.JSFunction textheight = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length > 1) return null;
- if (args.length == 1 && args[0] == null) return null;
- String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
- XWF xwf = XWF.getXWF(font);
- if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
- else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
- }
- };
-
- private static final JSObject.JSFunction newBox = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-
- if (args.length > 0)
- if (Log.on) Log.log(XWT.class, "DEPRECATED: xwt.newBox() with multiple arguments is deprecated; use xwt.newBox().apply() " +
- Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
-
- Function callback = null;
- for(int i=1; i<args.length; i++)
- if (args[i] instanceof Function && callback == null)
- callback = (Function)args[i];
-
- Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList, callback);
-
- for(int i=1; i<args.length; i++)
- if (args[i] instanceof Box)
- ret.put(ret.numChildren(), null, (Box)args[i]);
- for(int i=1; i<args.length; i++)
- if (args[i] instanceof Scriptable && !(args[i] instanceof Box) && !(args[i] instanceof Function)) {
- Scriptable s = (Scriptable)args[i];
- Object[] keys = s.getIds();
-
- // FIXME: need to ensure that this is putGlobally(), but still run traps...
- for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
- }
- return ret;
- }
- };
-
- private static final JSObject.JSFunction sleep = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args != null && (args.length != 1 || args[0] == null)) return null;
- int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
-
- Thread thread = Thread.currentThread();
- if (!(thread instanceof ThreadMessage)) {
- if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
- return null;
- }
- ThreadMessage mythread = (ThreadMessage)thread;
- mythread.done.release();
-
- if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
-
- MessageQueue.add(mythread);
- mythread.go.block();
- return null;
- }
- };
-
- private static final JSObject.JSFunction openFile = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1) return null;
- String file = Platform.fileDialog(args[0].toString(), false);
- return file == null ? null : new ByteStream(file);
- }
- };
-
- private static final JSObject.JSFunction saveFile = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 2) return null;
- if (!(args[1] instanceof ByteStream)) return null;
- String file = args[0].toString();
- if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
- file = Platform.fileDialog(file, true);
- if (file == null) return null;
- safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
- }
- try {
- ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
- return null;
- } catch (IOException e) {
- if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
- if (Log.on) Log.log(ByteStream.class, e);
- throw new JavaScriptException("error while writing a ByteStream to a file");
- }
- }
- };
-
- private static final JSObject.JSFunction saveFileAs = new JSObject.JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 2) return null;
- if (!(args[1] instanceof ByteStream)) return null;
- String file = args[0].toString();
- file = Platform.fileDialog(file, true);
- if (file == null) return null;
- safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
- try {
- ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
- return null;
- } catch (IOException e) {
- if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
- if (Log.on) Log.log(ByteStream.class, e);
- throw new JavaScriptException("error while writing a ByteStream to a file");
- }
- }
- };
+ public Object call(Object name, JSArray args) throws JS.Exn {
+ Object a = args.elementAt(0);
+ Object b = args.elementAt(1);
+ Object c = args.elementAt(2);
+ if (name.equals("date")) return new org.xwt.js.JSDate(args);
+ else if (args.length() == 0 && name.equals("thread.yield")) { sleep(0); return null; }
+ else if (args.length() == 2) {
+ //#switch(name)
+ case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
+ case "soap": return new SOAP((String)args.elementAt(0), "", (String)args.elementAt(1), null);
+ case "apply":
+ Template.getTemplate((Res)args.elementAt(1)).apply((Box)args.elementAt(0), XWT.this);
+ return args.elementAt(0);
+ //#end
+ } else if (args.length() == 3 && name.equals("soap")) {
+ if (name.equals("soap")) {
+ return new SOAP((String)args.elementAt(0), "", (String)args.elementAt(1), (String)args.elementAt(2));
+ } else if (name.equals("graft")) {
+ if (a instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
+ if (a instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
+ if (a instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
+ if (a instanceof Res) return new Res.Graft((Res)a, b, c);
+ return new JS.Graft((JS)a, b, c);
+ }
+ } else if (args.length() == 1) {
+ //#switch(name)
+ case "ui.browser": Platform.newBrowserWindow((String)a); return null;
+ case "clone": return new XWT((Res)a);
+ case "res.unzip": return new Res.Zip((Res)a);
+ case "res.uncab": return new Res.Cab((Res)a);
+ case "res.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
+ catch (Res.NotCacheableException e) { throw new JS.Exn("this resource cannot be cached"); }
+ case "res.url":
+ String url = (String)a;
+ if (url.startsWith("http://")) return new Res.HTTP(url);
+ else if (url.startsWith("https://")) return new Res.HTTP(url);
+ else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
+ else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
+ throw new JS.Exn("invalid resource specifier " + url);
+
+ case "thread.sleep": sleep(JS.toInt(a)); return null;
+ case "log.println": Log.logJS(this, a== null ? "**null**" : a.toString()); return null;
+ case "log.dump": Log.recursiveLog("","",a); return null;
+ case "regexp": return new JSRegexp(a, null);
+ case "rpc.xml": return new XMLRPC((String)a, "");
+ case "rpc.soap": return new SOAP((String)a, "", null, null);
+ case "crypto.rsa": /* FEATURE */ break;
+ case "crypto.md5": /* FEATURE */ break;
+ case "crypto.sha1": /* FEATURE */ break;
+ case "crypto.rc4": /* FEATURE */ break;
+ case "stream.parse.html": /* FEATURE */ break;
+ case "stream.parse.xml": /* FEATURE */ break;
+ case "stream.parse.utf8": /* FEATURE */ break;
+ //#end
+ }
+ return null;
+ }