KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > Messages


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/Messages.java,v 1.8 2004/07/28 09:34:29 ib Exp $
3  * $Revision: 1.8 $
4  * $Date: 2004/07/28 09:34:29 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util;
25
26 import java.text.*;
27 import java.util.*;
28 import org.apache.slide.common.Domain;
29
30 /**
31  * Messages resource bundle manager.
32  *
33  * @version $Revision: 1.8 $ $Date: 2004/07/28 09:34:29 $
34  */

35 public class Messages {
36     
37     
38     // -------------------------------------------------------------- Constants
39

40     
41     /**
42      * Resource filename.
43      */

44     public static final String JavaDoc ResourceName =
45         "org.apache.slide.util.resources.messages";
46     
47     
48     // -------------------------------------------------------------- Variables
49

50     
51     /**
52      * Resource bundle.
53      */

54     private static ResourceBundle _messages;
55     
56     
57     /**
58      * Formats.
59      */

60     private static Hashtable _formats;
61     
62     
63     // --------------------------------------------------------- Public Methods
64

65     
66     /**
67      * Format a message.
68      *
69      * @param message Message to format
70      * @param arg1 Argument 1
71      */

72     public static String JavaDoc format(String JavaDoc message, Object JavaDoc arg1) {
73         return format( message, new Object JavaDoc[] { arg1 } );
74     }
75     
76     
77     /**
78      * Format a message.
79      *
80      * @param message Message to format
81      * @param arg1 Argument 1
82      * @param arg2 Argument 2
83      */

84     public static String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2) {
85         return format( message, new Object JavaDoc[] { arg1, arg2 } );
86     }
87     
88     
89     /**
90      * Format a message.
91      *
92      * @param message Message to format
93      * @param arg1 Argument 1
94      * @param arg2 Argument 2
95      * @param arg3 Argument 3
96      */

97     public static String JavaDoc format(String JavaDoc message, Object JavaDoc arg1, Object JavaDoc arg2,
98                                 Object JavaDoc arg3) {
99         return format( message, new Object JavaDoc[] { arg1, arg2, arg3 } );
100     }
101     
102     
103     /**
104      * Format a message.
105      *
106      * @param message Message to format
107      * @param args Argument array
108      */

109     public static String JavaDoc format( String JavaDoc message, Object JavaDoc[] args )
110     {
111         MessageFormat mf;
112         String JavaDoc msg;
113         
114         try {
115             mf = (MessageFormat) _formats.get( message );
116             if (mf == null) {
117                 try {
118                     msg = _messages.getString( message );
119                 } catch (MissingResourceException except) {
120                     return message;
121                 }
122                 mf = new MessageFormat(msg);
123                 _formats.put(message, mf);
124             }
125             return mf.format(args);
126         } catch (Exception JavaDoc except) {
127             return "An internal error occured while processing message "
128                 + message;
129         }
130     }
131     
132     
133     /**
134      * Fetch a message from the resource bundle.
135      *
136      * @param message Id of the message if the bundle
137      */

138     public static String JavaDoc message(String JavaDoc message) {
139         try {
140             return _messages.getString(message);
141         } catch (MissingResourceException except) {
142             return message;
143         }
144     }
145     
146     
147     /**
148      * Set current locale.
149      *
150      * @param locale Current locale
151      */

152     public static void setLocale(Locale locale) {
153         _formats = new Hashtable();
154         try {
155             if (locale == null) {
156                 _messages = ResourceBundle.getBundle(ResourceName);
157             } else {
158                 _messages = ResourceBundle.getBundle(ResourceName, locale);
159             }
160         } catch (Exception JavaDoc except) {
161             _messages = new EmptyResourceBundle();
162             Domain.error("Failed to locate messages resource " + ResourceName);
163         }
164     }
165     
166     
167     // ------------------------------------------------------------ Initializer
168

169     
170     static {
171         setLocale(Locale.getDefault());
172     }
173     
174     
175     // ---------------------------------------- EmptyResourceBundle Inner Class
176

177     
178     static class EmptyResourceBundle
179         extends ResourceBundle
180         implements Enumeration {
181         
182         public Enumeration getKeys() {
183             return this;
184         }
185         
186         protected Object JavaDoc handleGetObject(String JavaDoc name) {
187             return "[Missing message " + name + "]";
188         }
189         
190         public boolean hasMoreElements() {
191             return false;
192         }
193         
194         public Object JavaDoc nextElement() {
195             return null;
196         }
197         
198     }
199     
200 }
201
Popular Tags