KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > util > Messages


1 /*
2  * Copyright 2002-2005 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 package org.apache.commons.vfs.util;
17
18 import java.text.MessageFormat JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 /**
25  * Formats messages.
26  *
27  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
28  */

29 public class Messages
30 {
31     /**
32      * Map from message code to MessageFormat object for the message.
33      */

34     private static Map JavaDoc messages = new HashMap JavaDoc();
35     private static ResourceBundle JavaDoc resources;
36
37     private Messages()
38     {
39     }
40
41     /**
42      * Formats a message.
43      *
44      * @param code The message code.
45      * @return The formatted message.
46      */

47     public static String JavaDoc getString(final String JavaDoc code)
48     {
49         return getString(code, new Object JavaDoc[0]);
50     }
51
52     /**
53      * Formats a message.
54      *
55      * @param code The message code.
56      * @param param The message parameter.
57      * @return The formatted message.
58      */

59     public static String JavaDoc getString(final String JavaDoc code, final Object JavaDoc param)
60     {
61         return getString(code, new Object JavaDoc[]{param});
62     }
63
64     /**
65      * Formats a message.
66      *
67      * @param code The message code.
68      * @param params The message parameters.
69      * @return The formatted message.
70      */

71     public static String JavaDoc getString(final String JavaDoc code, final Object JavaDoc[] params)
72     {
73         try
74         {
75             if (code == null)
76             {
77                 return null;
78             }
79
80             final MessageFormat JavaDoc msg = findMessage(code);
81             return msg.format(params);
82         }
83         catch (final MissingResourceException JavaDoc mre)
84         {
85             return "Unknown message with code \"" + code + "\".";
86         }
87     }
88
89     /**
90      * Locates a message by its code.
91      */

92     private static synchronized MessageFormat JavaDoc findMessage(final String JavaDoc code)
93         throws MissingResourceException JavaDoc
94     {
95         // Check if the message is cached
96
MessageFormat JavaDoc msg = (MessageFormat JavaDoc) messages.get(code);
97         if (msg != null)
98         {
99             return msg;
100         }
101
102         // Locate the message
103
if (resources == null)
104         {
105             resources = ResourceBundle.getBundle("org.apache.commons.vfs.Resources");
106         }
107         final String JavaDoc msgText = resources.getString(code);
108         msg = new MessageFormat JavaDoc(msgText);
109         messages.put(code, msg);
110         return msg;
111     }
112 }
113
Popular Tags