1 24 package org.ofbiz.base.util.collections; 25 26 import java.io.Serializable ; 27 import java.util.Collection ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import java.util.MissingResourceException ; 32 import java.util.ResourceBundle ; 33 import java.util.Set ; 34 35 import org.ofbiz.base.util.UtilProperties; 36 37 38 45 public class ResourceBundleMapWrapper implements Map , Serializable { 46 47 protected MapStack rbmwStack; 48 protected ResourceBundle initialResourceBundle; 49 50 protected ResourceBundleMapWrapper() { 51 rbmwStack = MapStack.create(); 52 } 53 54 57 public ResourceBundleMapWrapper(InternalRbmWrapper initialInternalRbmWrapper) { 58 this.initialResourceBundle = initialInternalRbmWrapper.getResourceBundle(); 59 this.rbmwStack = MapStack.create(initialInternalRbmWrapper); 60 } 61 62 65 public ResourceBundleMapWrapper(ResourceBundle initialResourceBundle) { 66 if (initialResourceBundle == null) { 67 throw new IllegalArgumentException ("Cannot create ResourceBundleMapWrapper with a null initial ResourceBundle."); 68 } 69 this.initialResourceBundle = initialResourceBundle; 70 this.rbmwStack = MapStack.create(new InternalRbmWrapper(initialResourceBundle)); 71 } 72 73 74 public void addBottomResourceBundle(ResourceBundle topResourceBundle) { 75 this.rbmwStack.addToBottom(new InternalRbmWrapper(topResourceBundle)); 76 } 77 78 79 public void addBottomResourceBundle(InternalRbmWrapper topInternalRbmWrapper) { 80 this.rbmwStack.addToBottom(topInternalRbmWrapper); 81 } 82 83 84 public void addBottomResourceBundle(String resource) { 85 if (this.initialResourceBundle == null) { 86 throw new IllegalArgumentException ("Cannot add bottom resource bundle, this wrapper was not properly initialized (there is no base/initial ResourceBundle)."); 87 } 88 this.addBottomResourceBundle(UtilProperties.getInternalRbmWrapper(resource, this.initialResourceBundle.getLocale())); 89 } 90 91 94 public void pushResourceBundle(ResourceBundle topResourceBundle) { 95 this.rbmwStack.push(new InternalRbmWrapper(topResourceBundle)); 96 } 97 98 public ResourceBundle getInitialResourceBundle() { 99 return this.initialResourceBundle; 100 } 101 102 public void clear() { 103 this.rbmwStack.clear(); 104 } 105 public boolean containsKey(Object arg0) { 106 return this.rbmwStack.containsKey(arg0); 107 } 108 public boolean containsValue(Object arg0) { 109 return this.rbmwStack.containsValue(arg0); 110 } 111 public Set entrySet() { 112 return this.rbmwStack.entrySet(); 113 } 114 public Object get(Object arg0) { 115 Object value = this.rbmwStack.get(arg0); 116 if (value == null) { 117 value = arg0; 118 } 119 return value; 120 } 121 public boolean isEmpty() { 122 return this.rbmwStack.isEmpty(); 123 } 124 public Set keySet() { 125 return this.keySet(); 126 } 127 public Object put(Object key, Object value) { 128 return this.rbmwStack.put(key, value); 129 } 130 public void putAll(Map arg0) { 131 this.rbmwStack.putAll(arg0); 132 } 133 public Object remove(Object arg0) { 134 return this.rbmwStack.remove(arg0); 135 } 136 public int size() { 137 return this.rbmwStack.size(); 138 } 139 public Collection values() { 140 return this.rbmwStack.values(); 141 } 142 143 public static class InternalRbmWrapper implements Map , Serializable { 144 protected ResourceBundle resourceBundle; 145 protected Map topLevelMap; 146 147 public InternalRbmWrapper(ResourceBundle resourceBundle) { 148 if (resourceBundle == null) { 149 throw new IllegalArgumentException ("Cannot create InternalRbmWrapper with a null ResourceBundle."); 150 } 151 this.resourceBundle = resourceBundle; 152 topLevelMap = new HashMap (); 153 if (resourceBundle != null) { 155 Enumeration keyNum = resourceBundle.getKeys(); 156 while (keyNum.hasMoreElements()) { 157 String key = (String ) keyNum.nextElement(); 158 Object value = resourceBundle.getObject(key); 160 topLevelMap.put(key, value); 161 } 162 } 163 topLevelMap.put("_RESOURCE_BUNDLE_", resourceBundle); 164 } 165 166 169 public int size() { 170 return topLevelMap.size() - 1; 172 } 173 174 177 public boolean isEmpty() { 178 return topLevelMap.isEmpty(); 179 } 180 181 184 public boolean containsKey(Object arg0) { 185 if (topLevelMap.containsKey(arg0)) { 186 return true; 187 } else { 188 try { 189 if (this.resourceBundle.getObject((String ) arg0) != null) { 190 return true; 191 } 192 } catch (MissingResourceException e) { 193 } 195 } 196 return false; 197 } 198 199 202 public boolean containsValue(Object arg0) { 203 throw new RuntimeException ("Not implemented for ResourceBundleMapWrapper"); 204 } 205 206 209 public Object get(Object arg0) { 210 Object value = this.topLevelMap.get(arg0); 211 if (resourceBundle != null) { 212 if (value == null) { 213 try { 214 value = this.resourceBundle.getObject((String ) arg0); 215 } catch(MissingResourceException mre) { 216 } 218 } 219 if (value == null) { 220 try { 221 value = this.resourceBundle.getString((String ) arg0); 222 } catch(MissingResourceException mre) { 223 } 225 } 226 } 227 232 return value; 233 } 234 235 238 public Object put(Object arg0, Object arg1) { 239 throw new RuntimeException ("Not implemented/allowed for ResourceBundleMapWrapper"); 240 } 241 242 245 public Object remove(Object arg0) { 246 throw new RuntimeException ("Not implemented for ResourceBundleMapWrapper"); 247 } 248 249 252 public void putAll(Map arg0) { 253 throw new RuntimeException ("Not implemented for ResourceBundleMapWrapper"); 254 } 255 256 259 public void clear() { 260 throw new RuntimeException ("Not implemented for ResourceBundleMapWrapper"); 261 } 262 263 266 public Set keySet() { 267 return this.topLevelMap.keySet(); 268 } 269 270 273 public Collection values() { 274 return this.topLevelMap.values(); 275 } 276 277 280 public Set entrySet() { 281 return this.topLevelMap.entrySet(); 282 } 283 284 public ResourceBundle getResourceBundle() { 285 return this.resourceBundle; 286 } 287 288 291 } 292 } 293 | Popular Tags |