1 package com.ca.commons.cbutil; 2 3 import java.awt.*; 4 import java.io.InputStream ; 5 import java.util.*; 6 import java.util.logging.Logger ; 7 import java.util.zip.ZipException ; 8 9 16 17 18 public class CBResourceLoader 19 { 20 23 24 protected HashSet unknownResources = new HashSet(); 25 26 29 30 protected CBJarResource[] resourceFiles = null; 31 32 35 36 protected static boolean debug = false; 37 38 39 private static Logger log = Logger.getLogger(CBResourceLoader.class.getName()); 40 41 45 46 public CBResourceLoader() 47 { 48 log.fine("Started CBResourceLoader"); 49 } 50 51 54 55 public void addResource(CBJarResource resource) 56 { 57 int size = (resourceFiles == null) ? 0 : (resourceFiles.length); 58 59 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 76 77 public InputStream getInputStream(String resourceName) throws ZipException 78 { 79 CBJarResource resourceFile = getJarContainingResource(resourceName); 80 if (resourceFile != null) 81 return resourceFile.getInputStream(resourceName); 82 83 throw new ZipException ("File: '" + resourceName + "' not found"); 84 } 85 86 90 91 public Image getImage(String imageName, Toolkit imageCreator) throws ZipException 92 { 93 CBJarResource resourceFile = getJarContainingResource(imageName); 94 if (resourceFile != null) 95 return resourceFile.getImage(imageName, imageCreator); 96 97 throw new ZipException ("Image File: '" + imageName + "' not found"); 98 } 99 100 105 106 public byte[] getResource(String resourceName) throws ZipException 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 ("File: '" + resourceName + "' not found"); 116 117 } 118 119 public long getLastModified(String resourceName) throws ZipException 120 { 121 CBJarResource resourceFile = getJarContainingResource(resourceName); 122 if (resourceFile != null) 123 { 124 return resourceFile.getLastModified(); 125 } 126 throw new ZipException ("File: '" + resourceName + "' not found"); 127 128 } 129 130 131 134 public CBJarResource getJarContainingResource(String resourceName) 135 { 136 if (resourceFiles == null) return null; 138 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 148 unknownResources.add(resourceName); 149 150 return null; } 152 153 158 159 public String [] getPrefixedResources(String prefix) 160 { 161 if (resourceFiles == null) return new String []{}; 163 Vector resources = new Vector(); 164 165 168 for (int i = 0; i < resourceFiles.length; i++) 169 { 170 resources.addAll(Arrays.asList(resourceFiles[i].getPrefixedResources(prefix))); 171 } 172 173 if (resources.size() == 0) 175 { 176 return new String []{}; 177 } 178 else 179 { 180 return (String []) resources.toArray(new String [resources.size()]); 181 } 182 } 183 184 191 192 229 } | Popular Tags |