1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.eclipse.webapp.internal.ui;
22
23 import java.text.MessageFormat;
24 import java.util.MissingResourceException;
25 import java.util.ResourceBundle;
26
27 /**
28 * Helper class to format text messages from the Cactus property resource
29 * bundle.
30 *
31 * @version $Id: WebappMessages.java 238816 2004-02-29 16:36:46Z vmassol $
32 */
33 public final class WebappMessages
34 {
35 /**
36 * Name and location of property resource bundle on disk.
37 */
38 private static final String BUNDLE_NAME =
39 "org.apache.cactus.eclipse.webapp.internal.ui.WebappMessages";
40
41 /**
42 * The resource bundle object were Cactus messages are stored.
43 */
44 private static final ResourceBundle RESOURCE_BUNDLE =
45 ResourceBundle.getBundle(BUNDLE_NAME);
46
47 /**
48 * Prevent this class from being instantiated. It contains only static
49 * methods.
50 */
51 private WebappMessages()
52 {
53 }
54
55 /**
56 * Gets a string from the resource bundle and formats it with one argument.
57 *
58 * @param theKey the string used to get the bundle value, must not be null
59 * @param theArg the object to use when constructing the message
60 * @return the formatted string
61 */
62 public static String getFormattedString(final String theKey,
63 final Object theArg)
64 {
65 return MessageFormat.format(getString(theKey),
66 new Object[] {theArg});
67 }
68
69 /**
70 * Gets a string from the resource bundle and formats it with arguments.
71 *
72 * @param theKey the string used to get the bundle value, must not be null
73 * @param theArgs the objects to use when constructing the message
74 * @return the formatted string
75 */
76 public static String getFormattedString(final String theKey,
77 final Object[] theArgs)
78 {
79 return MessageFormat.format(getString(theKey), theArgs);
80 }
81
82 /**
83 * Gets an unformatted string from the resource bundle.
84 *
85 * @param theKey the string used to get the bundle value, must not be null
86 * @return the string from the resource bundle or "![key name]!" if the key
87 * does not exist in the resource bundle
88 */
89 public static String getString(final String theKey)
90 {
91 try
92 {
93 return RESOURCE_BUNDLE.getString(theKey);
94 }
95 catch (MissingResourceException e)
96 {
97 return '!' + theKey + '!';
98 }
99 }
100 }