1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts.faces.util;
18
19
20 import java.util.Collection;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.apache.struts.util.MessageResources;
26
27
28 /***
29 * <p>A limited immutable <code>Map</code> implementation that wraps the
30 * <code>MessageResources</code> instance for the specified
31 * <code>Locale</code>. Exposing the messages as a <code>Map</code>
32 * makes them easily accessible via value binding expressions, as
33 * well as JSP 2.0 expression language expressions.
34 */
35
36 public class MessagesMap implements Map {
37
38
39
40
41
42 /***
43 * <p>Construct a new {@link MessagesMap} instance that wraps the
44 * specified <code>MessageResources</code> instance, and returns messages
45 * for the specified <code>Locale</code>.</p>
46 *
47 * @param messages <code>MessageResources</code> instance to wrap
48 * @param locale <code>Locale</code> for which to retrieve messages,
49 * or <code>null</code> for the system default <code>Locale</code>
50 *
51 * @exception NullPointerException if <code>messages</code>
52 * is <code>null</code>
53 */
54 public MessagesMap(MessageResources messages, Locale locale) {
55
56 super();
57 if (messages == null) {
58 throw new NullPointerException();
59 }
60 this.messages = messages;
61 this.locale = locale;
62
63 }
64
65
66
67
68
69 /***
70 * <p>The <code>Locale</code> for which to return messages, or
71 * <code>null</code> for the system default <code>Locale</code>.</p>
72 */
73 private Locale locale = null;
74
75
76 /***
77 * <p>The <code>MessageResources</code> being wrapped by this
78 * {@link MessagesMap}.</p>
79 */
80 private MessageResources messages = null;
81
82
83
84
85
86 /***
87 * <p>The <code>clear()</code> method is not supported.</p>
88 */
89 public void clear() {
90
91 throw new UnsupportedOperationException();
92
93 }
94
95
96 /***
97 * <p>Return <code>true</code> if there is a message for the
98 * specified key.</p>
99 *
100 * @param key Message key to evaluate
101 */
102 public boolean containsKey(Object key) {
103
104 if (key == null) {
105 return (false);
106 } else {
107 return (messages.isPresent(locale, key.toString()));
108 }
109
110 }
111
112
113 /***
114 * <p>The <code>containsValue()</code> method is not supported.</p>
115 *
116 * @param value Value to evaluate
117 */
118 public boolean containsValue(Object value) {
119
120 throw new UnsupportedOperationException();
121
122 }
123
124
125 /***
126 * <p>The <code>entrySet()</code> method is not supported.</p>
127 */
128 public Set entrySet() {
129
130 throw new UnsupportedOperationException();
131
132 }
133
134
135 /***
136 * <p>The <code>equals</code> method checks whether equal
137 * <code>MessageResources</code> and <code>Locale</code> are
138 * being wrapped.</p>
139 *
140 * @param o The object to be compared
141 */
142 public boolean equals(Object o) {
143
144 if (!(o instanceof MessagesMap)) {
145 return (false);
146 }
147 MessagesMap other = (MessagesMap) o;
148 if (!messages.equals(other.getMessages())) {
149 return (false);
150 }
151 if (locale == null) {
152 return (other.getLocale() == null);
153 } else {
154 return (locale.equals(other.getLocale()));
155 }
156
157 }
158
159
160 /***
161 * <p>Return the message string for the specified key.</p>
162 *
163 * @param key Key for message to return
164 */
165 public Object get(Object key) {
166
167 if (key == null) {
168 return ("??????");
169 } else {
170 return (messages.getMessage(locale, key.toString()));
171 }
172
173 }
174
175
176 /***
177 * <p>The <code>hashCode()</code> method returns values that will
178 * be identical if the <code>equals</code> method returns <code>true</code>.
179 * </p>
180 */
181 public int hashCode() {
182
183 int value = messages.hashCode();
184 if (locale != null) {
185 value = value ^ locale.hashCode();
186 }
187 return (value);
188
189 }
190
191
192 /***
193 * <p>The <code>isEmpty()</code> method returns <code>false</code>, on the
194 * assumption that there is always at least one message available.</p>
195 */
196 public boolean isEmpty() {
197
198 return (false);
199
200 }
201
202
203 /***
204 * <p>The <code>keySet()</code> method is not supported.</p>
205 */
206 public Set keySet() {
207
208 throw new UnsupportedOperationException();
209
210 }
211
212
213 /***
214 * <p>The <code>put()</code> method is not supported.</p>
215 *
216 * @param key Key to store
217 * @param value Value to store
218 */
219 public Object put(Object key, Object value) {
220
221 throw new UnsupportedOperationException();
222
223 }
224
225
226 /***
227 * <p>The <code>putAll()</code> method is not supported.</p>
228 *
229 * @param map Keys and values to store
230 */
231 public void putAll(Map map) {
232
233 throw new UnsupportedOperationException();
234
235 }
236
237
238 /***
239 * <p>The <code>remove()</code> method is not supported.</p>
240 *
241 * @param key Key to remove
242 */
243 public Object remove(Object key) {
244
245 throw new UnsupportedOperationException();
246
247 }
248
249
250 /***
251 * <p>The <code>size()</code> method is not supported.</p>
252 */
253 public int size() {
254
255 throw new UnsupportedOperationException();
256
257 }
258
259
260 /***
261 * <p>The <code>values()</code> method is not supported.</p>
262 */
263 public Collection values() {
264
265 throw new UnsupportedOperationException();
266
267 }
268
269
270
271
272
273 /***
274 * <p>Return the <code>Locale</code> we object we are wrapping.</p>
275 */
276 Locale getLocale() {
277
278 return (this.locale);
279
280 }
281
282
283 /***
284 * <p>Return the <code>MessageResources</code> object we are wrapping.</p>
285 */
286 MessageResources getMessages() {
287
288 return (this.messages);
289
290 }
291
292
293
294 }