From b086f57f56bf0eb9dab9cf321a0f69aaaae84347 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 30 May 2012 08:37:45 +0200 Subject: Initial Maven Conversion --- src/main/java/com/c2kernel/utils/Resource.java | 166 +++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/main/java/com/c2kernel/utils/Resource.java (limited to 'src/main/java/com/c2kernel/utils/Resource.java') diff --git a/src/main/java/com/c2kernel/utils/Resource.java b/src/main/java/com/c2kernel/utils/Resource.java new file mode 100644 index 0000000..10228da --- /dev/null +++ b/src/main/java/com/c2kernel/utils/Resource.java @@ -0,0 +1,166 @@ +package com.c2kernel.utils; + +//Java +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Hashtable; + +import javax.swing.ImageIcon; + +import com.c2kernel.common.ObjectNotFoundException; + +/************************************************************************** + * + * @author $Author: abranson $ $Date: 2005/10/05 07:39:36 $ + * @version $Revision: 1.71 $ + **************************************************************************/ +public class Resource { + static private Hashtable txtCache = new Hashtable(); + static private Hashtable imgCache = new Hashtable(); + static private URL baseURL = getURLorResURL("com/c2kernel/utils/resources/"); + static private HashMap moduleBaseURLs = new HashMap(); + static public final ImageIcon nullImg = new ImageIcon(new byte[] { 0 }); + + public static URL getKernelBaseURL() { + return baseURL; + } + + public static URL getKernelResourceURL(String resName) throws MalformedURLException { + return new URL(baseURL, resName); + } + + public static void addModuleBaseURL(String ns, URL newBaseURL) { + moduleBaseURLs.put(ns, newBaseURL); + Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString()); + } + + public static void addModuleBaseURL(String ns, String newBaseURL) { + addModuleBaseURL(ns, getURLorResURL(newBaseURL)); + } + + public static URL getModuleBaseURL(String ns) { + return moduleBaseURLs.get(ns); + } + + public static HashMap getModuleBaseURLs() { + return moduleBaseURLs; + } + + static public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException { + return new URL(moduleBaseURLs.get(ns), resName); + } + + private static URL getURLorResURL(String newURL) { + URL result; + try { + result = new URL(newURL); + } catch (java.net.MalformedURLException ex) { + //it is assumed that a 'wrong' URL denotes a directory + //of files resolvable from the CLASSPATH + result = Resource.class.getClassLoader().getResource(newURL); + } + return result; + } + /************************************************************************** + * Gets any text resource files + **************************************************************************/ + + static public String findTextResource(String resName) { + try { + for (String ns : getModuleBaseURLs().keySet()) { + try { + return getTextResource(ns, resName); + } catch (ObjectNotFoundException ex) { } + } + return getTextResource(null, resName); + } catch (ObjectNotFoundException ex) { + Logger.warning("Text resource '"+resName+"' not found."); + return null; + } + } + + static public String getTextResource(String ns, String resName) throws ObjectNotFoundException + // throws IOException + { + Logger.msg(8, "Resource::getTextResource() - Getting resource: " + resName); + + if (txtCache.containsKey(ns+'/'+resName)) { + return txtCache.get(ns+'/'+resName); + } + + try { + + String newRes = null; + URL loc; + + if (ns == null) // kernel + loc = getKernelResourceURL(resName); + else + loc = getModuleResourceURL(ns, resName); + newRes = FileStringUtility.url2String(loc); + txtCache.put(ns+'/'+resName, newRes); + return newRes; + } catch (Exception e) { + throw new ObjectNotFoundException(e.getMessage(),null); + } + } + /** + * Gets an image from the resource directories + * + * @param resName - filename after resources/images + * @return + */ + static public ImageIcon findImage(String resName) { + try { + for (String ns : getModuleBaseURLs().keySet()) { + try { + return getImage(ns, resName); + } catch (ObjectNotFoundException ex) { } + } + return getImage(null, resName); + } catch (ObjectNotFoundException ex) { + Logger.warning("Image '"+resName+"' not found. Using null icon"); + return nullImg; + } + } + + static public ImageIcon getImage(String ns, String resName) throws ObjectNotFoundException { + if (resName == null) + return nullImg; + + if (imgCache.containsKey(ns+'/'+resName)) { + return imgCache.get(ns+'/'+resName); + } + + URL imgLocation = null; + if (ns == null) + try { + imgLocation = getKernelResourceURL("images/"+resName); + } catch (MalformedURLException ex) { } + else + try { + imgLocation = getModuleResourceURL(ns, "images/"+resName); + } catch (MalformedURLException ex) { } + + if (imgLocation!= null) { + ImageIcon newImg = new ImageIcon(imgLocation); + + if (newImg.getIconHeight() > -1) { + imgCache.put(ns+'/'+resName, newImg); + Logger.msg(0, "Loaded "+resName+" "+newImg.getIconWidth()+"x"+newImg.getIconHeight()); + return newImg; + } + } + throw new ObjectNotFoundException(); + } + + static public String getKernelVersion() { + try { + return FileStringUtility.url2String(getKernelResourceURL("textFiles/version.txt")); + } catch (Exception ex) { + return "No version info found"; + } + + } +} -- cgit v1.2.3