1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
10 import org.mozilla.javascript.*;
11 import org.xwt.util.*;
14 * A singleton class that acts as a repository for files obtained
15 * from xwar archives or the local filesystem.
17 * All names are converted to resource names (dots instead of
18 * slashes) when they are loaded into this repository; however,
19 * filename extensions are left on, so queries (resolveResource(),
20 * getResource()) should include the extension when querying for
23 public class Resources {
25 /** Holds resources added at runtime. Initialized to hold 2000 to work around a NetscapeJVM bug. */
26 private static Hash bytes = new Hash(2000, 3);
28 /** The number of bytes read from the initial-xwar stream; used to display a progress bar on the splash screen */
29 public static int bytesDownloaded = 0;
31 /** Returns true iff <tt>name</tt> is a valid resource name */
32 private static boolean validResourceName(String name) {
33 if (name == null || name.equals("")) return false;
34 if (name.endsWith("/box.xwt") || name.endsWith("/svg.xwt")) return false;
35 if (name.equals("box.xwt") || name.equals("svg.xwt")) return false;
36 if (!((name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') ||
37 (name.charAt(0) >= 'a' && name.charAt(0) <= 'z'))) return false;
38 for(int i=1; i<name.length(); i++) {
39 char c = name.charAt(i);
40 if (!((c >= 'A' && c <= 'Z') ||
41 (c >= 'a' && c <= 'z') ||
43 (c >= '0' && c <= '9') ||
44 (c == '.' && i == name.length() - 4))) return false;
49 /** Load a directory as if it were an archive */
50 public static synchronized void loadDirectory(File dir) throws IOException { loadDirectory(dir, ""); }
51 private static synchronized void loadDirectory(File dir, String prefix) throws IOException {
52 new Static(prefix.replace(File.separatorChar, '.'));
53 String[] subfiles = dir.list();
54 for(int i=0; i<subfiles.length; i++) {
56 if (subfiles[i].equals("CVS")) {
57 if (Log.on) Log.log(Resources.class, "ignoring CVS/ directory");
60 if (!validResourceName(subfiles[i])) {
61 if (subfiles[i].charAt(subfiles[i].length() - 1) != '~')
62 if (Log.on) Log.log(Resources.class, "WARNING: ignoring file entry with invalid name: " + subfiles[i]);
66 String name = prefix + subfiles[i];
67 File file = new File(dir.getPath() + File.separatorChar + subfiles[i]);
68 if (file.isDirectory()) loadDirectory(file, name + File.separatorChar);
70 bytes.put(name.replace(File.separatorChar, '.'), file);
71 bytesDownloaded += file.length();
72 Main.updateSplashScreen();
77 /** Load an archive from an inputstream. */
78 public static synchronized void loadArchive(InputStream is) throws IOException {
79 ZipInputStream zis = new ZipInputStream(new FilterInputStream(is) {
80 public int read() throws IOException {
84 public int read(byte[] b, int off, int len) throws IOException {
85 int ret = super.read(b, off, len);
86 if (ret != -1) bytesDownloaded += ret;
87 Main.updateSplashScreen();
91 for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
92 String name = ze.getName();
93 if (!validResourceName(name.substring(name.lastIndexOf('/') + 1))) {
94 if (Log.on) Log.log(Resources.class, "WARNING: ignoring xwar entry with invalid name: " + name);
97 if (ze.isDirectory()) {
98 new Static(name.replace('/', '.'));
101 if (name.endsWith(".xwt")) {
102 if (Log.verbose) Log.log(Resources.class, "building template " + name);
103 Template.buildTemplate(zis, name.substring(0, name.length() - 4).replace('/', '.'));
104 if (Log.verbose) Log.log(Resources.class, " done building template " + name);
105 bytes.put(name.replace('/', '.'), new byte[] { }); // placeholder so resolveResource() works properly
107 bytes.put(name.replace('/', '.'), isToByteArray(zis));
110 if (Log.verbose) Log.log(Resources.class, "done loading archive");
113 /** holds the current theme mappings */
114 static Vector mapFrom = new Vector();
116 /** holds the current theme mappings */
117 static Vector mapTo = new Vector();
120 * Resolves the partial resource name <tt>name</tt> to a fully
121 * resolved resource name, using <tt>importlist</tt> as a search
122 * list, or null if no resource was found.
124 * Both the arguments and return values from this function SHOULD
125 * include extensions (".xwt", ".xwf", etc) and SHOULD use dots
126 * (".") instead of slashes ("/").
128 public static String resolve(String name, String[] importlist) {
129 final int imax = importlist == null ? 0 : importlist.length;
130 for(int i=-1; i < imax; i++) {
131 String resolved = i == -1 ? name : (importlist[i] + '.' + name);
132 for(int j=mapFrom.size() - 1; j>=0; j--) {
133 String from = mapFrom.elementAt(j).toString();
134 if (resolved.startsWith(from) && (resolved.endsWith(".xwt") || resolved.endsWith(".xwf"))) {
135 String tryme = mapTo.elementAt(j) + resolved.substring(from.length());
136 if (bytes.get(tryme) != null) return tryme;
139 if (bytes.get(resolved) != null) return resolved;
144 /** Returns the named resource as a byte[].
145 * @param name A fully resolved resource name, using slashes
146 * instead of periods. If it is null, this function
149 public static byte[] getResource(String name) {
150 if (name == null) return null;
151 synchronized(bytes) {
152 Object o = bytes.get(name);
153 if (o == null) return null;
154 if (o instanceof byte[]) return ((byte[])o);
155 if (o instanceof File) {
157 FileInputStream fi = new FileInputStream((File)o);
158 byte[] b = isToByteArray(fi);
161 } catch (Exception e) {
162 if (Log.on) Log.log(Resources.class, "Exception while reading from file " + o);
163 if (Log.on) Log.log(Resources.class, e);
171 /** scratch space for isToByteArray() */
172 private static byte[] workspace = new byte[16 * 1024];
174 /** Trivial method to completely read an InputStream */
175 public static synchronized byte[] isToByteArray(InputStream is) throws IOException {
178 int numread = is.read(workspace, pos, workspace.length - pos);
179 if (numread == -1) break;
180 else if (pos + numread < workspace.length) pos += numread;
183 byte[] temp = new byte[workspace.length * 2];
184 System.arraycopy(workspace, 0, temp, 0, workspace.length);
188 byte[] ret = new byte[pos];
189 System.arraycopy(workspace, 0, ret, 0, pos);