KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBResourceLoader


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

16
17
18 public class CBResourceLoader
19 {
20     /**
21      * a cached list of 'unknown' resources that have been (unsuccessfully) looked up before.
22      */

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

29
30     protected CBJarResource[] resourceFiles = null;
31
32     /**
33      * Whether to print out shtuff on the way.
34      */

35
36     protected static boolean debug = false;
37
38
39     private static Logger JavaDoc log = Logger.getLogger(CBResourceLoader.class.getName());
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 CBResourceLoader()
47     {
48         log.fine("Started CBResourceLoader");
49     }
50
51     /**
52      * Adds a new zip/jar resource file to the list of files to be examined for resources.
53      */

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

63         CBJarResource[] newArray = new CBJarResource[size + 1];
64         for (int i = 0; i < size; i++)
65             newArray[i] = resourceFiles[i];
66         newArray[size] = resource;
67         resourceFiles = newArray;
68
69         log.fine("Added CBJarResource: " + resource.toString());
70     }
71
72     /**
73      * Search all registered zip files for a particular file, and return an input stream to that
74      * file.
75      */

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

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

105
106     public byte[] getResource(String JavaDoc resourceName) throws ZipException JavaDoc
107     {
108         log.finer(" $$ getting resource '" + resourceName + "' from CBResourceLoader");
109         CBJarResource resourceFile = getJarContainingResource(resourceName);
110         if (resourceFile != null)
111         {
112             log.finer(" $$ found resource '" + resourceName + "' in zip file '" + resourceFile.getZipFileName() + "' - extracting");
113             return resourceFile.getResource(resourceName);
114         }
115         throw new ZipException JavaDoc("File: '" + resourceName + "' not found");
116
117     }
118
119     public long getLastModified(String JavaDoc resourceName) throws ZipException JavaDoc
120     {
121         CBJarResource resourceFile = getJarContainingResource(resourceName);
122         if (resourceFile != null)
123         {
124             return resourceFile.getLastModified();
125         }
126         throw new ZipException JavaDoc("File: '" + resourceName + "' not found");
127
128     }
129
130
131     /**
132      * Searches an internal hash, followed by all Jar files, for a particular resource.
133      */

134     public CBJarResource getJarContainingResource(String JavaDoc resourceName)
135     {
136         if (resourceFiles == null) return null; // we don't have any resources...
137

138         // check to see if we've already looked for this resource.
139
if (unknownResources.contains(resourceName)) return null;
140
141         for (int i = 0; i < resourceFiles.length; i++)
142             if (resourceFiles[i].hasResource(resourceName))
143                 return resourceFiles[i];
144
145         // nothing found! Add an entry to the unknownResources hashset so that we don't look for
146
// it again.
147

148         unknownResources.add(resourceName);
149
150         return null; // nothing found
151
}
152
153     /**
154      * Returns all resources with the given prefix.
155      *
156      * @param prefix a string to match the start of resources against, e.g. 'icons/'
157      */

158
159     public String JavaDoc[] getPrefixedResources(String JavaDoc prefix)
160     {
161         if (resourceFiles == null) return new String JavaDoc[]{}; // don't have anything.
162

163         Vector resources = new Vector();
164
165         // cycle through all resource files, gathering prefixed resources.
166
// name clashes are simply included twice :-)
167

168         for (int i = 0; i < resourceFiles.length; i++)
169         {
170             resources.addAll(Arrays.asList(resourceFiles[i].getPrefixedResources(prefix)));
171         }
172         
173         // cast stuff back to string for return.
174
if (resources.size() == 0)
175         {
176             return new String JavaDoc[]{};
177         }
178         else
179         {
180             return (String JavaDoc[]) resources.toArray(new String JavaDoc[resources.size()]);
181         }
182     }
183
184     /**
185      * This is a very PRIMATIVE wildcard matching routine - it allows
186      * only ONE wildcard, and that wildcard MUST be a '*' character.
187      * (So in effect this is simply a prefix + suffix match).
188      *
189      * @param exp a SIMPLE wildcard expression to match, e.g. 'templates/plain*.html'
190      */

191
192 /*
193 XXX Under Development...
194
195     public String[] getWildCardResources(String exp)
196     {
197         if (resourceFiles == null) return new String[]{}; // don't have anything.to search
198
199         int wildpos = exp.indexOf('*');
200         if (wildpos == -1) return new String[]{exp};
201
202         if (wildpos == exp.length() - 1) // i.e. last character
203             return getPrefixedResources(exp.substring(0, exp.length() - 1));
204
205         Vector resources = new Vector();
206
207         // cycle through all resource files, gathering prefixed resources.
208         // name clashes are simply included twice :-)
209
210         String prefix = exp.substring(0, wildpos);
211         String suffix = exp.substring(wildpos + 1);
212
213         for (int i = 0; i < resourceFiles.length; i++)
214         {
215             resources.addAll(Arrays.asList(resourceFiles[i].getBoundedResources(prefix, suffix)));
216         }
217
218         // cast stuff back to string for return.
219         if (resources.size() == 0)
220         {
221             return new String[]{};
222         }
223         else
224         {
225             return (String[]) resources.toArray(new String[resources.size()]);
226         }
227     }
228 */

229 }
Popular Tags