001    // Copyright 2004 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.hivemind.util.Defense;
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 final ResourceBundle _bundle;
033    
034        public MessageFormatter(ResourceBundle bundle)
035        {
036            Defense.notNull(bundle, "bundle");
037            _bundle = bundle;
038        }
039    
040        /**
041         * @deprecated in 1.2, to be removed in a later release. Use
042         *             {@link #MessageFormatter(ResourceBundle)} instead.
043         */
044        public MessageFormatter(Log log, ResourceBundle bundle)
045        {
046            this(bundle);
047        }
048    
049        /**
050         * Assumes that the bundle name is the same as the reference class, with "Messages" stripped
051         * off, and "Strings" appended.
052         * 
053         * @since 1.1
054         */
055        public MessageFormatter(Class referenceClass)
056        {
057            this(referenceClass, getStringsName(referenceClass));
058        }
059    
060        public MessageFormatter(Class referenceClass, String name)
061        {
062            this(getResourceBundleName(referenceClass, name));
063        }
064    
065        /**
066         * @deprecated in 1.2, to be removed in a later release. Use
067         *             {@link #MessageFormatter(Class, String)} instead.
068         */
069        public MessageFormatter(Log log, Class referenceClass, String name)
070        {
071            this(referenceClass, name);
072        }
073    
074        public MessageFormatter(String bundleName)
075        {
076            this(ResourceBundle.getBundle(bundleName));
077        }
078    
079        /**
080         * @deprecated in 1.2, to be removed in a later release. Use {@link #MessageFormatter(String)}
081         *             instead.
082         */
083        public MessageFormatter(Log log, String bundleName)
084        {
085            this(bundleName);
086        }
087    
088        protected String findMessage(String key)
089        {
090            try
091            {
092                return _bundle.getString(key);
093            }
094            catch (MissingResourceException ex)
095            {
096                return null;
097            }
098        }
099    
100        protected Locale getLocale()
101        {
102            return Locale.getDefault();
103        }
104    
105        private static String getStringsName(Class referenceClass)
106        {
107            String className = referenceClass.getName();
108    
109            int lastDotIndex = className.lastIndexOf('.');
110    
111            String 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 getResourceBundleName(Class referenceClass, String name)
119        {
120            String 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    }