Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 141   Methods: 10
NCLOC: 85   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
MessageFormatter.java 80% 79.3% 100% 83.7%
coverage coverage
 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.text.MessageFormat;
 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   
 import org.apache.hivemind.HiveMind;
 24   
 
 25   
 /**
 26   
  * A wrapper around {@link java.util.ResourceBundle} that makes
 27   
  * it easier to access and format messages.
 28   
  *
 29   
  * @author Howard Lewis Ship
 30   
  */
 31   
 public class MessageFormatter
 32   
 {
 33   
     private Log _log;
 34   
     private ResourceBundle _bundle;
 35   
 
 36  11
     public MessageFormatter(Log log, ResourceBundle bundle)
 37   
     {
 38  11
         _log = log;
 39  11
         _bundle = bundle;
 40   
     }
 41   
 
 42  11
     public MessageFormatter(Class referenceClass, String name)
 43   
     {
 44  11
         this(LogFactory.getLog(referenceClass), referenceClass, name);
 45   
     }
 46   
 
 47  11
     public MessageFormatter(Log log, Class referenceClass, String name)
 48   
     {
 49  11
         this(log, referenceClass.getPackage().getName() + "." + name);
 50   
     }
 51   
 
 52  11
     public MessageFormatter(Log log, String bundleName)
 53   
     {
 54  11
         this(log, ResourceBundle.getBundle(bundleName));
 55   
     }
 56   
 
 57  186
     public String getMessage(String key)
 58   
     {
 59  186
         try
 60   
         {
 61  186
             return _bundle.getString(key);
 62   
         }
 63   
         catch (MissingResourceException ex)
 64   
         {
 65  0
             _log.error("Missing resource key: " + key + ".");
 66  0
             return "[" + key.toUpperCase() + "]";
 67   
         }
 68   
     }
 69   
 
 70  36
     public String format(String key, Object arg)
 71   
     {
 72  36
         return format(key, new Object[] { arg });
 73   
     }
 74   
 
 75  81
     public String format(String key, Object arg1, Object arg2)
 76   
     {
 77  81
         return format(key, new Object[] { arg1, arg2 });
 78   
     }
 79   
 
 80  20
     public String format(String key, Object arg1, Object arg2, Object arg3)
 81   
     {
 82  20
         return format(key, new Object[] { arg1, arg2, arg3 });
 83   
     }
 84   
 
 85   
     /**
 86   
      * Formats a message using the key to obtain a pattern, and passing the arguments.
 87   
      * 
 88   
      * <p>
 89   
      * It is common to pass an exception instance as an arg.  Those are treated specially:
 90   
      * The exception instance is replaced with its message {@link Throwable#getMessage()}. If the
 91   
      * message is blank (null or empty), then the exception's class name is used.
 92   
      */
 93  152
     public String format(String key, Object[] args)
 94   
     {
 95  152
         String pattern = getMessage(key);
 96   
 
 97  152
         if (args == null)
 98  0
             return pattern;
 99   
 
 100  152
         for (int i = 0; i < args.length; i++)
 101   
         {
 102  311
             if (args[i] instanceof Throwable)
 103   
             {
 104  42
                 Throwable t = (Throwable) args[i];
 105   
 
 106  42
                 args[i] = extractMessage(t);
 107   
             }
 108   
         }
 109   
 
 110  152
         try
 111   
         {
 112  152
             return MessageFormat.format(pattern, args);
 113   
         }
 114   
         catch (Exception ex)
 115   
         {
 116  0
             _log.error("Unable to format message: \"" + pattern + "\" from key " + key + ".", ex);
 117   
 
 118  0
             return null;
 119   
         }
 120   
     }
 121   
 
 122   
     /**
 123   
      * Extracts the message from an exception. If the message is null, the the class name
 124   
      * of the exception is returned.
 125   
      * 
 126   
      */
 127  42
     private String extractMessage(Throwable t)
 128   
     {
 129  42
         if (t == null)
 130  0
             return null;
 131   
 
 132  42
         String message = t.getMessage();
 133   
 
 134  42
         if (HiveMind.isBlank(message))
 135  4
             return t.getClass().getName();
 136   
 
 137  38
         return message;
 138   
     }
 139   
 
 140   
 }
 141