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 */
017 package org.apache.geronimo.validator;
018
019 import java.text.MessageFormat;
020 import java.util.Enumeration;
021 import java.util.Hashtable;
022 import java.util.Locale;
023 import java.util.MissingResourceException;
024 import java.util.ResourceBundle;
025
026 /**
027 *
028 *
029 * @version $Revision: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
030 */
031 public class Messages {
032 static private Hashtable bundles = new Hashtable();
033 static private Hashtable rbFormats = new Hashtable();
034 static private Locale globalLocale;
035
036 private ResourceBundle messages;
037 private Hashtable formats;
038 private Locale locale;
039 private String resourceName;
040
041 public Messages(String resourceName) {
042 synchronized (Messages.class) {
043 locale = globalLocale;
044 this.resourceName = resourceName + ".Messages";
045
046 ResourceBundle rb = (ResourceBundle) bundles.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, locale);
063 } catch (Exception except) {
064 messages = new EmptyResourceBundle();
065 }
066
067 formats = new Hashtable();
068
069 bundles.put(resourceName, messages);
070 rbFormats.put(resourceName, formats);
071 }
072
073 public String format(String message, Object arg1) {
074 return format(message, new Object[] { arg1 });
075 }
076
077 public String format(String message, Object arg1, Object arg2) {
078 return format(message, new Object[] { arg1, arg2 });
079 }
080
081 public String format(String message, Object arg1, Object arg2, Object arg3) {
082 return format(message, new Object[] { arg1, arg2, arg3 });
083 }
084
085 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4) {
086 return format(message, new Object[] { arg1, arg2, arg3, arg4 });
087 }
088
089 public String format(String message, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
090 return format(message, new Object[] { arg1, arg2, arg3, arg4, arg5 });
091 }
092
093 public String format(String message) {
094 return message(message);
095 }
096
097 public String format(String message, Object[] args) {
098 if (locale != globalLocale) {
099 synchronized (Messages.class) {
100 init(); // TODO Remove lazy call to init
101 }
102 }
103
104 MessageFormat mf;
105 String msg;
106
107 try {
108 mf = (MessageFormat) formats.get(message);
109 if (mf == null) {
110 try {
111 msg = messages.getString(message);
112 } catch (MissingResourceException except) {
113 return message;
114 }
115 mf = new MessageFormat(msg);
116 formats.put(message, mf);
117 }
118 return mf.format(args);
119 } catch (Exception except) {
120 return "An internal error occured while processing message " + message;
121 }
122 }
123
124 public String message(String message) {
125 if (locale != globalLocale) {
126 synchronized (Messages.class) {
127 init();
128 }
129 }
130
131 try {
132 return messages.getString(message);
133 } catch (MissingResourceException except) {
134 return message;
135 }
136 }
137
138 static public void setLocale(Locale locale) {
139 synchronized (Messages.class) {
140 globalLocale = locale;
141 bundles = new Hashtable();
142 rbFormats = new Hashtable();
143 }
144 }
145
146 static {
147 setLocale(Locale.getDefault());
148 }
149
150 private static final class EmptyResourceBundle extends ResourceBundle implements Enumeration {
151
152 public Enumeration getKeys() {
153 return this;
154 }
155
156 protected Object handleGetObject(String name) {
157 return "[Missing message " + name + "]";
158 }
159
160 public boolean hasMoreElements() {
161 return false;
162 }
163
164 public Object nextElement() {
165 return null;
166 }
167
168 }
169
170 }