- private static final JSFunction textheight = new 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 JSFunction newBox = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
- 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)) {
- 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;
+ public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
+
+ if (method.equals("newBrowserWindow")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() != 1 || args.elementAt(0) == null) return null;
+ Platform.newBrowserWindow(args.elementAt(0).toString());
+ return null;
+
+ } else if (method.equals("clone")) {
+ if (checkOnly) return Boolean.TRUE;
+ return new XWT((Res)args.elementAt(0));
+
+ } else if (method.equals("graft")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.elementAt(0) instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
+ if (args.elementAt(0) instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
+ if (args.elementAt(0) instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
+ if (args.elementAt(0) instanceof Res)
+ return new Res.Graft((Res)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+ return new JS.Graft((JS)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+
+ } else if (method.equals("unzip")) {
+ if (checkOnly) return Boolean.TRUE;
+ return new Res.Zip((Res)args.elementAt(0));
+
+ } else if (method.equals("watchProgress")) {
+ if (checkOnly) return Boolean.TRUE;
+ return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.CompiledFunction)args.elementAt(1));
+
+ } else if (method.equals("yield")) {
+ if (checkOnly) return Boolean.TRUE;
+ sleep(0);
+ return null;
+
+ } else if (method.equals("load")) {
+ if (checkOnly) return Boolean.TRUE;
+ return Res.stringToRes(args.elementAt(0).toString());
+
+ } else if (method.equals("println")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() != 1) return null;
+ if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
+ return null;
+
+ } else if (method.equals("date")) {
+ if (checkOnly) return Boolean.TRUE;
+ Log.log(XWT.class, "date not implemented");
+ return null;
+
+ } else if (method.equals("regexp")) {
+ if (checkOnly) return Boolean.TRUE;
+ return new Regexp(args);
+
+ } else if (method.equals("apply")) {
+ if (checkOnly) return Boolean.TRUE;
+ Box b = (Box)args.elementAt(0);
+ Template.getTemplate((Res)args.elementAt(1)).apply(b, null, this);
+ return b;
+
+ } else if (method.equals("xmlrpc")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() != 1 || args.elementAt(0) == null) return null;
+ return new XMLRPC(args.elementAt(0).toString(), "");
+
+ } else if (method.equals("soap")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
+ else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
+ return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
+ else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
+ return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
+ else return null;
+
+ } else if (method.equals("sleep")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
+ int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
+ sleep(i);
+ return null;
+
+ } else if (method.equals("openFile")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() != 1) return null;
+ String file = Platform.fileDialog(args.elementAt(0).toString(), false);
+ return file == null ? null : new Res.File(file);
+
+ } else if (method.equals("saveFile") || method.equals("saveFileAs")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.length() != 2) return null;
+ if (!(args.elementAt(1) instanceof Res)) return null;
+ String file = args.elementAt(0).toString();
+ if (method.equals("saveFileAs") ||
+ safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
+ file = Platform.fileDialog(file, true);
+ // FIXME: throw exception here
+ if (file == null) return null;
+ safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());