2004/01/13 10:27:46
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:44:20 +0000 (07:44 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:44:20 +0000 (07:44 +0000)
darcs-hash:20040130074420-2ba56-8fe661b8a8707a217b6768f3ecc48e156481c944.gz

src/org/xwt/HTTP.java
src/org/xwt/Main.java
src/org/xwt/Picture.java
src/org/xwt/Platform.java
src/org/xwt/Scheduler.java
src/org/xwt/Stream.java
src/org/xwt/Surface.java

index 9190f8f..d94edc6 100644 (file)
@@ -339,7 +339,7 @@ public class HTTP {
         OUTER: do {
             if (pi != null) {
                 for(int i=0; i<pi.excluded.length; i++) if (host.equals(pi.excluded[i])) break OUTER;
-                if (sock == null && pi.proxyAutoConfigJSFunction != null) sock = attemptPAC(pi.proxyAutoConfigJSFunction);
+                if (sock == null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
                 if (sock == null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost,pi.httpsProxyPort);
                 if (sock == null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
                 if (sock == null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
@@ -658,7 +658,7 @@ public class HTTP {
         public String socksProxyHost = null;                 ///< the SOCKS Proxy Host to use
         public int socksProxyPort = -1;                      ///< the SOCKS Proxy Port to use
         public String[] excluded = null;                     ///< hosts to be excluded from proxy use; wildcards permitted
-        public JSFunction proxyAutoConfigJSFunction = null;  ///< the PAC script
+        public JS proxyAutoConfigFunction = null;  ///< the PAC script
     
         public static Proxy detectProxyViaManual() {
             Proxy ret = new Proxy();
@@ -713,8 +713,8 @@ public class HTTP {
             return ret;
         }
     
-        public static JSScope proxyAutoConfigRootJSScope = new ProxyAutoConfigRootJSScope();
-        public static JSFunction getProxyAutoConfigJSFunction(String url) {
+        public static JSScope proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
+        public static JS getProxyAutoConfigFunction(String url) {
             try { 
                 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).GET()));
                 String s = null;
@@ -740,9 +740,9 @@ public class HTTP {
                     if (Log.on) Log.info(Proxy.class, script);
                 }
 
-                JSFunction scr = JSFunction.fromReader("PAC script at " + url, 0, new StringReader(script));
-                scr.cloneWithNewParentScope(proxyAutoConfigRootJSScope).call(null, null, null, null, 0);
-                return (JSFunction)proxyAutoConfigRootJSScope.get("FindProxyForURL");
+                JS scr = JS.fromReader("PAC script at " + url, 0, new StringReader(script));
+                JS.cloneWithNewParentScope(scr, proxyAutoConfigRootScope).call(null, null, null, null, 0);
+                return (JS)proxyAutoConfigRootScope.get("FindProxyForURL");
             } catch (Exception e) {
                 if (Log.on) {
                     Log.info(Platform.class, "WPAD detection failed due to:");
@@ -781,8 +781,8 @@ public class HTTP {
                 Scheduler.add(new Scheduler.Task() {
                         public void perform() throws Exception {
                             Box b = new Box();
-                            Template t = Template.getTemplate((Stream)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt"));
-                            t.apply(b, null);
+                            Template t = new Template(Stream.getInputStream((JS)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt")), new XWT(null));
+                            t.apply(b);
                             b.put("realm", realm);
                             b.put("proxyIP", proxyIP);
                         }
@@ -796,9 +796,9 @@ public class HTTP {
 
         // ProxyAutoConfigRootJSScope ////////////////////////////////////////////////////////////////////
 
-        public static class ProxyAutoConfigRootJSScope extends JSScope.Global {
+        public static class ProxyAutoConfigRootScope extends JSScope.Global {
 
-            public ProxyAutoConfigRootJSScope() { super(); }
+            public ProxyAutoConfigRootScope() { super(); }
         
             public Object get(Object name) throws JSExn {
                 //#switch(name)
index b9967e8..6bae13c 100644 (file)
@@ -57,6 +57,7 @@ public class Main {
 
         String initialTemplateName = args.length > startargs + 1 ? args[startargs + 1] : "main";
         initialTemplateName = initialTemplateName.replace('.', '/');
+        initialTemplateName += ".xwt";
         origin = args[startargs];
 
         Stream rr;
@@ -77,12 +78,12 @@ public class Main {
 
         if (Log.on) Log.info(Main.class, "loading xwar");
         final XWT xwt = new XWT(rr);
-        final Stream final_rr = rr;
+        final JS final_rr = (JS)xwt.get("");
 
         scarImage =
             Picture.load((Stream)Main.builtin.get("org/xwt/builtin/scar.png"),
-                         new Scheduler.Task() { public void perform() throws JSExn {
-                             Template.getTemplate(((Stream)final_rr.get(initialTemplate))).apply(new Box(), xwt);
+                         new Scheduler.Task() { public void perform() throws Exception {
+                             new Template(Stream.getInputStream(final_rr.get(initialTemplate)), xwt).apply(new Box());
                          } });
 
         Scheduler.init();
index 4cbd299..4a8289d 100644 (file)
@@ -17,40 +17,32 @@ import org.xwt.translators.*;
 public class Picture {
 
     public Picture() { this.res = null; }
-    public Picture(Stream r) { this.res = r; }
+    public Picture(JS r) { this.res = r; }
     private static Cache cache = new Cache(100);   ///< Picture, keyed by the Stream that loaded them
 
-    public Stream res = null;                         ///< the resource we were loaded from
+    public JS res = null;                          ///< the resource we were loaded from
     public int width = -1;                         ///< the width of the image
     public int height = -1;                        ///< the height of the image
     public int[] data = null;                      ///< argb samples
     public boolean isLoaded = false;               ///< true iff the image is fully loaded
     
     /** turns a resource into a Picture.Source and passes it to the callback */
-    public static Picture load(final Stream r, final Scheduler.Task callback) {
+    public static Picture load(JS rr, final Scheduler.Task callback) {
+        final JS r = rr;
         Picture ret = (Picture)cache.get(r);
         if (ret == null) cache.put(r, ret = Platform.createPicture(r));
         final Picture p = ret;
         if (!ret.isLoaded && callback != null)
             new java.lang.Thread() { public void run() {
                 // get the InputStream for the image
+                Log.info(Picture.class, "r is a " + r.getClass().getName());
                 InputStream in = null;
                 try {
-                    in = r.getInputStream();
+                    in = (r instanceof XWT.Blessing) ? ((XWT.Blessing)r).getImage() : Stream.getInputStream(r);
                 } catch (IOException e) {
-                    in = null;
-                    if (r instanceof Stream.Ref) {
-                        // add extensions to the resource, looking for the image
-                        Stream.Ref ref = (Stream.Ref)r;
-                        Stream newr;
-
-                        String[] exts = new String[] { ".png", ".jpeg", ".gif" };
-                        for (int i=0; i < exts.length && in == null; i++) {
-                            newr = ref.addExtension(exts[i]);
-                            try { in = newr.getInputStream(); }
-                            catch (IOException f) { in = null; }
-                        }
-                    }
+                    Log.error(Picture.class, r);
+                } catch (JSExn e) {
+                    Log.error(Picture.class, r);
                 }
         
                 // could not find image
index 2e086da..e364a50 100644 (file)
@@ -5,6 +5,7 @@ import java.lang.reflect.*;
 import java.net.*;
 import java.io.*;
 import java.util.*;
+import org.xwt.js.*;
 import org.xwt.util.*;
 
 /** 
@@ -104,17 +105,16 @@ public abstract class Platform {
     protected void postInit() { }
 
     protected Surface _createSurface(Box b, boolean framed) { return null; }
-    protected Picture _createPicture(Stream r) { return null; }
+    protected Picture _createPicture(JS r) { return null; }
     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return null; }
     protected Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new DefaultGlyph(f, c); }
 
     public static PixelBuffer createPixelBuffer(int w, int h, Surface s) { return platform._createPixelBuffer(w, h, s); }
-    public static Picture createPicture(Stream r) { return platform._createPicture(r); }
+    public static Picture createPicture(JS r) { return platform._createPicture(r); }
     public static Font.Glyph createGlyph(org.xwt.Font f, char c) { return platform._createGlyph(f, c); }
     public static Surface createSurface(Box b, boolean framed, boolean refreshable) {
         Surface ret = platform._createSurface(b, framed);
         ret.setInvisible(false);
-        ret.setLimits(b.minwidth, b.minheight, b.maxwidth, b.maxheight);
         if (refreshable) {
             Surface.allSurfaces.addElement(ret);
             ret.dirty(0, 0, b.width, b.height);
index 4dc85bf..58b6a8b 100644 (file)
@@ -8,8 +8,6 @@ import org.xwt.util.*;
 /** Implements cooperative multitasking */
 public class Scheduler {
 
-    // FIXME: prepending events messes with keysate -- make a "no re-ordering" invariant?
-
     // Public API Exposed to org.xwt /////////////////////////////////////////////////
 
     private static Scheduler singleton;
@@ -18,9 +16,6 @@ public class Scheduler {
     /** adds a task to the back of the queue */
     public static void add(Task t) { singleton.runnable.append(t); }
 
-    /** adds a task to the front of the queue (guaranteed to run next) */
-    public static void addAtFront(Task t) { singleton.runnable.prepend(t); }
-
     public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
 
     private static Task currentTask = null;
@@ -71,7 +66,7 @@ public class Scheduler {
                 synchronized(this) {
                     for(int i=0; i<Surface.allSurfaces.size(); i++) {
                         Surface s = (Surface)Surface.allSurfaces.elementAt(i);
-                        if (currentTask instanceof JSFunction) {
+                        if (currentTask instanceof JS) {
                             s._mousex = Integer.MAX_VALUE;
                             s._mousey = Integer.MAX_VALUE;
                         } else {
index a26a28c..0d5f037 100644 (file)
@@ -1,4 +1,3 @@
-// FIXEME
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
@@ -7,136 +6,67 @@ import java.util.*;
 import java.util.zip.*;
 import org.xwt.js.*;
 import org.xwt.util.*;
+import org.xwt.translators.MSPack;
 import org.bouncycastle.util.encoders.Base64;
 
-
 /** Base class for XWT resources */
-public abstract class Stream extends JS {
-
-    /** return a resource for a given url */
-    public static final Stream fromURL(String url) throws JSExn {
-        if (url.startsWith("http://")) return new Stream.HTTP(url);
-        else if (url.startsWith("https://")) return new Stream.HTTP(url);
-        else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
-        else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null);
-        throw new JSExn("invalid resource specifier " + url);
-    }
-
-    // Base Class //////////////////////////////////////////////////////////////////////
-
-    public String typeName() { return "resource"; }
-
-    /** so that we get the same subresource each time */
-    private Hash refCache = null;
-
-    public Template t = null;
-
-    public final InputStream getInputStream() throws IOException { return getInputStream(""); }
-    public abstract InputStream getInputStream(String path) throws IOException;
-
-    public Stream addExtension(String extension) { return new Ref(this, extension); }
-
-    public Object get(Object key) throws JSExn {
-        if ("".equals(key)) {
-            try {
-                Template t = Template.getTemplate(addExtension(".xwt"));
-                return t == null ? null : t.getStatic(null);  /** FIXME VERY BAD! */
-            } catch (Exception e) {
-                Log.info(this, e);
-                return null;
-            }
-        }
-        Object ret = refCache == null ? null : refCache.get(key);
-        if (ret != null) return ret;
-        ret = new Ref(this, key);
-        if (refCache == null) refCache = new Hash();
-        refCache.put(key, ret);
-        return ret;
-    }
-
-
+public abstract class Stream extends JS.Cloneable {
 
-    // Caching //////////////////////////////////////////////////////////////////////
+    // Public Interface //////////////////////////////////////////////////////////////////////////////
 
+    public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
     public static class NotCacheableException extends Exception { }
-    public static NotCacheableException notCacheable = new NotCacheableException();
 
-    /** if it makes sense to cache a resource, the resource must return a unique key */
-    public String getCacheKey() throws NotCacheableException { throw notCacheable; }
-
-    /** subclass from this if you want a CachedInputStream for each path */
-    public static class CachedStream extends Stream {
-        private Stream parent;
-        private boolean disk = false;
-        private String key;
-        public String getCacheKey() throws NotCacheableException { return key; }
-        public String toString() { return key; }
-        private Hash cachedInputStreams = new Hash();
-        public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
-            this.parent = p; this.disk = d; this.key = p.getCacheKey();
-        }
-        public InputStream getInputStream(String path) throws IOException {
-            CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
-            if (cis == null) {
-                if (disk) {
-                    java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
-                    if (f.exists()) return new FileInputStream(f);
-                    cis = new CachedInputStream(parent.getInputStream(path), f);
-                } else {
-                    cis = new CachedInputStream(parent.getInputStream(path));
-                }
-                cachedInputStreams.put(path, cis);
-            }
-            return cis.getInputStream();
-        }
-    }
+    // streams are "sealed" by default to prevent accidental object leakage
+    public void put(Object key, Object val) throws JSExn { }
+    public Object get(Object key) throws JSExn { return null; }
 
+    // Private Interface //////////////////////////////////////////////////////////////////////////////
 
-    // Useful Subclasses //////////////////////////////////////////////////////////////////////
+    protected abstract InputStream getInputStream() throws IOException;
+    protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
 
     /** HTTP or HTTPS resource */
     public static class HTTP extends Stream {
         private String url;
         HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
-        public String toString() { return url; }
-        public String getCacheKey() throws NotCacheableException { return url; }
-        public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
+        public Object get(Object key) throws JSExn { return new HTTP(url + "/" + (String)key); }
+        public String getCacheKey(Vec path) throws NotCacheableException { return url; }
+        public InputStream getInputStream() throws IOException { return new org.xwt.HTTP(url).GET(); }
     }
 
     /** byte arrays */
     public static class ByteArray extends Stream {
         private byte[] bytes;
-        private String cacheKey = null;
+        private String cacheKey;
         ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
-        public String toString() { return "byte[]"; }
         public String getCacheKey() throws NotCacheableException { return cacheKey; }
-        public InputStream getInputStream(String path) throws IOException {
-            if (!"".equals(path)) throw new IOException("can't get subresources of a byte[] resource");
-            return new ByteArrayInputStream(bytes);
-        }
+        public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
     }
 
     /** a file */
     public static class File extends Stream {
         private String path;
-        File(String path) {
-            while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
-            this.path = path;
-        }
+        File(String path) { this.path = path; }
         public String toString() { return "file:" + path; }
-        public String getCacheKey() throws NotCacheableException { throw notCacheable; }  // already on the disk!
-        public InputStream getInputStream(String rest) throws IOException {
-            return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
+        public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
+        public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
+        public Object get(Object key) throws JSExn { return new File(path + java.io.File.separatorChar + (String)key); }
     }
 
     /** "unwrap" a Zip archive */
     public static class Zip extends Stream {
         private Stream parent;
-        Zip(Stream parent) { this.parent = parent; }
-        public String toString() { return parent.toString() + "!zip"; }
+        private String path;
+        Zip(Stream parent) { this(parent, null); }
+        Zip(Stream parent, String path) {
+            this.parent=parent;
+            while (path != null && path.startsWith("/")) path = path.substring(1);
+            this.path=path;
+        }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
-        public InputStream getInputStream(String path) throws IOException {
-            if (path.startsWith("/")) path = path.substring(1);
+        public Object get(Object key) throws JSExn { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
+        public InputStream getInputStream() throws IOException {
             InputStream pis = parent.getInputStream();
             ZipInputStream zis = new ZipInputStream(pis);
             ZipEntry ze = zis.getNextEntry();
@@ -149,70 +79,29 @@ public abstract class Stream extends JS {
     /** "unwrap" a Cab archive */
     public static class Cab extends Stream {
         private Stream parent;
-        Cab(Stream parent) { this.parent = parent; }
-        public String toString() { return parent.toString() + "!cab"; }
+        private String path;
+        Cab(Stream parent) { this(parent, null); }
+        Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
-        public InputStream getInputStream(String path) throws IOException {
-            if (path.startsWith("/")) path = path.substring(1);
-            return new org.xwt.translators.MSPack(parent.getInputStream()).getInputStream(path);
-        }
+        public Object get(Object key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
+        public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
     }
 
     /** the Builtin resource */
     public static class Builtin extends Stream {
         public Builtin() { };
-        public String getCacheKey() throws NotCacheableException { throw notCacheable; }    // not cacheable
-        public String toString() { return "builtin:"; }
-        public InputStream getInputStream(String path) throws IOException {
-            if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
-            return Platform.getBuiltinInputStream();
-        }
-    }
-
-    /** what you get when you reference a subresource */
-    public static class Ref extends Stream {
-        Stream parent;
-        Object key;
-        public String toString() { return parent.toString() + "/" + key; }
-        Ref(Stream parent, Object key) { this.parent = parent; this.key = key; }
-        public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "/" + key; }
-        public Stream addExtension(String extension) {
-            return ((String)key).endsWith(extension) ? this : new Ref(parent, key + extension); }
-        public InputStream getInputStream(String path) throws IOException { return parent.getInputStream("/" + key + path); }
-    }
-
-    /** provides redirection of a specified key */
-    public static class Graft extends Stream {
-        Stream graftee;
-        Object replaced_key, replaced_val;
-        Graft(Stream 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(); }
+        public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
+        public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
     }
 
     /** shadow resource which replaces the graft */
     public static class ProgressWatcher extends Stream {
         final Stream watchee;
-        JSFunction callback;
-        ProgressWatcher(Stream watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
-        public String toString() { return watchee.toString(); }
+        JS callback;
+        ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
-        public InputStream getInputStream(String s) throws IOException {
-            final InputStream is = watchee.getInputStream(s);
+        public InputStream getInputStream() throws IOException {
+            final InputStream is = watchee.getInputStream();
             return new FilterInputStream(is) {
                     int bytesDownloaded = 0;
                     public int read() throws IOException {
@@ -232,4 +121,27 @@ public abstract class Stream extends JS {
                 };
         }
     }
+
+    /** subclass from this if you want a CachedInputStream for each path */
+    public static class CachedStream extends Stream {
+        private Stream parent;
+        private boolean disk = false;
+        private String key;
+        public String getCacheKey() throws NotCacheableException { return key; }
+        CachedInputStream cis = null;
+        public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
+            this.parent = p; this.disk = d; this.key = p.getCacheKey();
+        }
+        public InputStream getInputStream() throws IOException {
+            if (cis != null) return cis.getInputStream();
+            if (!disk) {
+                cis = new CachedInputStream(parent.getInputStream());
+            } else {
+                java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
+                if (f.exists()) return new FileInputStream(f);
+                cis = new CachedInputStream(parent.getInputStream(), f);
+            }
+            return cis.getInputStream();
+        }
+    }
 }
index 5e517c8..99533f6 100644 (file)
@@ -79,7 +79,6 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     public abstract void setTitleBarText(String s);    ///< Sets the surface's title bar text, if applicable
     public abstract void setIcon(Picture i);           ///< Sets the surface's title bar text, if applicable
     public abstract void _dispose();                   ///< Destroy the surface
-    public void setLimits(int min_width, int min_height, int max_width, int max_height) { /* FIXME */ }
 
     protected void setSize(int w, int h) {
         if (w == actualWidth && h == actualHeight) return;