2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 116   Methods: 9
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MessageFormatter.java 66.7% 90.9% 100% 89.2%
coverage coverage
 1    // Copyright 2004, 2005 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;
 18    import java.util.MissingResourceException;
 19    import java.util.ResourceBundle;
 20   
 21    import org.apache.commons.logging.Log;
 22    import org.apache.commons.logging.LogFactory;
 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 Log _log;
 33   
 34    private ResourceBundle _bundle;
 35   
 36  13 public MessageFormatter(Log log, ResourceBundle bundle)
 37    {
 38  13 _log = log;
 39  13 _bundle = bundle;
 40    }
 41   
 42    /**
 43    * Assumes that the bundle name is the same as the reference class, with "Messages" stripped
 44    * off, and "Strings" appended.
 45    *
 46    * @since 1.1
 47    */
 48  11 public MessageFormatter(Class referenceClass)
 49    {
 50  11 this(referenceClass, getStringsName(referenceClass));
 51    }
 52   
 53  13 public MessageFormatter(Class referenceClass, String name)
 54    {
 55  13 this(LogFactory.getLog(referenceClass), referenceClass, name);
 56    }
 57   
 58  13 public MessageFormatter(Log log, Class referenceClass, String name)
 59    {
 60  13 this(log, getResourceBundleName(referenceClass, name));
 61    }
 62   
 63  13 public MessageFormatter(Log log, String bundleName)
 64    {
 65  13 this(log, ResourceBundle.getBundle(bundleName));
 66    }
 67   
 68  297 protected String findMessage(String key)
 69    {
 70  297 try
 71    {
 72  297 return _bundle.getString(key);
 73    }
 74    catch (MissingResourceException ex)
 75    {
 76  0 _log.error("Missing resource key: " + key + ".");
 77  0 return null;
 78    }
 79    }
 80   
 81  241 protected Locale getLocale()
 82    {
 83  241 return Locale.getDefault();
 84    }
 85   
 86  11 private static String getStringsName(Class referenceClass)
 87    {
 88  11 String className = referenceClass.getName();
 89   
 90  11 int lastDotIndex = className.lastIndexOf('.');
 91   
 92  11 String justClass = className.substring(lastDotIndex + 1);
 93   
 94  11 int mpos = justClass.indexOf("Messages");
 95   
 96  11 return justClass.substring(0, mpos) + "Strings";
 97    }
 98   
 99  13 private static String getResourceBundleName(Class referenceClass, String name)
 100    {
 101  13 String packageName = null;
 102  13 if (referenceClass.getPackage() != null)
 103    {
 104  12 packageName = referenceClass.getPackage().getName();
 105    }
 106    else
 107    {
 108  1 final int lastDotIndex = referenceClass.getName().lastIndexOf('.');
 109  1 packageName = (lastDotIndex == -1 ? "" : referenceClass.getName().substring(
 110    0,
 111    lastDotIndex));
 112   
 113    }
 114  13 return packageName.equals("") ? name : packageName + "." + name;
 115    }
 116    }