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.jaxws.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, but defer object instantiation (and therefore
029: * resource bundle loading) until required.
030: */
031: public class MessageBundle {
032: private boolean loaded = false;
033:
034: private ProjectResourceBundle _resourceBundle = null;
035:
036: private final String projectName;
037: private final String packageName;
038: private final String resourceName;
039: private final Locale locale;
040: private final ClassLoader classLoader;
041: private final ResourceBundle parent;
042:
043: public final ProjectResourceBundle getResourceBundle() {
044: if (!loaded) {
045: _resourceBundle = ProjectResourceBundle.getBundle(
046: projectName, packageName, resourceName, locale,
047: classLoader, parent);
048: loaded = true;
049: }
050: return _resourceBundle;
051: }
052:
053: /** Construct a new ExtendMessages */
054: public MessageBundle(String projectName, String packageName,
055: String resourceName, Locale locale,
056: ClassLoader classLoader, ResourceBundle parent)
057: throws MissingResourceException {
058: this .projectName = projectName;
059: this .packageName = packageName;
060: this .resourceName = resourceName;
061: this .locale = locale;
062: this .classLoader = classLoader;
063: this .parent = parent;
064: }
065:
066: /**
067: * Gets a string message from the resource bundle for the given key
068: *
069: * @param key The resource key
070: * @return The message
071: */
072: public String getMessage(String key)
073: throws MissingResourceException {
074: return getMessage(key, (String[]) null);
075: }
076:
077: /**
078: * <p>Gets a string message from the resource bundle for the given key. The message may contain
079: * variables that will be substituted with the given arguments. Variables have the format:</p>
080: * <dir> This message has two variables: {0} and {1} </dir>
081: *
082: * @param key The resource key
083: * @param arg0 The argument to place in variable {0}
084: * @return The message
085: */
086: public String getMessage(String key, String arg0)
087: throws MissingResourceException {
088: return getMessage(key, new String[] { arg0 });
089: }
090:
091: /**
092: * <p>Gets a string message from the resource bundle for the given key. The message may contain
093: * variables that will be substituted with the given arguments. Variables have the format:</p>
094: * <dir> This message has two variables: {0} and {1} </dir>
095: *
096: * @param key The resource key
097: * @param arg0 The argument to place in variable {0}
098: * @param arg1 The argument to place in variable {1}
099: * @return The message
100: */
101: public String getMessage(String key, String arg0, String arg1)
102: throws MissingResourceException {
103: return getMessage(key, new String[] { arg0, arg1 });
104: }
105:
106: /**
107: * <p>Gets a string message from the resource bundle for the given key. The message may contain
108: * variables that will be substituted with the given arguments. Variables have the format:</p>
109: * <dir> This message has two variables: {0} and {1} </dir>
110: *
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: * @param arg2 The argument to place in variable {2}
115: * @return The message
116: */
117: public String getMessage(String key, String arg0, String arg1,
118: String arg2) throws MissingResourceException {
119: return getMessage(key, new String[] { arg0, arg1, arg2 });
120: }
121:
122: /**
123: * <p>Gets a string message from the resource bundle for the given key. The message may contain
124: * variables that will be substituted with the given arguments. Variables have the format:</p>
125: * <dir> This message has two variables: {0} and {1} </dir>
126: *
127: * @param key The resource key
128: * @param arg0 The argument to place in variable {0}
129: * @param arg1 The argument to place in variable {1}
130: * @param arg2 The argument to place in variable {2}
131: * @param arg3 The argument to place in variable {3}
132: * @return The message
133: */
134: public String getMessage(String key, String arg0, String arg1,
135: String arg2, String arg3) throws MissingResourceException {
136: return getMessage(key, new String[] { arg0, arg1, arg2, arg3 });
137: }
138:
139: /**
140: * <p>Gets a string message from the resource bundle for the given key. The message may contain
141: * variables that will be substituted with the given arguments. Variables have the format:</p>
142: * <dir> This message has two variables: {0} and {1} </dir>
143: *
144: * @param key The resource key
145: * @param arg0 The argument to place in variable {0}
146: * @param arg1 The argument to place in variable {1}
147: * @param arg2 The argument to place in variable {2}
148: * @param arg3 The argument to place in variable {3}
149: * @param arg4 The argument to place in variable {4}
150: * @return The message
151: */
152: public String getMessage(String key, String arg0, String arg1,
153: String arg2, String arg3, String arg4)
154: throws MissingResourceException {
155: return getMessage(key, new String[] { arg0, arg1, arg2, arg3,
156: arg4 });
157: }
158:
159: /**
160: * <p>Gets a string message from the resource bundle for the given key. The message may contain
161: * variables that will be substituted with the given arguments. Variables have the format:</p>
162: * <dir> This message has two variables: {0} and {1} </dir>
163: *
164: * @param key The resource key
165: * @param array An array of objects to place in corresponding variables
166: * @return The message
167: */
168: public String getMessage(String key, String[] array)
169: throws MissingResourceException {
170: String msg = null;
171: if (getResourceBundle() != null) {
172: msg = getResourceBundle().getString(key);
173: }
174:
175: if (msg == null) {
176: throw new MissingResourceException(
177: "Cannot find resource key \"" + key
178: + "\" in base name "
179: + getResourceBundle().getResourceName(),
180: getResourceBundle().getResourceName(), key);
181: }
182:
183: return MessageFormat.format(msg, (Object[]) array);
184: }
185: }
|