1 21 22 package com.izforge.izpack.installer; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 29 import javax.swing.ImageIcon ; 30 31 48 public class ResourceManager 49 { 50 51 56 private String locale = ""; 57 58 59 protected final String resourceBasePath = "/res/"; 60 61 62 private AutomatedInstallData installData; 63 64 65 private static ResourceManager instance = null; 66 67 72 protected ResourceManager(AutomatedInstallData data) 73 { 74 this.installData = data; 75 if (data.localeISO3 != null) 76 { 77 this.locale = data.localeISO3; 78 } 79 else 80 { 81 this.locale = installData.xmlData.getAttribute("langpack", "eng"); 83 } 84 } 85 86 96 public static ResourceManager create(AutomatedInstallData data) 97 { 98 if (ResourceManager.instance == null) ResourceManager.instance = new ResourceManager(data); 99 100 return ResourceManager.instance; 101 } 102 103 108 public static ResourceManager getInstance() 109 { 110 return ResourceManager.instance; 111 } 112 113 122 private String getLanguageResourceString(String resource) throws ResourceNotFoundException 123 { 124 InputStream in; 125 String resourcePath = this.resourceBasePath + resource + "_" + this.locale; 126 in = ResourceManager.class.getResourceAsStream(resourcePath); 127 if (in != null) 128 return resourcePath; 129 else 130 { 131 resourcePath = this.resourceBasePath + resource; 133 in = ResourceManager.class.getResourceAsStream(resourcePath); 134 if (in != null) 135 return resourcePath; 136 else 137 throw new ResourceNotFoundException("Can not find Resource " + resource 138 + " for language " + this.locale); 139 } 140 } 141 142 152 public InputStream getInputStream(String resource) throws ResourceNotFoundException 153 { 154 String resourcepath = this.getLanguageResourceString(resource); 155 return ResourceManager.class.getResourceAsStream(resourcepath); 157 } 158 159 167 public URL getURL(String resource) throws ResourceNotFoundException 168 { 169 try 170 { 171 return this.getClass().getResource( 172 this.getLanguageResourceString(resource + "_" + installData.localeISO3)); 173 } 174 catch (Exception ex) 175 { 176 return this.getClass().getResource(this.getLanguageResourceString(resource)); 177 } 178 } 179 180 190 public String getTextResource(String resource, String encoding) throws ResourceNotFoundException, IOException 192 { 193 InputStream in = null; 194 try 195 { 196 in = this.getInputStream(resource + "_" + this.installData.localeISO3); 197 } 198 catch (Exception ex) 199 { 200 in = this.getInputStream(resource); 201 } 202 203 ByteArrayOutputStream infoData = new ByteArrayOutputStream (); 204 byte[] buffer = new byte[5120]; 205 int bytesInBuffer; 206 while ((bytesInBuffer = in.read(buffer)) != -1) 207 infoData.write(buffer, 0, bytesInBuffer); 208 209 if (encoding != null){ 210 return infoData.toString(encoding); 211 } 212 else { 213 return infoData.toString(); 214 } 215 } 216 217 226 public String getTextResource(String resource) throws ResourceNotFoundException, IOException 228 { 229 return this.getTextResource(resource,null); 230 } 231 232 240 public ImageIcon getImageIconResource(String resource) throws ResourceNotFoundException, 241 IOException 242 { 243 return new ImageIcon (this.getURL(resource)); 244 } 245 246 253 public void setLocale(String locale) 254 { 255 this.locale = locale; 256 } 257 258 265 public String getLocale() 266 { 267 return this.locale; 268 } 269 } 270 | Popular Tags |