001: /*
002: * $Id: MessagesMap.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.util;
023:
024: import java.util.Collection;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import org.apache.struts.util.MessageResources;
030:
031: /**
032: * <p>A limited immutable <code>Map</code> implementation that wraps the
033: * <code>MessageResources</code> instance for the specified
034: * <code>Locale</code>. Exposing the messages as a <code>Map</code>
035: * makes them easily accessible via value binding expressions, as
036: * well as JSP 2.0 expression language expressions.
037: */
038:
039: public class MessagesMap implements Map {
040:
041: // ------------------------------------------------------------ Constructors
042:
043: /**
044: * <p>Construct a new {@link MessagesMap} instance that wraps the
045: * specified <code>MessageResources</code> instance, and returns messages
046: * for the specified <code>Locale</code>.</p>
047: *
048: * @param messages <code>MessageResources</code> instance to wrap
049: * @param locale <code>Locale</code> for which to retrieve messages,
050: * or <code>null</code> for the system default <code>Locale</code>
051: *
052: * @exception NullPointerException if <code>messages</code>
053: * is <code>null</code>
054: */
055: public MessagesMap(MessageResources messages, Locale locale) {
056:
057: super ();
058: if (messages == null) {
059: throw new NullPointerException();
060: }
061: this .messages = messages;
062: this .locale = locale;
063:
064: }
065:
066: // ------------------------------------------------------ Instance Variables
067:
068: /**
069: * <p>The <code>Locale</code> for which to return messages, or
070: * <code>null</code> for the system default <code>Locale</code>.</p>
071: */
072: private Locale locale = null;
073:
074: /**
075: * <p>The <code>MessageResources</code> being wrapped by this
076: * {@link MessagesMap}.</p>
077: */
078: private MessageResources messages = null;
079:
080: // ---------------------------------------------------------- Public Methods
081:
082: /**
083: * <p>The <code>clear()</code> method is not supported.</p>
084: */
085: public void clear() {
086:
087: throw new UnsupportedOperationException();
088:
089: }
090:
091: /**
092: * <p>Return <code>true</code> if there is a message for the
093: * specified key.</p>
094: *
095: * @param key Message key to evaluate
096: */
097: public boolean containsKey(Object key) {
098:
099: if (key == null) {
100: return (false);
101: } else {
102: return (messages.isPresent(locale, key.toString()));
103: }
104:
105: }
106:
107: /**
108: * <p>The <code>containsValue()</code> method is not supported.</p>
109: *
110: * @param value Value to evaluate
111: */
112: public boolean containsValue(Object value) {
113:
114: throw new UnsupportedOperationException();
115:
116: }
117:
118: /**
119: * <p>The <code>entrySet()</code> method is not supported.</p>
120: */
121: public Set entrySet() {
122:
123: throw new UnsupportedOperationException();
124:
125: }
126:
127: /**
128: * <p>The <code>equals</code> method checks whether equal
129: * <code>MessageResources</code> and <code>Locale</code> are
130: * being wrapped.</p>
131: *
132: * @param o The object to be compared
133: */
134: public boolean equals(Object o) {
135:
136: if (!(o instanceof MessagesMap)) {
137: return (false);
138: }
139: MessagesMap other = (MessagesMap) o;
140: if (!messages.equals(other.getMessages())) {
141: return (false);
142: }
143: if (locale == null) {
144: return (other.getLocale() == null);
145: } else {
146: return (locale.equals(other.getLocale()));
147: }
148:
149: }
150:
151: /**
152: * <p>Return the message string for the specified key.</p>
153: *
154: * @param key Key for message to return
155: */
156: public Object get(Object key) {
157:
158: if (key == null) {
159: return ("??????");
160: } else {
161: return (messages.getMessage(locale, key.toString()));
162: }
163:
164: }
165:
166: /**
167: * <p>The <code>hashCode()</code> method returns values that will
168: * be identical if the <code>equals</code> method returns <code>true</code>.
169: * </p>
170: */
171: public int hashCode() {
172:
173: int value = messages.hashCode();
174: if (locale != null) {
175: value = value ^ locale.hashCode();
176: }
177: return (value);
178:
179: }
180:
181: /**
182: * <p>The <code>isEmpty()</code> method returns <code>false</code>, on the
183: * assumption that there is always at least one message available.</p>
184: */
185: public boolean isEmpty() {
186:
187: return (false);
188:
189: }
190:
191: /**
192: * <p>The <code>keySet()</code> method is not supported.</p>
193: */
194: public Set keySet() {
195:
196: throw new UnsupportedOperationException();
197:
198: }
199:
200: /**
201: * <p>The <code>put()</code> method is not supported.</p>
202: *
203: * @param key Key to store
204: * @param value Value to store
205: */
206: public Object put(Object key, Object value) {
207:
208: throw new UnsupportedOperationException();
209:
210: }
211:
212: /**
213: * <p>The <code>putAll()</code> method is not supported.</p>
214: *
215: * @param map Keys and values to store
216: */
217: public void putAll(Map map) {
218:
219: throw new UnsupportedOperationException();
220:
221: }
222:
223: /**
224: * <p>The <code>remove()</code> method is not supported.</p>
225: *
226: * @param key Key to remove
227: */
228: public Object remove(Object key) {
229:
230: throw new UnsupportedOperationException();
231:
232: }
233:
234: /**
235: * <p>The <code>size()</code> method is not supported.</p>
236: */
237: public int size() {
238:
239: throw new UnsupportedOperationException();
240:
241: }
242:
243: /**
244: * <p>The <code>values()</code> method is not supported.</p>
245: */
246: public Collection values() {
247:
248: throw new UnsupportedOperationException();
249:
250: }
251:
252: // --------------------------------------------------------- Package Methods
253:
254: /**
255: * <p>Return the <code>Locale</code> we object we are wrapping.</p>
256: */
257: Locale getLocale() {
258:
259: return (this .locale);
260:
261: }
262:
263: /**
264: * <p>Return the <code>MessageResources</code> object we are wrapping.</p>
265: */
266: MessageResources getMessages() {
267:
268: return (this.messages);
269:
270: }
271:
272: }
|