2002/07/16 00:39:23
[org.ibex.core.git] / src / org / xwt / Resources.java
index eebef05..c11562c 100644 (file)
@@ -25,12 +25,12 @@ public class Resources {
     /** Holds resources added at runtime. Initialized to hold 2000 to work around a NetscapeJVM bug. */
     private static Hash bytes = new Hash(2000, 3);
 
-    /** The number of bytes read from the initial-xwar stream; used to display a progress bar on the splash screen */
-    public static int bytesDownloaded = 0;
+    /** keeps track of which archive loaded templates into which package */
+    private static Hash usedPackages = new Hash();
 
     /** Returns true iff <tt>name</tt> is a valid resource name */
     private static boolean validResourceName(String name) {
-        if (name == null || name.equals("")) return false;
+        if (name == null || name.equals("")) return true;
         if (name.endsWith("/box.xwt") || name.endsWith("/svg.xwt")) return false;
         if (name.equals("box.xwt") || name.equals("svg.xwt")) return false;
         if (!((name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') ||
@@ -56,17 +56,20 @@ public class Resources {
             String name = prefix + subfiles[i];
             File file = new File(dir.getPath() + File.separatorChar + subfiles[i]);
             if (file.isDirectory()) loadDirectory(file, name + File.separatorChar);
-            else {
-                bytes.put(name.replace(File.separatorChar, '.'), file);
-                bytesDownloaded += file.length();
-                Main.updateSplashScreen();
-            }
+            else bytes.put(name.replace(File.separatorChar, '.'), file);
         }
     }
 
     /** Load an archive from an inputstream. */
-    public static synchronized void loadArchive(InputStream is) throws IOException {
+    public static synchronized void loadArchive(InputStream is) throws IOException { loadArchive(is, 0, null); }
+    public static synchronized void loadArchive(InputStream is, final int length, final Function callback) throws IOException {
+
+        // random placeholder
+        Object thisArchive = new Object();
+
         ZipInputStream zis = new ZipInputStream(new FilterInputStream(is) {
+                int bytesDownloaded = 0;
+                boolean clear = true;
                 public int read() throws IOException {
                     bytesDownloaded++;
                     return super.read();
@@ -74,23 +77,46 @@ public class Resources {
                 public int read(byte[] b, int off, int len) throws IOException {
                     int ret = super.read(b, off, len);
                     if (ret != -1) bytesDownloaded += ret;
-                    Main.updateSplashScreen();
+                    if (clear && callback != null) {
+                        clear = false;
+                        ThreadMessage.newthread(new JSObject.JSFunction() {
+                                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                                    try {
+                                        callback.call(cx, null, null, new Object[] { new Double(bytesDownloaded), new Double(length) });
+                                    } finally {
+                                        clear = true;
+                                    }
+                                    return null;
+                                }                            
+                            });
+                    }
                     return ret;
                 }
             });
+
         for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
             String name = ze.getName();
             if (!validResourceName(name.substring(name.lastIndexOf('/') + 1))) {
                 if (Log.on) Log.log(Resources.class, "WARNING: ignoring xwar entry with invalid name: " + name);
                 continue;
             }
-            if (ze.isDirectory()) {
-                new Static(name.replace('/', '.'));
-                continue;
-            }
+
             if (name.endsWith(".xwt")) {
-                Template.buildTemplate(zis, name.substring(0, name.length() - 4).replace('/', '.'));
-                bytes.put(name.replace('/', '.'), new byte[] { });                          // placeholder so resolveResource() works properly
+                // placeholder so resolveResource() works properly
+                bytes.put(name.replace('/', '.'), new byte[] { });
+                name = name.substring(0, name.length() - 4);
+
+                String packageName = JSObject.nodeNameToPackageName(name.replace('/', '.'));
+                Object user = usedPackages.get(packageName);
+                if (user != null && user != thisArchive) {
+                    if (Log.on) Log.log(Resources.class, "templates have already been loaded into " + packageName + "; refusing to load " + name);
+                } else {
+                    usedPackages.put(packageName, thisArchive);
+                    for(int i=0; i<name.length() && i != -1; i = name.indexOf('/', i + 1))
+                        Static.createStatic(name.substring(0, i).replace('/', '.'));
+                    Template.buildTemplate(zis, name.replace('/', '.'));
+                }
+
             } else {
                 bytes.put(name.replace('/', '.'), isToByteArray(zis));
             }