2003/07/05 22:11:55
[org.ibex.core.git] / src / org / xwt / Resources.java
index ae2a1f4..615ef97 100644 (file)
@@ -4,10 +4,9 @@ package org.xwt;
 import java.io.*;
 import java.net.*;
 import java.util.*;
-import jazz.*;
+import java.util.zip.*;
 import java.lang.*;
-import java.applet.*;
-import org.mozilla.javascript.*;
+import org.xwt.js.*;
 import org.xwt.util.*;
 
 /**
@@ -25,12 +24,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') ||
@@ -39,9 +38,8 @@ public class Resources {
             char c = name.charAt(i);
             if (!((c >= 'A' && c <= 'Z') ||
                   (c >= 'a' && c <= 'z') ||
-                  c == '_' ||
-                  (c >= '0' && c <= '9') ||
-                  (c == '.' && i == name.length() - 4))) return false;
+                  c == '_' || c == '.' || 
+                  (c >= '0' && c <= '9'))) return false;
         }
         return true;
     }
@@ -49,58 +47,79 @@ public class Resources {
     /** Load a directory as if it were an archive */
     public static synchronized void loadDirectory(File dir) throws IOException { loadDirectory(dir, ""); }
     private static synchronized void loadDirectory(File dir, String prefix) throws IOException {
-        new Static(prefix.replace(File.separatorChar, '.'));
+        String n = prefix.replace(File.separatorChar, '.');
+        if (n.endsWith(".")) n = n.substring(0, n.length() - 1);
+        new Static(n);
         String[] subfiles = dir.list();
         for(int i=0; i<subfiles.length; i++) {
-
-            if (subfiles[i].equals("CVS")) {
-                if (Log.on) Log.log(Resources.class, "ignoring CVS/ directory");
-                continue;
-            }
-            if (!validResourceName(subfiles[i])) {
-                if (subfiles[i].charAt(subfiles[i].length() - 1) != '~')
-                    if (Log.on) Log.log(Resources.class, "WARNING: ignoring file entry with invalid name: " + subfiles[i]);
-                continue;
-            }
-
+            if (subfiles[i].equals("CVS") || !validResourceName(subfiles[i])) continue;
             String name = prefix + subfiles[i];
             File file = new File(dir.getPath() + File.separatorChar + subfiles[i]);
-            if (file.isDirectory()) loadDirectory(file, name + File.separatorChar);
-            else {
+            if (file.isDirectory()) {
+                loadDirectory(file, name + File.separatorChar);
+            } else {
+                if (name.endsWith(".xwt")) {
+                    String name2 = name.substring(0, name.length() - 4);
+                    Static.createStatic(name2.replace(File.separatorChar, '.'), false);
+                }
                 bytes.put(name.replace(File.separatorChar, '.'), file);
-                bytesDownloaded += file.length();
-                Main.updateSplashScreen();
             }
         }
     }
 
     /** 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 JS.Callable 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();
                 }
                 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 JS.Callable() {
+                                public Object call(JS.Array args_) throws JS.Exn {
+                                    try {
+                                        JS.Array args = new JS.Array();
+                                        args.addElement(new Double(bytesDownloaded));
+                                        args.addElement(new Double(length));
+                                        callback.call(args);
+                                    } finally {
+                                        clear = true;
+                                    }
+                                    return null;
+                                }                            
+                            });
+                    }
+                    bytesDownloaded += ret;
                     return ret;
                 }
             });
+
+        Template.TemplateHelper t = new Template.TemplateHelper();
+
         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);
+                Static.createStatic(name.replace('/', '.'), false);
+                Template.buildTemplate(zis, name.replace('/', '.'), t);
+
             } else {
                 bytes.put(name.replace('/', '.'), isToByteArray(zis));
             }