001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.impl;
016
017 import java.util.Locale;
018 import java.util.MissingResourceException;
019 import java.util.ResourceBundle;
020
021 import org.apache.commons.logging.Log;
022 import org.apache.commons.logging.LogFactory;
023
024 /**
025 * A wrapper around {@link java.util.ResourceBundle} that makes it easier to access and format
026 * messages.
027 *
028 * @author Howard Lewis Ship
029 */
030 public class MessageFormatter extends AbstractMessages
031 {
032 private Log _log;
033
034 private ResourceBundle _bundle;
035
036 public MessageFormatter(Log log, ResourceBundle bundle)
037 {
038 _log = log;
039 _bundle = bundle;
040 }
041
042 /**
043 * Assumes that the bundle name is the same as the reference class, with "Messages" stripped
044 * off, and "Strings" appended.
045 *
046 * @since 1.1
047 */
048 public MessageFormatter(Class referenceClass)
049 {
050 this(referenceClass, getStringsName(referenceClass));
051 }
052
053 public MessageFormatter(Class referenceClass, String name)
054 {
055 this(LogFactory.getLog(referenceClass), referenceClass, name);
056 }
057
058 public MessageFormatter(Log log, Class referenceClass, String name)
059 {
060 this(log, getResourceBundleName(referenceClass, name));
061 }
062
063 public MessageFormatter(Log log, String bundleName)
064 {
065 this(log, ResourceBundle.getBundle(bundleName));
066 }
067
068 protected String findMessage(String key)
069 {
070 try
071 {
072 return _bundle.getString(key);
073 }
074 catch (MissingResourceException ex)
075 {
076 _log.error("Missing resource key: " + key + ".");
077 return null;
078 }
079 }
080
081 protected Locale getLocale()
082 {
083 return Locale.getDefault();
084 }
085
086 private static String getStringsName(Class referenceClass)
087 {
088 String className = referenceClass.getName();
089
090 int lastDotIndex = className.lastIndexOf('.');
091
092 String justClass = className.substring(lastDotIndex + 1);
093
094 int mpos = justClass.indexOf("Messages");
095
096 return justClass.substring(0, mpos) + "Strings";
097 }
098
099 private static String getResourceBundleName(Class referenceClass, String name)
100 {
101 String packageName = null;
102 if (referenceClass.getPackage() != null)
103 {
104 packageName = referenceClass.getPackage().getName();
105 }
106 else
107 {
108 final int lastDotIndex = referenceClass.getName().lastIndexOf('.');
109 packageName = (lastDotIndex == -1 ? "" : referenceClass.getName().substring(
110 0,
111 lastDotIndex));
112
113 }
114 return packageName.equals("") ? name : packageName + "." + name;
115 }
116 }