KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > validator > Messages


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.validator;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 /**
27  *
28  *
29  * @version $Revision: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  */

31 public class Messages {
32     static private Hashtable JavaDoc bundles = new Hashtable JavaDoc();
33     static private Hashtable JavaDoc rbFormats = new Hashtable JavaDoc();
34     static private Locale JavaDoc globalLocale;
35
36     private ResourceBundle JavaDoc messages;
37     private Hashtable JavaDoc formats;
38     private Locale JavaDoc locale;
39     private String JavaDoc resourceName;
40
41     public Messages(String JavaDoc resourceName) {
42         synchronized (Messages.class) {
43             locale = globalLocale;
44             this.resourceName = resourceName + ".Messages";
45
46             ResourceBundle JavaDoc rb = (ResourceBundle JavaDoc) bundles.get(this.resourceName);
47             if (rb == null) {
48                 init(); // TODO Remove lazy call to init
49
} else {
50                 messages = rb;
51                 formats = (Hashtable JavaDoc) rbFormats.get(this.resourceName);
52             }
53         }
54
55     }
56
57     protected void init() {
58         try {
59             if (locale == null)
60                 messages = ResourceBundle.getBundle(resourceName);
61             else
62                 messages = ResourceBundle.getBundle(resourceName, locale);
63         } catch (Exception JavaDoc except) {
64             messages = new EmptyResourceBundle();
65         }
66
67         formats = new Hashtable JavaDoc();
68
69         bundles.put(resourceName, messages);
70         rbFormats.put(resourceName, formats);
71     }
72
73     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1) {
74         return format(message, new Object JavaDoc[] { arg1 });
75     }
76
77     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2) {
78         return format(message, new Object JavaDoc[] { arg1, arg2 });
79     }
80
81     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
82         return format(message, new Object JavaDoc[] { arg1, arg2, arg3 });
83     }
84
85     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4) {
86         return format(message, new Object JavaDoc[] { arg1, arg2, arg3, arg4 });
87     }
88
89     public String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, Object JavaDoc arg4, Object JavaDoc arg5) {
90         return format(message, new Object JavaDoc[] { arg1, arg2, arg3, arg4, arg5 });
91     }
92
93     public String JavaDoc format(String JavaDoc message) {
94         return message(message);
95     }
96
97     public String JavaDoc format(String JavaDoc message, Object JavaDoc[] args) {
98         if (locale != globalLocale) {
99             synchronized (Messages.class) {
100                 init(); // TODO Remove lazy call to init
101
}
102         }
103
104         MessageFormat JavaDoc mf;
105         String JavaDoc msg;
106
107         try {
108             mf = (MessageFormat JavaDoc) formats.get(message);
109             if (mf == null) {
110                 try {
111                     msg = messages.getString(message);
112                 } catch (MissingResourceException JavaDoc except) {
113                     return message;
114                 }
115                 mf = new MessageFormat JavaDoc(msg);
116                 formats.put(message, mf);
117             }
118             return mf.format(args);
119         } catch (Exception JavaDoc except) {
120             return "An internal error occured while processing message " + message;
121         }
122     }
123
124     public String JavaDoc message(String JavaDoc message) {
125         if (locale != globalLocale) {
126             synchronized (Messages.class) {
127                 init();
128             }
129         }
130
131         try {
132             return messages.getString(message);
133         } catch (MissingResourceException JavaDoc except) {
134             return message;
135         }
136     }
137
138     static public void setLocale(Locale JavaDoc locale) {
139         synchronized (Messages.class) {
140             globalLocale = locale;
141             bundles = new Hashtable JavaDoc();
142             rbFormats = new Hashtable JavaDoc();
143         }
144     }
145
146     static {
147         setLocale(Locale.getDefault());
148     }
149
150     private static final class EmptyResourceBundle extends ResourceBundle JavaDoc implements Enumeration JavaDoc {
151
152         public Enumeration JavaDoc getKeys() {
153             return this;
154         }
155
156         protected Object JavaDoc handleGetObject(String JavaDoc name) {
157             return "[Missing message " + name + "]";
158         }
159
160         public boolean hasMoreElements() {
161             return false;
162         }
163
164         public Object JavaDoc nextElement() {
165             return null;
166         }
167
168     }
169
170 }
171
Popular Tags