1 package com.ca.directory.jxplorer; 2 3 import java.util.*; 4 import java.io.*; 5 import java.util.zip.ZipException ; 6 import java.awt.Toolkit ; 7 import java.awt.Image ; 8 9 import com.ca.commons.cbutil.*; 10 11 19 20 21 public class JXResourceLoader 22 { 23 26 27 protected HashSet unknownResources = new HashSet(); 28 29 32 33 protected CBJarResource[] resourceFiles = null; 34 35 38 39 protected static boolean debug = false; 40 41 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 57 58 public void addResource(CBJarResource resource) 59 { 60 int size = (resourceFiles == null)?0:(resourceFiles.length); 61 62 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 80 81 public InputStream getInputStream(String resourceName) throws ZipException 82 { 83 CBJarResource resourceFile = getJarContainingResource(resourceName); 84 if (resourceFile != null) 85 return resourceFile.getInputStream(resourceName); 86 87 throw new ZipException ("File: '" + resourceName + "' not found"); 88 } 89 90 94 95 public Image getImage(String imageName, Toolkit imageCreator) throws ZipException 96 { 97 CBJarResource resourceFile = getJarContainingResource(imageName); 98 if (resourceFile != null) 99 return resourceFile.getImage(imageName, imageCreator); 100 101 throw new ZipException ("Image File: '" + imageName + "' not found"); 102 } 103 104 109 110 public byte[] getResource(String resourceName) throws ZipException 111 { 112 CBJarResource resourceFile = getJarContainingResource(resourceName); 113 if (resourceFile != null) 114 return resourceFile.getResource(resourceName); 115 116 throw new ZipException ("File: '" + resourceName + "' not found"); 117 118 } 119 120 123 protected CBJarResource getJarContainingResource(String resourceName) 124 { 125 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 135 unknownResources.add(resourceName); 136 137 return null; } 139 140 144 145 public String [] getPrefixedResources(String prefix) 146 { 147 Vector resources = new Vector(); 148 149 152 for (int i=0; i<resourceFiles.length; i++) 153 { 154 resources.addAll(Arrays.asList(resourceFiles[i].getPrefixedResources(prefix))); 155 } 156 157 if (resources.size() == 0) 159 { 160 return new String [] {}; 161 } 162 else 163 { 164 return (String []) resources.toArray(new String [resources.size()]); 165 } 166 } 167 168 174 175 public String [] getWildCardResources(String exp) 176 { 177 int wildpos = exp.indexOf('*'); 178 if (wildpos == -1) return new String [] {exp}; 179 180 if (wildpos == exp.length()-1) return getPrefixedResources(exp.substring(0,exp.length()-1)); 182 183 String prefix = exp.substring(0,wildpos); 184 String suffix = exp.substring(wildpos+1); 185 186 System.out.println("found prefix: " + prefix + " suffix " + suffix); 187 188 Vector resources = new Vector(); 189 190 193 for (int i=0; i<resourceFiles.length; i++) 194 { 195 } 197 198 if (resources.size() == 0) 200 { 201 return new String [] {}; 202 } 203 else 204 { 205 return (String []) resources.toArray(new String [resources.size()]); 206 } 207 } 208 209 210 211 } | Popular Tags |