KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > MessageFormatter


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.util.Locale JavaDoc;
18 import java.util.MissingResourceException JavaDoc;
19 import java.util.ResourceBundle JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.hivemind.util.Defense;
23
24 /**
25  * A wrapper around {@link java.util.ResourceBundle} that makes it easier to access and format
26  * messages.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class MessageFormatter extends AbstractMessages
31 {
32     private final ResourceBundle JavaDoc _bundle;
33
34     public MessageFormatter(ResourceBundle JavaDoc bundle)
35     {
36         Defense.notNull(bundle, "bundle");
37         _bundle = bundle;
38     }
39
40     /**
41      * @deprecated in 1.2, to be removed in a later release. Use
42      * {@link #MessageFormatter(ResourceBundle)} instead.
43      */

44     public MessageFormatter(Log log, ResourceBundle JavaDoc bundle)
45     {
46         this(bundle);
47     }
48
49     /**
50      * Assumes that the bundle name is the same as the reference class, with "Messages" stripped
51      * off, and "Strings" appended.
52      *
53      * @since 1.1
54      */

55     public MessageFormatter(Class JavaDoc referenceClass)
56     {
57         this(referenceClass, getStringsName(referenceClass));
58     }
59
60     public MessageFormatter(Class JavaDoc referenceClass, String JavaDoc name)
61     {
62         this(getResourceBundleName(referenceClass, name));
63     }
64
65     /**
66      * @deprecated in 1.2, to be removed in a later release. Use
67      * {@link #MessageFormatter(Class, String)} instead.
68      */

69     public MessageFormatter(Log log, Class JavaDoc referenceClass, String JavaDoc name)
70     {
71         this(referenceClass, name);
72     }
73
74     public MessageFormatter(String JavaDoc bundleName)
75     {
76         this(ResourceBundle.getBundle(bundleName));
77     }
78
79     /**
80      * @deprecated in 1.2, to be removed in a later release. Use {@link #MessageFormatter(String)}
81      * instead.
82      */

83     public MessageFormatter(Log log, String JavaDoc bundleName)
84     {
85         this(bundleName);
86     }
87
88     protected String JavaDoc findMessage(String JavaDoc key)
89     {
90         try
91         {
92             return _bundle.getString(key);
93         }
94         catch (MissingResourceException JavaDoc ex)
95         {
96             return null;
97         }
98     }
99
100     protected Locale JavaDoc getLocale()
101     {
102         return Locale.getDefault();
103     }
104
105     private static String JavaDoc getStringsName(Class JavaDoc referenceClass)
106     {
107         String JavaDoc className = referenceClass.getName();
108
109         int lastDotIndex = className.lastIndexOf('.');
110
111         String JavaDoc justClass = className.substring(lastDotIndex + 1);
112
113         int mpos = justClass.indexOf("Messages");
114
115         return justClass.substring(0, mpos) + "Strings";
116     }
117
118     private static String JavaDoc getResourceBundleName(Class JavaDoc referenceClass, String JavaDoc name)
119     {
120         String JavaDoc packageName = null;
121         if (referenceClass.getPackage() != null)
122         {
123             packageName = referenceClass.getPackage().getName();
124         }
125         else
126         {
127             final int lastDotIndex = referenceClass.getName().lastIndexOf('.');
128             packageName = (lastDotIndex == -1 ? "" : referenceClass.getName().substring(
129                     0,
130                     lastDotIndex));
131
132         }
133         return packageName.equals("") ? name : packageName + "." + name;
134     }
135 }
Popular Tags