KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > JXResourceLoader


1 package com.ca.directory.jxplorer;
2
3 import java.util.*;
4 import java.io.*;
5 import java.util.zip.ZipException JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.Image JavaDoc;
8
9 import com.ca.commons.cbutil.*;
10
11 /**
12  * This Class provides access to a group of zip files. In the case of namespace conflicts between
13  * Zip files, the first zip file registered has priority. The zip files are accessed through CBJarResource.
14  *
15  *
16  * @author Chris Betts
17  * @see com.ca.od.util.CBJarResource
18  */

19
20
21 public class JXResourceLoader
22 {
23     /**
24      * a cached list of 'unknown' resources that have been (unsuccessfully) looked up before.
25      */

26
27     protected HashSet unknownResources = new HashSet();
28
29     /**
30      * A list of CBJarResources objects that may be searched for packaged resources.
31      */

32
33     protected CBJarResource[] resourceFiles = null;
34
35     /**
36      * Whether to print out shtuff on the way.
37      */

38
39     protected static boolean debug = false;
40
41     /**
42      * Constructor - note that that the class is useless until at least one resource file has been
43      * registered with it using the addResource() method.
44      */

45
46     public JXResourceLoader()
47     {
48         debug = (JXplorer.getProperty("debuglevel", "0").compareTo("7") >= 0);
49
50         if (debug)
51             System.out.println("Started JXResourceLoader");
52     }
53
54     /**
55      * Adds a new zip/jar resource file to the list of files to be examined for resources.
56      */

57
58     public void addResource(CBJarResource resource)
59     {
60         int size = (resourceFiles == null)?0:(resourceFiles.length);
61
62         // The clumsiness of using an array is balanced by the need for quick
63
// access, and the fact that normally only a small number of resources are added,
64
// and only at the start of program operation.
65

66         CBJarResource[] newArray = new CBJarResource[size+1];
67         for (int i=0; i<size; i++)
68             newArray[i] = resourceFiles[i];
69         newArray[size] = resource;
70         resourceFiles = newArray;
71
72         if (debug)
73             System.out.println("Added CBJarResource: " + resource.toString());
74     }
75
76     /**
77      * Search all registered zip files for a particular file, and return an input stream to that
78      * file.
79      */

80
81     public InputStream getInputStream(String JavaDoc resourceName) throws ZipException JavaDoc
82     {
83         CBJarResource resourceFile = getJarContainingResource(resourceName);
84         if (resourceFile != null)
85             return resourceFile.getInputStream(resourceName);
86
87         throw new ZipException JavaDoc("File: '" + resourceName + "' not found");
88     }
89
90     /**
91      * Search all registered zip files for a particular Image, and return an awt Image
92      * object.
93      */

94
95     public Image JavaDoc getImage(String JavaDoc imageName, Toolkit JavaDoc imageCreator) throws ZipException JavaDoc
96     {
97         CBJarResource resourceFile = getJarContainingResource(imageName);
98         if (resourceFile != null)
99             return resourceFile.getImage(imageName, imageCreator);
100
101         throw new ZipException JavaDoc("Image File: '" + imageName + "' not found");
102     }
103
104     /**
105      * Search all registered zip files for a particular file, and return the data within that
106      * file as a byte array. (This might be used to initialise a String object if the file is
107      * a text file, for instance.)
108      */

109
110     public byte[] getResource(String JavaDoc resourceName) throws ZipException JavaDoc
111     {
112         CBJarResource resourceFile = getJarContainingResource(resourceName);
113         if (resourceFile != null)
114             return resourceFile.getResource(resourceName);
115
116         throw new ZipException JavaDoc("File: '" + resourceName + "' not found");
117
118     }
119
120     /**
121      * Searches an internal hash, followed by all Jar files, for a particular resource.
122      */

123     protected CBJarResource getJarContainingResource(String JavaDoc resourceName)
124     {
125         // check to see if we've already looked for this resource.
126
if (unknownResources.contains(resourceName)) return null;
127
128         for (int i=0; i<resourceFiles.length; i++)
129             if (resourceFiles[i].hasResource(resourceName))
130                 return resourceFiles[i];
131
132         // nothing found! Add an entry to the unknownResources hashset so that we don't look for
133
// it again.
134

135         unknownResources.add(resourceName);
136
137         return null; // nothing found
138
}
139
140     /**
141      * Returns all resources with the given prefix.
142      * @param prefix a string to match the start of resources against, e.g. 'icons/'
143      */

144     
145     public String JavaDoc[] getPrefixedResources(String JavaDoc prefix)
146     {
147         Vector resources = new Vector();
148
149         // cycle through all resource files, gathering prefixed resources.
150
// name clashes are simply included twice :-)
151

152         for (int i=0; i<resourceFiles.length; i++)
153         {
154             resources.addAll(Arrays.asList(resourceFiles[i].getPrefixedResources(prefix)));
155         }
156         
157         // cast stuff back to string for return.
158
if (resources.size() == 0)
159         {
160             return new String JavaDoc[] {};
161         }
162         else
163         {
164             return (String JavaDoc[]) resources.toArray(new String JavaDoc[resources.size()]);
165         }
166     }
167
168     /**
169      * This is a very PRIMATIVE wildcard matching routine - it allows
170      * only ONE wildcard, and that wildcard MUST be a '*' character.
171      * (So in effect this is simply a prefix + suffix match).
172      * @param exp a SIMPLE wildcard expression to match, e.g. 'templates/plain*.html'
173      */

174     
175     public String JavaDoc[] getWildCardResources(String JavaDoc exp)
176     {
177         int wildpos = exp.indexOf('*');
178         if (wildpos == -1) return new String JavaDoc[] {exp};
179         
180         if (wildpos == exp.length()-1) // i.e. last character
181
return getPrefixedResources(exp.substring(0,exp.length()-1));
182             
183         String JavaDoc prefix = exp.substring(0,wildpos);
184         String JavaDoc suffix = exp.substring(wildpos+1);
185         
186 System.out.println("found prefix: " + prefix + " suffix " + suffix);
187     
188         Vector resources = new Vector();
189
190         // cycle through all resource files, gathering prefixed resources.
191
// name clashes are simply included twice :-)
192

193         for (int i=0; i<resourceFiles.length; i++)
194         {
195 // resources.addAll(Arrays.asList(resourceFiles[i].getBoundedResources(prefix, suffix)));
196
}
197         
198         // cast stuff back to string for return.
199
if (resources.size() == 0)
200         {
201             return new String JavaDoc[] {};
202         }
203         else
204         {
205             return (String JavaDoc[]) resources.toArray(new String JavaDoc[resources.size()]);
206         }
207     }
208
209
210
211 }
Popular Tags