X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FResources.java;h=26d4f68acb17d68bf5e7a64f2d85fc065a9edff9;hb=cb1a5438e2216c357fdfcf53b182b78875f782ab;hp=eebef059ec12bde85691f6feb7d7a9eef366867a;hpb=41046cea8b3f3129f18ea9bbb34927202b391c88;p=org.ibex.core.git diff --git a/src/org/xwt/Resources.java b/src/org/xwt/Resources.java index eebef05..26d4f68 100644 --- a/src/org/xwt/Resources.java +++ b/src/org/xwt/Resources.java @@ -4,7 +4,7 @@ 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.*; @@ -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 name 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 +39,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; } @@ -55,42 +54,77 @@ public class Resources { 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); + usedPackages.put(JSObject.nodeNameToPackageName(name2.replace('/', '.')), new Object()); + } 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 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(); } 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; + } + }); + } + 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); + + 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); + Static.createStatic(name.replace('/', '.'), false); + Template.buildTemplate(zis, name.replace('/', '.'), t); + } + } else { bytes.put(name.replace('/', '.'), isToByteArray(zis)); }