KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.i18n;
57
58 import java.text.MessageFormat JavaDoc;
59 import java.util.Locale JavaDoc;
60 import java.util.MissingResourceException JavaDoc;
61 import java.util.ResourceBundle JavaDoc;
62
63 /**
64  * Accept parameters for ProjectResourceBundle,
65  * but defer object instantiation (and therefore
66  * resource bundle loading) until required.
67  *
68  * @author Richard A. Sitze (rsitze@us.ibm.com)
69  * @author Karl Moss (kmoss@macromedia.com)
70  * @author Glen Daniels (gdaniels@macromedia.com)
71  */

72 public class MessageBundle
73 {
74    private boolean loaded = false;
75
76    private ProjectResourceBundle _resourceBundle = null;
77
78    private final String JavaDoc projectName;
79    private final String JavaDoc packageName;
80    private final String JavaDoc resourceName;
81    private final Locale JavaDoc locale;
82    private final ClassLoader JavaDoc classLoader;
83    private final ResourceBundle JavaDoc parent;
84
85
86    public final ProjectResourceBundle getResourceBundle()
87    {
88       if (!loaded)
89       {
90          _resourceBundle = ProjectResourceBundle.getBundle(projectName,
91                  packageName,
92                  resourceName,
93                  locale,
94                  classLoader,
95                  parent);
96          loaded = true;
97       }
98       return _resourceBundle;
99    }
100
101    /**
102     * Construct a new ExtendMessages
103     */

104    public MessageBundle(String JavaDoc projectName,
105                         String JavaDoc packageName,
106                         String JavaDoc resourceName,
107                         Locale JavaDoc locale,
108                         ClassLoader JavaDoc classLoader,
109                         ResourceBundle JavaDoc parent)
110            throws MissingResourceException JavaDoc
111    {
112       this.projectName = projectName;
113       this.packageName = packageName;
114       this.resourceName = resourceName;
115       this.locale = locale;
116       this.classLoader = classLoader;
117       this.parent = parent;
118    }
119
120    /**
121     * Gets a string message from the resource bundle for the given key
122     *
123     * @param key The resource key
124     * @return The message
125     */

126    public String JavaDoc getMessage(String JavaDoc key) throws MissingResourceException JavaDoc
127    {
128       return getMessage(key, (String JavaDoc[])null);
129    }
130
131    /**
132     * <p>Gets a string message from the resource bundle for the given key. The
133     * message may contain variables that will be substituted with the given
134     * arguments. Variables have the format:</p>
135     * <dir>
136     * This message has two variables: {0} and {1}
137     * </dir>
138     *
139     * @param key The resource key
140     * @param arg0 The argument to place in variable {0}
141     * @return The message
142     */

143    public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0) throws MissingResourceException JavaDoc
144    {
145       return getMessage(key, new String JavaDoc[]{arg0});
146    }
147
148    /**
149     * <p>Gets a string message from the resource bundle for the given key. The
150     * message may contain variables that will be substituted with the given
151     * arguments. Variables have the format:</p>
152     * <dir>
153     * This message has two variables: {0} and {1}
154     * </dir>
155     *
156     * @param key The resource key
157     * @param arg0 The argument to place in variable {0}
158     * @param arg1 The argument to place in variable {1}
159     * @return The message
160     */

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

180    public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2) throws MissingResourceException JavaDoc
181    {
182       return getMessage(key, new String JavaDoc[]{arg0, arg1, arg2});
183    }
184
185    /**
186     * <p>Gets a string message from the resource bundle for the given key. The
187     * message may contain variables that will be substituted with the given
188     * arguments. Variables have the format:</p>
189     * <dir>
190     * This message has two variables: {0} and {1}
191     * </dir>
192     *
193     * @param key The resource key
194     * @param arg0 The argument to place in variable {0}
195     * @param arg1 The argument to place in variable {1}
196     * @param arg2 The argument to place in variable {2}
197     * @param arg3 The argument to place in variable {3}
198     * @return The message
199     */

200    public String JavaDoc getMessage(String JavaDoc key, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) throws MissingResourceException JavaDoc
201    {
202       return getMessage(key, new String JavaDoc[]{arg0, arg1, arg2, arg3});
203    }
204
205    /**
206     * <p>Gets a string message from the resource bundle for the given key. The
207     * message may contain variables that will be substituted with the given
208     * arguments. Variables have the format:</p>
209     * <dir>
210     * This message has two variables: {0} and {1}
211     * </dir>
212     *
213     * @param key The resource key
214     * @param arg0 The argument to place in variable {0}
215     * @param arg1 The argument to place in variable {1}
216     * @param arg2 The argument to place in variable {2}
217     * @param arg3 The argument to place in variable {3}
218     * @param arg4 The argument to place in variable {4}
219     * @return The message
220     */

221    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
222    {
223       return getMessage(key, new String JavaDoc[]{arg0, arg1, arg2, arg3, arg4});
224    }
225
226    /**
227     * <p>Gets a string message from the resource bundle for the given key. The
228     * message may contain variables that will be substituted with the given
229     * arguments. Variables have the format:</p>
230     * <dir>
231     * This message has two variables: {0} and {1}
232     * </dir>
233     *
234     * @param key The resource key
235     * @param array An array of objects to place in corresponding variables
236     * @return The message
237     */

238    public String JavaDoc getMessage(String JavaDoc key, String JavaDoc[] array) throws MissingResourceException JavaDoc
239    {
240       String JavaDoc msg = null;
241       if (getResourceBundle() != null)
242       {
243          msg = getResourceBundle().getString(key);
244       }
245
246       if (msg == null)
247       {
248          throw new MissingResourceException JavaDoc("Cannot find resource key \"" + key +
249                  "\" in base name " +
250                  getResourceBundle().getResourceName(),
251                  getResourceBundle().getResourceName(), key);
252       }
253
254       return MessageFormat.format(msg, array);
255    }
256 }
257
Popular Tags