1 55 package org.lateralnz.common.util; 56 57 import java.io.File ; 58 import java.io.FileInputStream ; 59 import java.io.FileNotFoundException ; 60 import java.io.InputStream ; 61 import java.io.IOException ; 62 63 import java.util.Enumeration ; 64 import java.util.HashMap ; 65 import java.util.Iterator ; 66 import java.util.Locale ; 67 import java.util.Map ; 68 import java.util.MissingResourceException ; 69 import java.util.Properties ; 70 import java.util.ResourceBundle ; 71 72 77 public final class ResourceUtils implements Constants { 78 private ResourceUtils() { 79 } 80 81 public static final Properties getProperties(String filename) throws IOException , FileNotFoundException { 82 File f = new File (filename); 83 InputStream is = null; 84 try { 85 if (f.exists()) { 86 is = new FileInputStream (filename); 87 } 88 else { 89 is = ResourceUtils.class.getResourceAsStream(FORWARD_SLASH + filename); 92 } 93 if (is == null) { 94 throw new FileNotFoundException (filename); 95 } 96 Properties p = new Properties (); 97 p.load(is); 98 99 return p; 100 } 101 finally { 102 IOUtils.close(is); 103 } 104 } 105 106 109 public static final ResourceBundle getStaticBundle(Class c) { 110 return getStaticBundle(c.getName(), Locale.getDefault(), c); 111 } 112 113 116 public static final ResourceBundle getStaticBundle(String name, Class c) { 117 return getStaticBundle(name, Locale.getDefault(), c); 118 } 119 120 124 public static final ResourceBundle getStaticBundle(String name, Locale locale, Class c) { 125 ResourceBundle rb = null; 126 127 try { 128 rb = ResourceBundle.getBundle(name, locale); 129 } 130 catch (Exception e1) { 131 try { 132 rb = ResourceBundle.getBundle(name, locale, Thread.currentThread().getContextClassLoader()); 133 } 134 catch (Exception e2) { 135 rb = ResourceBundle.getBundle(name, locale, c.getClassLoader()); 136 } 137 } 138 139 return rb; 140 } 141 142 public static final String getString(ResourceBundle res, String key, String def) { 143 try { 144 return res.getString(key); 145 } 146 catch (MissingResourceException mre) { 147 return def; 148 } 149 } 150 151 public static final String getString(ResourceBundle res, String key) { 152 return getString(res, key, EMPTY); 153 } 154 155 public static final boolean hasString(ResourceBundle resources, String key) { 156 try { 157 if (!StringUtils.isEmpty(resources.getString(key))) { 158 return true; 159 } 160 } 161 catch (MissingResourceException mre) { } 162 return false; 163 } 164 165 168 public static final void loadProperties(Properties props, Map map) { 169 Iterator iter = map.keySet().iterator(); 170 String key; 171 while (iter.hasNext()) { 172 key = (String )iter.next(); 173 props.setProperty(key, (String )map.get(key)); 174 } 175 } 176 177 180 public static final Map toMap(ResourceBundle res) { 181 HashMap map = new HashMap (); 182 Enumeration en = res.getKeys(); 183 while (en.hasMoreElements()) { 184 String key = (String )en.nextElement(); 185 map.put(key, res.getObject(key)); 186 } 187 return map; 188 } 189 190 193 public static final Properties toProperties(ResourceBundle res) { 194 Properties props = new Properties (); 195 Enumeration en = res.getKeys(); 196 while (en.hasMoreElements()) { 197 String key = (String )en.nextElement(); 198 props.setProperty(key, res.getString(key)); 199 } 200 return props; 201 } 202 } | Popular Tags |