001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.validator;
017:
018: import java.text.MessageFormat;
019: import java.util.Enumeration;
020: import java.util.Hashtable;
021: import java.util.Locale;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: /**
026: *
027: *
028: * @version $Revision: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
029: */
030: public class Messages {
031: static private Hashtable bundles = new Hashtable();
032: static private Hashtable rbFormats = new Hashtable();
033: static private Locale globalLocale;
034:
035: private ResourceBundle messages;
036: private Hashtable formats;
037: private Locale locale;
038: private String resourceName;
039:
040: public Messages(String resourceName) {
041: synchronized (Messages.class) {
042: locale = globalLocale;
043: this .resourceName = resourceName + ".Messages";
044:
045: ResourceBundle rb = (ResourceBundle) bundles
046: .get(this .resourceName);
047: if (rb == null) {
048: init(); // TODO Remove lazy call to init
049: } else {
050: messages = rb;
051: formats = (Hashtable) rbFormats.get(this .resourceName);
052: }
053: }
054:
055: }
056:
057: protected void init() {
058: try {
059: if (locale == null)
060: messages = ResourceBundle.getBundle(resourceName);
061: else
062: messages = ResourceBundle.getBundle(resourceName,
063: locale);
064: } catch (Exception except) {
065: messages = new EmptyResourceBundle();
066: }
067:
068: formats = new Hashtable();
069:
070: bundles.put(resourceName, messages);
071: rbFormats.put(resourceName, formats);
072: }
073:
074: public String format(String message, Object arg1) {
075: return format(message, new Object[] { arg1 });
076: }
077:
078: public String format(String message, Object arg1, Object arg2) {
079: return format(message, new Object[] { arg1, arg2 });
080: }
081:
082: public String format(String message, Object arg1, Object arg2,
083: Object arg3) {
084: return format(message, new Object[] { arg1, arg2, arg3 });
085: }
086:
087: public String format(String message, Object arg1, Object arg2,
088: Object arg3, Object arg4) {
089: return format(message, new Object[] { arg1, arg2, arg3, arg4 });
090: }
091:
092: public String format(String message, Object arg1, Object arg2,
093: Object arg3, Object arg4, Object arg5) {
094: return format(message, new Object[] { arg1, arg2, arg3, arg4,
095: arg5 });
096: }
097:
098: public String format(String message) {
099: return message(message);
100: }
101:
102: public String format(String message, Object[] args) {
103: if (locale != globalLocale) {
104: synchronized (Messages.class) {
105: init(); // TODO Remove lazy call to init
106: }
107: }
108:
109: MessageFormat mf;
110: String msg;
111:
112: try {
113: mf = (MessageFormat) formats.get(message);
114: if (mf == null) {
115: try {
116: msg = messages.getString(message);
117: } catch (MissingResourceException except) {
118: return message;
119: }
120: mf = new MessageFormat(msg);
121: formats.put(message, mf);
122: }
123: return mf.format(args);
124: } catch (Exception except) {
125: return "An internal error occured while processing message "
126: + message;
127: }
128: }
129:
130: public String message(String message) {
131: if (locale != globalLocale) {
132: synchronized (Messages.class) {
133: init();
134: }
135: }
136:
137: try {
138: return messages.getString(message);
139: } catch (MissingResourceException except) {
140: return message;
141: }
142: }
143:
144: static public void setLocale(Locale locale) {
145: synchronized (Messages.class) {
146: globalLocale = locale;
147: bundles = new Hashtable();
148: rbFormats = new Hashtable();
149: }
150: }
151:
152: static {
153: setLocale(Locale.getDefault());
154: }
155:
156: private static final class EmptyResourceBundle extends
157: ResourceBundle implements Enumeration {
158:
159: public Enumeration getKeys() {
160: return this ;
161: }
162:
163: protected Object handleGetObject(String name) {
164: return "[Missing message " + name + "]";
165: }
166:
167: public boolean hasMoreElements() {
168: return false;
169: }
170:
171: public Object nextElement() {
172: return null;
173: }
174:
175: }
176:
177: }
|