-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.xwt;
-
-import java.io.*;
-import java.net.*;
-import java.util.*;
-import java.util.zip.*;
-import java.lang.*;
-import org.xwt.js.*;
-import org.xwt.util.*;
-
-/**
- * A singleton class that acts as a repository for files obtained
- * from xwar archives or the local filesystem.
- *
- * All names are converted to resource names (dots instead of
- * slashes) when they are loaded into this repository; however,
- * filename extensions are left on, so queries (resolveResource(),
- * getResource()) should include the extension when querying for
- * resources.
- */
-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);
-
- /** 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 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') ||
- (name.charAt(0) >= 'a' && name.charAt(0) <= 'z'))) return false;
- for(int i=1; i<name.length(); i++) {
- char c = name.charAt(i);
- if (!((c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z') ||
- c == '_' || c == '.' ||
- (c >= '0' && c <= '9'))) return false;
- }
- return true;
- }
-
- /** 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 {
- 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") || !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 (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);
- }
- }
- }
-
- /** Load an archive from an inputstream. */
- 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 (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 (name.endsWith(".xwt")) {
- // 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));
- }
- }
- if (Log.verbose) Log.log(Resources.class, "done loading archive");
- }
-
- /** holds the current theme mappings */
- static Vector mapFrom = new Vector();
-
- /** holds the current theme mappings */
- static Vector mapTo = new Vector();
- static {
- mapFrom.addElement("xwt.standard");
- mapTo.addElement("org.xwt.themes.monopoly");
- }
-
- /**
- * Resolves the partial resource name <tt>name</tt> to a fully
- * resolved resource name, using <tt>importlist</tt> as a search
- * list, or null if no resource was found.
- *
- * Both the arguments and return values from this function SHOULD
- * include extensions (".xwt", ".xwf", etc) and SHOULD use dots
- * (".") instead of slashes ("/").
- */
- public static String resolve(String name, String[] importlist) {
- final int imax = importlist == null ? 0 : importlist.length;
- for(int i=-1; i < imax; i++) {
- String resolved = i == -1 ? name : (importlist[i] + '.' + name);
- for(int j=mapFrom.size() - 1; j>=0; j--) {
- String from = mapFrom.elementAt(j).toString();
- if (resolved.startsWith(from) && (resolved.endsWith(".xwt") || resolved.endsWith(".xwf"))) {
- String tryme = mapTo.elementAt(j) + resolved.substring(from.length());
- if (bytes.get(tryme) != null) return tryme;
- }
- }
- if (bytes.get(resolved) != null) return resolved;
- }
- return null;
- }
-
- /** Returns the named resource as a byte[].
- * @param name A fully resolved resource name, using slashes
- * instead of periods. If it is null, this function
- * will return null.
- */
- public static byte[] getResource(String name) {
- if (name == null) return null;
- synchronized(bytes) {
- Object o = bytes.get(name);
- if (o == null) return null;
- if (o instanceof byte[]) return ((byte[])o);
- if (o instanceof File) {
- try {
- FileInputStream fi = new FileInputStream((File)o);
- byte[] b = isToByteArray(fi);
- bytes.put(name, b);
- return b;
- } catch (Exception e) {
- if (Log.on) Log.log(Resources.class, "Exception while reading from file " + o);
- if (Log.on) Log.log(Resources.class, e);
- return null;
- }
- }
- return null;
- }
- }
-
- /** scratch space for isToByteArray() */
- private static byte[] workspace = new byte[16 * 1024];
-
- /** Trivial method to completely read an InputStream */
- public static synchronized byte[] isToByteArray(InputStream is) throws IOException {
- int pos = 0;
- while (true) {
- int numread = is.read(workspace, pos, workspace.length - pos);
- if (numread == -1) break;
- else if (pos + numread < workspace.length) pos += numread;
- else {
- pos += numread;
- byte[] temp = new byte[workspace.length * 2];
- System.arraycopy(workspace, 0, temp, 0, workspace.length);
- workspace = temp;
- }
- }
- byte[] ret = new byte[pos];
- System.arraycopy(workspace, 0, ret, 0, pos);
- return ret;
- }
-}
-
-
-