KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > i18n > MessageBundle


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

16
17 package org.apache.axis.i18n;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 /**
25  * Accept parameters for ProjectResourceBundle,
26  * but defer object instantiation (and therefore
27  * resource bundle loading) until required.
28  *
29  * @author Richard A. Sitze (rsitze@us.ibm.com)
30  * @author Karl Moss (kmoss@macromedia.com)
31  * @author Glen Daniels (gdaniels@apache.org)
32  */

33 public class MessageBundle {
34     private boolean loaded = false;
35     
36     private ProjectResourceBundle _resourceBundle = null;
37
38     private final String JavaDoc projectName;
39     private final String JavaDoc packageName;
40     private final String JavaDoc resourceName;
41     private final Locale JavaDoc locale;
42     private final ClassLoader JavaDoc classLoader;
43     private final ResourceBundle JavaDoc parent;
44
45
46     public final ProjectResourceBundle getResourceBundle() {
47         if (!loaded) {
48             _resourceBundle = ProjectResourceBundle.getBundle(projectName,
49                                                               packageName,
50                                                               resourceName,
51                                                               locale,
52                                                               classLoader,
53                                                               parent);
54             loaded = true;
55         }
56         return _resourceBundle;
57     }
58
59     /**
60      * Construct a new ExtendMessages
61      */

62     public MessageBundle(String JavaDoc projectName,
63                              String JavaDoc packageName,
64                              String JavaDoc resourceName,
65                              Locale JavaDoc locale,
66                              ClassLoader JavaDoc classLoader,
67                              ResourceBundle JavaDoc parent)
68         throws MissingResourceException JavaDoc
69     {
70         this.projectName = projectName;
71         this.packageName = packageName;
72         this.resourceName = resourceName;
73         this.locale = locale;
74         this.classLoader = classLoader;
75         this.parent = parent;
76     }
77
78     /**
79       * Gets a string message from the resource bundle for the given key
80       * @param key The resource key
81       * @return The message
82       */

83     public String JavaDoc getMessage(String JavaDoc key) throws MissingResourceException JavaDoc
84     {
85         return getMessage(key, (String JavaDoc[]) null);
86     }
87
88     /**
89       * <p>Gets a string message from the resource bundle for the given key. The
90       * message may contain variables that will be substituted with the given
91       * arguments. Variables have the format:</p>
92       * <dir>
93       * This message has two variables: {0} and {1}
94       * </dir>
95       * @param key The resource key
96       * @param arg0 The argument to place in variable {0}
97       * @return The message
98       */

99     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0) throws MissingResourceException JavaDoc
100     {
101         return getMessage(key, new String JavaDoc[] { arg0 });
102     }
103
104     /**
105       * <p>Gets a string message from the resource bundle for the given key. The
106       * message may contain variables that will be substituted with the given
107       * arguments. Variables have the format:</p>
108       * <dir>
109       * This message has two variables: {0} and {1}
110       * </dir>
111       * @param key The resource key
112       * @param arg0 The argument to place in variable {0}
113       * @param arg1 The argument to place in variable {1}
114       * @return The message
115       */

116     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1) throws MissingResourceException JavaDoc
117     {
118         return getMessage(key, new String JavaDoc[] { arg0, arg1 });
119     }
120
121     /**
122       * <p>Gets a string message from the resource bundle for the given key. The
123       * message may contain variables that will be substituted with the given
124       * arguments. Variables have the format:</p>
125       * <dir>
126       * This message has two variables: {0} and {1}
127       * </dir>
128       * @param key The resource key
129       * @param arg0 The argument to place in variable {0}
130       * @param arg1 The argument to place in variable {1}
131       * @param arg2 The argument to place in variable {2}
132       * @return The message
133       */

134     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2) throws MissingResourceException JavaDoc
135     {
136         return getMessage(key, new String JavaDoc[] { arg0, arg1, arg2 });
137     }
138
139     /**
140       * <p>Gets a string message from the resource bundle for the given key. The
141       * message may contain variables that will be substituted with the given
142       * arguments. Variables have the format:</p>
143       * <dir>
144       * This message has two variables: {0} and {1}
145       * </dir>
146       * @param key The resource key
147       * @param arg0 The argument to place in variable {0}
148       * @param arg1 The argument to place in variable {1}
149       * @param arg2 The argument to place in variable {2}
150       * @param arg3 The argument to place in variable {3}
151       * @return The message
152       */

153     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) throws MissingResourceException JavaDoc
154     {
155         return getMessage(key, new String JavaDoc[] { arg0, arg1, arg2, arg3 });
156     }
157
158     /**
159       * <p>Gets a string message from the resource bundle for the given key. The
160       * message may contain variables that will be substituted with the given
161       * arguments. Variables have the format:</p>
162       * <dir>
163       * This message has two variables: {0} and {1}
164       * </dir>
165       * @param key The resource key
166       * @param arg0 The argument to place in variable {0}
167       * @param arg1 The argument to place in variable {1}
168       * @param arg2 The argument to place in variable {2}
169       * @param arg3 The argument to place in variable {3}
170       * @param arg4 The argument to place in variable {4}
171       * @return The message
172       */

173     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3, String JavaDoc arg4) throws MissingResourceException JavaDoc
174     {
175         return getMessage(key, new String JavaDoc[] { arg0, arg1, arg2, arg3, arg4 });
176     }
177
178     /**
179       * <p>Gets a string message from the resource bundle for the given key. The
180       * message may contain variables that will be substituted with the given
181       * arguments. Variables have the format:</p>
182       * <dir>
183       * This message has two variables: {0} and {1}
184       * </dir>
185       * @param key The resource key
186       * @param array An array of objects to place in corresponding variables
187       * @return The message
188       */

189     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc[] array) throws MissingResourceException JavaDoc
190     {
191         String JavaDoc msg = null;
192         if (getResourceBundle() != null) {
193             msg = getResourceBundle().getString(key);
194         }
195
196         if (msg == null) {
197             throw new MissingResourceException JavaDoc("Cannot find resource key \"" + key +
198                                                "\" in base name " +
199                                                getResourceBundle().getResourceName(),
200                                                getResourceBundle().getResourceName(), key);
201         }
202
203         return MessageFormat.format(msg, array);
204     }
205 }
206
Popular Tags