KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > collections > ResourceBundleMapWrapper


1 /*
2  * $Id: ResourceBundleMapWrapper.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.collections;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import org.ofbiz.base.util.UtilProperties;
36
37
38 /**
39  * Generic ResourceBundle Map Wrapper, given ResourceBundle allows it to be used as a Map
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @version $Rev: 5720 $
43  * @since 3.1
44  */

45 public class ResourceBundleMapWrapper implements Map JavaDoc, Serializable JavaDoc {
46     
47     protected MapStack rbmwStack;
48     protected ResourceBundle JavaDoc initialResourceBundle;
49
50     protected ResourceBundleMapWrapper() {
51         rbmwStack = MapStack.create();
52     }
53
54     /**
55      * When creating new from a InternalRbmWrapper the one passed to the constructor should be the most specific or local InternalRbmWrapper, with more common ones pushed onto the stack progressively.
56      */

57     public ResourceBundleMapWrapper(InternalRbmWrapper initialInternalRbmWrapper) {
58         this.initialResourceBundle = initialInternalRbmWrapper.getResourceBundle();
59         this.rbmwStack = MapStack.create(initialInternalRbmWrapper);
60     }
61     
62     /**
63      * When creating new from a ResourceBundle the one passed to the constructor should be the most specific or local ResourceBundle, with more common ones pushed onto the stack progressively.
64      */

65     public ResourceBundleMapWrapper(ResourceBundle JavaDoc initialResourceBundle) {
66         if (initialResourceBundle == null) {
67             throw new IllegalArgumentException JavaDoc("Cannot create ResourceBundleMapWrapper with a null initial ResourceBundle.");
68         }
69         this.initialResourceBundle = initialResourceBundle;
70         this.rbmwStack = MapStack.create(new InternalRbmWrapper(initialResourceBundle));
71     }
72     
73     /** Puts ResourceBundle on the BOTTOM of the stack (bottom meaning will be overriden by higher layers on the stack, ie everything else already there) */
74     public void addBottomResourceBundle(ResourceBundle JavaDoc topResourceBundle) {
75         this.rbmwStack.addToBottom(new InternalRbmWrapper(topResourceBundle));
76     }
77
78     /** Puts InternalRbmWrapper on the BOTTOM of the stack (bottom meaning will be overriden by higher layers on the stack, ie everything else already there) */
79     public void addBottomResourceBundle(InternalRbmWrapper topInternalRbmWrapper) {
80         this.rbmwStack.addToBottom(topInternalRbmWrapper);
81     }
82
83     /** Don't pass the locale to make sure it has the same locale as the base */
84     public void addBottomResourceBundle(String JavaDoc resource) {
85         if (this.initialResourceBundle == null) {
86             throw new IllegalArgumentException JavaDoc("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     /** In general we don't want to use this, better to start with the more specific ResourceBundle and add layers of common ones...
92      * Puts ResourceBundle on the top of the stack (top meaning will override lower layers on the stack)
93      */

94     public void pushResourceBundle(ResourceBundle JavaDoc topResourceBundle) {
95         this.rbmwStack.push(new InternalRbmWrapper(topResourceBundle));
96     }
97
98     public ResourceBundle JavaDoc getInitialResourceBundle() {
99         return this.initialResourceBundle;
100     }
101
102     public void clear() {
103         this.rbmwStack.clear();
104     }
105     public boolean containsKey(Object JavaDoc arg0) {
106         return this.rbmwStack.containsKey(arg0);
107     }
108     public boolean containsValue(Object JavaDoc arg0) {
109         return this.rbmwStack.containsValue(arg0);
110     }
111     public Set JavaDoc entrySet() {
112         return this.rbmwStack.entrySet();
113     }
114     public Object JavaDoc get(Object JavaDoc arg0) {
115         Object JavaDoc 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 JavaDoc keySet() {
125         return this.keySet();
126     }
127     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
128         return this.rbmwStack.put(key, value);
129     }
130     public void putAll(Map JavaDoc arg0) {
131         this.rbmwStack.putAll(arg0);
132     }
133     public Object JavaDoc remove(Object JavaDoc arg0) {
134         return this.rbmwStack.remove(arg0);
135     }
136     public int size() {
137         return this.rbmwStack.size();
138     }
139     public Collection JavaDoc values() {
140         return this.rbmwStack.values();
141     }
142     
143     public static class InternalRbmWrapper implements Map JavaDoc, Serializable JavaDoc {
144         protected ResourceBundle JavaDoc resourceBundle;
145         protected Map JavaDoc topLevelMap;
146         
147         public InternalRbmWrapper(ResourceBundle JavaDoc resourceBundle) {
148             if (resourceBundle == null) {
149                 throw new IllegalArgumentException JavaDoc("Cannot create InternalRbmWrapper with a null ResourceBundle.");
150             }
151             this.resourceBundle = resourceBundle;
152             topLevelMap = new HashMap JavaDoc();
153             // NOTE: this does NOT return all keys, ie keys from parent ResourceBundles, so we keep the resourceBundle object to look at when the main Map doesn't have a certain value
154
if (resourceBundle != null) {
155                 Enumeration JavaDoc keyNum = resourceBundle.getKeys();
156                 while (keyNum.hasMoreElements()) {
157                     String JavaDoc key = (String JavaDoc) keyNum.nextElement();
158                     //resourceBundleMap.put(key, bundle.getObject(key));
159
Object JavaDoc value = resourceBundle.getObject(key);
160                     topLevelMap.put(key, value);
161                 }
162             }
163             topLevelMap.put("_RESOURCE_BUNDLE_", resourceBundle);
164         }
165         
166         /* (non-Javadoc)
167          * @see java.util.Map#size()
168          */

169         public int size() {
170             // this is an approximate size, won't include elements from parent bundles
171
return topLevelMap.size() - 1;
172         }
173     
174         /* (non-Javadoc)
175          * @see java.util.Map#isEmpty()
176          */

177         public boolean isEmpty() {
178             return topLevelMap.isEmpty();
179         }
180     
181         /* (non-Javadoc)
182          * @see java.util.Map#containsKey(java.lang.Object)
183          */

184         public boolean containsKey(Object JavaDoc arg0) {
185             if (topLevelMap.containsKey(arg0)) {
186                 return true;
187             } else {
188                 try {
189                     if (this.resourceBundle.getObject((String JavaDoc) arg0) != null) {
190                         return true;
191                     }
192                 } catch (MissingResourceException JavaDoc e) {
193                     // nope, not found... nothing, will automatically return false below
194
}
195             }
196             return false;
197         }
198     
199         /* (non-Javadoc)
200          * @see java.util.Map#containsValue(java.lang.Object)
201          */

202         public boolean containsValue(Object JavaDoc arg0) {
203             throw new RuntimeException JavaDoc("Not implemented for ResourceBundleMapWrapper");
204         }
205     
206         /* (non-Javadoc)
207          * @see java.util.Map#get(java.lang.Object)
208          */

209         public Object JavaDoc get(Object JavaDoc arg0) {
210             Object JavaDoc value = this.topLevelMap.get(arg0);
211             if (resourceBundle != null) {
212                 if (value == null) {
213                     try {
214                         value = this.resourceBundle.getObject((String JavaDoc) arg0);
215                     } catch(MissingResourceException JavaDoc mre) {
216                         // do nothing, this will be handled by recognition that the value is still null
217
}
218                 }
219                 if (value == null) {
220                     try {
221                         value = this.resourceBundle.getString((String JavaDoc) arg0);
222                     } catch(MissingResourceException JavaDoc mre) {
223                         // do nothing, this will be handled by recognition that the value is still null
224
}
225                 }
226             }
227             /* we used to do this here, but now we'll do it in the top-level class since doing it here would prevent searching down the stack
228             if (value == null) {
229                 value = arg0;
230             }
231             */

232             return value;
233         }
234     
235         /* (non-Javadoc)
236          * @see java.util.Map#put(java.lang.Object, java.lang.Object)
237          */

238         public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
239             throw new RuntimeException JavaDoc("Not implemented/allowed for ResourceBundleMapWrapper");
240         }
241     
242         /* (non-Javadoc)
243          * @see java.util.Map#remove(java.lang.Object)
244          */

245         public Object JavaDoc remove(Object JavaDoc arg0) {
246             throw new RuntimeException JavaDoc("Not implemented for ResourceBundleMapWrapper");
247         }
248     
249         /* (non-Javadoc)
250          * @see java.util.Map#putAll(java.util.Map)
251          */

252         public void putAll(Map JavaDoc arg0) {
253             throw new RuntimeException JavaDoc("Not implemented for ResourceBundleMapWrapper");
254         }
255     
256         /* (non-Javadoc)
257          * @see java.util.Map#clear()
258          */

259         public void clear() {
260             throw new RuntimeException JavaDoc("Not implemented for ResourceBundleMapWrapper");
261         }
262     
263         /* (non-Javadoc)
264          * @see java.util.Map#keySet()
265          */

266         public Set JavaDoc keySet() {
267             return this.topLevelMap.keySet();
268         }
269     
270         /* (non-Javadoc)
271          * @see java.util.Map#values()
272          */

273         public Collection JavaDoc values() {
274             return this.topLevelMap.values();
275         }
276     
277         /* (non-Javadoc)
278          * @see java.util.Map#entrySet()
279          */

280         public Set JavaDoc entrySet() {
281             return this.topLevelMap.entrySet();
282         }
283         
284         public ResourceBundle JavaDoc getResourceBundle() {
285             return this.resourceBundle;
286         }
287         
288         /*public String toString() {
289             return this.topLevelMap.toString();
290         }*/

291     }
292 }
293
Popular Tags