2003/12/16 22:15:41
authordavid <david@xwt.org>
Fri, 30 Jan 2004 07:42:56 +0000 (07:42 +0000)
committerdavid <david@xwt.org>
Fri, 30 Jan 2004 07:42:56 +0000 (07:42 +0000)
darcs-hash:20040130074256-0c9ea-8886c881285c53119607215cd8b61f31e81cee95.gz

src/org/xwt/Box.java
src/org/xwt/Res.java
src/org/xwt/XWT.java

index 5187dc0..a98e052 100644 (file)
@@ -61,7 +61,14 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     static Hash boxToCursor = new Hash(500, 3);
     public static final int MAX_LENGTH = Integer.MAX_VALUE;
-    static final Font DEFAULT_FONT = Font.getFont((Res)Main.builtin.get("fonts/vera/Vera.ttf"), 10);
+    static final Font DEFAULT_FONT;
+   
+    static {
+        Font f = null;
+        try { f = Font.getFont((Res)Main.builtin.get("fonts/vera/Vera.ttf"), 10); }
+        catch(JSExn e) { Log.log(Box.class, "should never happen: "+e); }
+        DEFAULT_FONT = f;
+    }
 
     // box properties can not be trapped
     static final String[] props = new String[] {
index c55f1ac..5639080 100644 (file)
@@ -13,6 +13,15 @@ import org.bouncycastle.util.encoders.Base64;
 /** Base class for XWT resources */
 public abstract class Res extends JS {
 
+    /** return a resource for a given url */
+    public static final Res fromURL(String url) throws JSExn {
+        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 JSExn("invalid resource specifier " + url);
+    }
+
     // Base Class //////////////////////////////////////////////////////////////////////
 
     public String typeName() { return "resource"; }
@@ -27,7 +36,7 @@ public abstract class Res extends JS {
 
     public Res addExtension(String extension) { return new Ref(this, extension); }
 
-    public Object get(Object key) {
+    public Object get(Object key) throws JSExn {
         if ("".equals(key)) {
             try {
                 Template t = Template.getTemplate(addExtension(".xwt"));
@@ -177,6 +186,29 @@ public abstract class Res extends JS {
         public InputStream getInputStream(String path) throws IOException { return parent.getInputStream("/" + key + path); }
     }
 
+    /** provides redirection of a specified key */
+    public static class Graft extends Res {
+        Res graftee;
+        Object replaced_key, replaced_val;
+        Graft(Res graftee, Object key, Object val) { this.graftee = graftee; this.replaced_key = key; this.replaced_val = val; }
+        public boolean equals(Object o) { return this == o || graftee.equals(o); }
+        public int hashCode() { return graftee.hashCode(); }
+        public InputStream getInputStream(String s) throws IOException { return graftee.getInputStream(s); }
+        public Object get(Object key) throws JSExn { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
+        public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+            if (replaced_key.equals(name)) {
+                if (replaced_val instanceof JS) return ((JS)replaced_val).call(a, b, c, rest, nargs);
+                else throw new JSExn("attempted to call non-function (class="+replaced_val.getClass()+")");
+            } else {
+                return graftee.callMethod(name, a, b, c, rest, nargs);
+            }
+        }
+        public Number coerceToNumber() { return graftee.coerceToNumber(); }
+        public String coerceToString() { return graftee.coerceToString(); }
+        public boolean coerceToBoolean() { return graftee.coerceToBoolean(); }
+        public String typeName() { return graftee.typeName(); }
+    }
+
     /** shadow resource which replaces the graft */
     public static class ProgressWatcher extends Res {
         final Res watchee;
index 270a3fe..3f4cc54 100644 (file)
@@ -28,7 +28,7 @@ public final class XWT extends JS {
         String key;
         Sub(String key) { this.key = key; }
         public String toString() { return "XWTSUB " + key; }
-        public void put(Object key, Object val) { XWT.this.put(this.key + "." + key, val); }
+        public void put(Object key, Object val) throws JSExn { XWT.this.put(this.key + "." + key, val); }
         public Object get(Object key) throws JSExn { return XWT.this.get(this.key + "." + key); }
         public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
@@ -87,6 +87,7 @@ public final class XWT extends JS {
         case "graft": return METHOD;
         case "ui.browser": return METHOD;
         case "clone": return METHOD;
+        case "log.print": return METHOD;
         case "log.println": return METHOD;
         case "log.dump": return METHOD;
         case "regexp": return METHOD;
@@ -103,59 +104,84 @@ public final class XWT extends JS {
         return super.get(name);
     }
 
-    public void put(Object name, final Object value) {
+    public void put(Object name, final Object value) throws JSExn {
         //#switch(name)
-        case "thread": Scheduler.add((JSFunction)value);
-        case "ui.clipboard": Platform.setClipBoard((String)value);
-        case "ui.frame": Platform.createSurface((Box)value, true, true);
-        case "ui.window": Platform.createSurface((Box)value, false, true);
+        case "thread": Scheduler.add((JSFunction)value); return;
+        case "ui.clipboard": Platform.setClipBoard((String)value); return;
+        case "ui.frame": Platform.createSurface((Box)value, true, true); return;
+        case "ui.window": Platform.createSurface((Box)value, false, true); return;
         case "undocumented.internal.proxyAuthorization":
             HTTP.Proxy.Authorization.authorization = value.toString();
-            HTTP.Proxy.Authorization.waitingForUser.release();
+            HTTP.Proxy.Authorization.waitingForUser.release(); return;
         //#end
+
+        throw new JSExn("attempted to put unknown property: xwt."+name);
     }
 
     public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
-        if (name.equals("date")) return new JSDate(a, b, c, rest, nargs);
-        else if (nargs == 0 && name.equals("thread.yield")) { sleep(0); return null; }
-        else if (nargs == 2) {
-            //#switch(name)
-            case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
-            case "soap": /* return new SOAP((String)a, "", (String)b, null); */
-            case "apply": Template.getTemplate((Res)b).apply((Box)a, XWT.this); return a;
-            //#end
-
-            /*
-        } else if (nargs == 3 && name.equals("soap")) {
-            if (name.equals("soap"))
-                return new SOAP((String)a, "", (String)b, (String)c);
-            */
-
-        } else if (nargs == 1) {
+        try {
             //#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 JSExn("this resource cannot be cached"); }
-            case "res.url": return url2res((String)a);
-            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;
+            case "date": return new JSDate(a, b, c, rest, nargs);
+            case "rpc.soap": return null;//new SOAP((String)a, "", (String)b, (String)c);
+            case "graft":
+                if (a instanceof Box) throw new JSExn("can't graft onto Boxes");
+                if (a instanceof String) throw new JSExn("can't graft onto Strings");
+                if (a instanceof Number) throw new JSExn("can't graft onto Numbers");
+                if (a instanceof Res) return new Res.Graft((Res)a, b, c);
+                // FEATURE: grafting onto JS
+                throw new JSExn("cannot graft onto "+a.getClass());
             //#end
+            switch (nargs) {
+                case 0:
+                    //#switch(name)
+                    case "thread.yield": sleep(0); return null;
+                    //#end
+                    break;
+                case 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 JSExn("this resource cannot be cached"); }
+                    case "res.url": return url2res((String)a);
+                    case "thread.sleep": sleep(JS.toInt(a)); return null;
+                    case "log.print":   Log.logJS(this, a== null ? "**null**" : a.toString()); 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 "crypto.rsa": /* FEATURE */ return null;
+                    case "crypto.md5": /* FEATURE */ return null;
+                    case "crypto.sha1": /* FEATURE */ return null;
+                    case "crypto.rc4": /* FEATURE */ return null;
+                    case "stream.parse.html": /* FEATURE */ return null;
+                    case "stream.parse.xml": /* FEATURE */ return null;
+                    case "stream.parse.utf8": /* FEATURE */ return null;
+                    //#end
+                    break;
+                case 2:
+                    //#switch(name)
+                    case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
+                    case "apply":
+                        if (b instanceof Res) Template.getTemplate((Res)b).apply((Box)a, XWT.this);
+                        else {
+                            JS to = (JS)a, from = (JS)b; Object k;
+                            for (Enumeration e = from.keys(); e.hasMoreElements();) {
+                                k = e.nextElement(); to.put(k, from.get(k));
+                            }
+                        }
+                        return a;
+                    //#end
+                    break;
+            }
+        } catch (RuntimeException e) {
+            throw new JSExn("invalid argument for xwt object method "+name+"()");
         }
-        return null;
+
+        throw new JSExn("invalid number of arguments for xwt object method "+name+"()");
     }
 
     public Res url2res(String url) throws JSExn {