1 23 24 package org.dbforms.util; 25 26 import java.util.HashMap ; 27 import java.util.Locale ; 28 import java.util.ResourceBundle ; 29 30 31 32 37 public class MessageResource { 38 42 private HashMap hashResources = new HashMap (); 43 private String subClass = null; 44 45 50 public MessageResource(String subClass) { 51 this.subClass = subClass; 52 } 53 54 64 public String getMessage(String msg, 65 Locale loc) { 66 if (subClass == null) { 67 return null; 68 } 69 70 if (loc == null) { 71 return null; 72 } 73 74 ResourceBundle rb = null; 75 76 String key = new StringBuffer ().append(loc.getLanguage()) 78 .append("_") 79 .append(loc.getCountry()) 80 .append("_") 81 .append(loc.getVariant()) 82 .toString(); 83 84 if (hashResources.containsKey(key)) { 85 rb = (ResourceBundle ) hashResources.get(key); 86 } else { 87 try { 88 rb = ResourceBundle.getBundle(subClass, loc); 89 } catch (Exception e) { 90 rb = null; 91 } 92 93 hashResources.put(key, rb); 95 } 96 97 String s = null; 98 99 if (rb != null) { 100 try { 101 s = rb.getString(msg); 102 } catch (Exception e) { 103 s = null; 104 } 105 } 106 107 return s; 108 } 109 110 111 123 public String getMessage(String msg, 124 Locale loc, 125 String [] parms) { 126 String result = getMessage(msg, loc); 127 128 if (result == null) { 129 return null; 130 } 131 132 String search = null; 133 134 for (int i = 0; i < parms.length; i++) { 135 search = "{" + i + "}"; 136 result = replaceAll(result, search, parms[i]); 137 } 138 139 return result; 140 } 141 142 143 148 public String getSubClass() { 149 return subClass; 150 } 151 152 153 162 private String replaceAll(String str, 163 String search, 164 String replace) { 165 StringBuffer result = null; 166 int oldpos = 0; 167 168 do { 169 int pos = str.indexOf(search, oldpos); 170 171 if (pos < 0) { 172 break; 173 } 174 175 if (result == null) { 176 result = new StringBuffer (); 177 } 178 179 result.append(str.substring(oldpos, pos)); 180 181 result.append(replace); 182 183 pos += search.length(); 184 oldpos = pos; 185 } while (true); 186 187 if (oldpos == 0) { 188 return str; 189 } else { 190 result.append(str.substring(oldpos)); 191 192 return new String (result); 193 } 194 } 195 } 196 | Popular Tags |