001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.i18n;
021:
022: import java.text.MessageFormat;
023: import java.util.Locale;
024: import java.util.MissingResourceException;
025: import java.util.ResourceBundle;
026:
027: /**
028: * Accept parameters for ProjectResourceBundle,
029: * but defer object instantiation (and therefore
030: * resource bundle loading) until required.
031: */
032: public class MessageBundle {
033: private boolean loaded = false;
034:
035: private ProjectResourceBundle _resourceBundle = null;
036:
037: private final String projectName;
038: private final String packageName;
039: private final String resourceName;
040: private final Locale locale;
041: private final ClassLoader classLoader;
042: private final ResourceBundle parent;
043:
044: public final ProjectResourceBundle getResourceBundle() {
045: if (!loaded) {
046: _resourceBundle = ProjectResourceBundle.getBundle(
047: projectName, packageName, resourceName, locale,
048: classLoader, parent);
049: loaded = true;
050: }
051: return _resourceBundle;
052: }
053:
054: /**
055: * Construct a new ExtendMessages
056: */
057: public MessageBundle(String projectName, String packageName,
058: String resourceName, Locale locale,
059: ClassLoader classLoader, ResourceBundle parent)
060: throws MissingResourceException {
061: this .projectName = projectName;
062: this .packageName = packageName;
063: this .resourceName = resourceName;
064: this .locale = locale;
065: this .classLoader = classLoader;
066: this .parent = parent;
067: }
068:
069: /**
070: * Gets a string message from the resource bundle for the given key
071: *
072: * @param key The resource key
073: * @return The message
074: */
075: public String getMessage(String key)
076: throws MissingResourceException {
077: return getMessage(key, (String[]) null);
078: }
079:
080: /**
081: * <p>Gets a string message from the resource bundle for the given key. The
082: * message may contain variables that will be substituted with the given
083: * arguments. Variables have the format:</p>
084: * <dir>
085: * This message has two variables: {0} and {1}
086: * </dir>
087: *
088: * @param key The resource key
089: * @param arg0 The argument to place in variable {0}
090: * @return The message
091: */
092: public String getMessage(String key, String arg0)
093: throws MissingResourceException {
094: return getMessage(key, new String[] { arg0 });
095: }
096:
097: /**
098: * <p>Gets a string message from the resource bundle for the given key. The
099: * message may contain variables that will be substituted with the given
100: * arguments. Variables have the format:</p>
101: * <dir>
102: * This message has two variables: {0} and {1}
103: * </dir>
104: *
105: * @param key The resource key
106: * @param arg0 The argument to place in variable {0}
107: * @param arg1 The argument to place in variable {1}
108: * @return The message
109: */
110: public String getMessage(String key, String arg0, String arg1)
111: throws MissingResourceException {
112: return getMessage(key, new String[] { arg0, arg1 });
113: }
114:
115: /**
116: * <p>Gets a string message from the resource bundle for the given key. The
117: * message may contain variables that will be substituted with the given
118: * arguments. Variables have the format:</p>
119: * <dir>
120: * This message has two variables: {0} and {1}
121: * </dir>
122: *
123: * @param key The resource key
124: * @param arg0 The argument to place in variable {0}
125: * @param arg1 The argument to place in variable {1}
126: * @param arg2 The argument to place in variable {2}
127: * @return The message
128: */
129: public String getMessage(String key, String arg0, String arg1,
130: String arg2) throws MissingResourceException {
131: return getMessage(key, new String[] { arg0, arg1, arg2 });
132: }
133:
134: /**
135: * <p>Gets a string message from the resource bundle for the given key. The
136: * message may contain variables that will be substituted with the given
137: * arguments. Variables have the format:</p>
138: * <dir>
139: * This message has two variables: {0} and {1}
140: * </dir>
141: *
142: * @param key The resource key
143: * @param arg0 The argument to place in variable {0}
144: * @param arg1 The argument to place in variable {1}
145: * @param arg2 The argument to place in variable {2}
146: * @param arg3 The argument to place in variable {3}
147: * @return The message
148: */
149: public String getMessage(String key, String arg0, String arg1,
150: String arg2, String arg3) throws MissingResourceException {
151: return getMessage(key, new String[] { arg0, arg1, arg2, arg3 });
152: }
153:
154: /**
155: * <p>Gets a string message from the resource bundle for the given key. The
156: * message may contain variables that will be substituted with the given
157: * arguments. Variables have the format:</p>
158: * <dir>
159: * This message has two variables: {0} and {1}
160: * </dir>
161: *
162: * @param key The resource key
163: * @param arg0 The argument to place in variable {0}
164: * @param arg1 The argument to place in variable {1}
165: * @param arg2 The argument to place in variable {2}
166: * @param arg3 The argument to place in variable {3}
167: * @param arg4 The argument to place in variable {4}
168: * @return The message
169: */
170: public String getMessage(String key, String arg0, String arg1,
171: String arg2, String arg3, String arg4)
172: throws MissingResourceException {
173: return getMessage(key, new String[] { arg0, arg1, arg2, arg3,
174: arg4 });
175: }
176:
177: /**
178: * <p>Gets a string message from the resource bundle for the given key. The
179: * message may contain variables that will be substituted with the given
180: * arguments. Variables have the format:</p>
181: * <dir>
182: * This message has two variables: {0} and {1}
183: * </dir>
184: *
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 getMessage(String key, String[] array)
190: throws MissingResourceException {
191: String msg = null;
192: if (getResourceBundle() != null) {
193: msg = getResourceBundle().getString(key);
194: }
195:
196: if (msg == null) {
197: throw new MissingResourceException(
198: "Cannot find resource key \"" + key
199: + "\" in base name "
200: + getResourceBundle().getResourceName(),
201: getResourceBundle().getResourceName(), key);
202: }
203:
204: return MessageFormat.format(msg, array);
205: }
206: }
|