Coverage report

  %line %branch
nl.toolforge.karma.core.ErrorCode
70% 
89% 

 1  
 /*
 2  
 Karma core - Core of the Karma application
 3  
 Copyright (C) 2004  Toolforge <www.toolforge.nl>
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, write to the Free Software
 17  
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 */
 19  
 package nl.toolforge.karma.core;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 import java.text.MessageFormat;
 25  
 import java.util.Locale;
 26  
 import java.util.MissingResourceException;
 27  
 import java.util.ResourceBundle;
 28  
 
 29  
 /**
 30  
  * <p>Class representing a Karma errorcode. These errorcodes are localized to support different languages. Errorcodes
 31  
  * are defined in ranges. Exceptions that are supported by Karma, define these ranges.</p>
 32  
  *
 33  
  * @author D.A. Smedes
 34  
  * @version $Id: ErrorCode.java,v 1.17 2004/11/10 22:06:33 asmedes Exp $
 35  
  */
 36  
 public final class ErrorCode {
 37  
 
 38  932
   private static Log logger = LogFactory.getLog(ErrorCode.class);
 39  
 
 40  466
   private static Locale currentLocale = null;
 41  
 
 42  6237
   private Object[] messageArguments = new Object[]{};
 43  
 
 44  6237
   private ResourceBundle messageBundle = null;
 45  
 
 46  
   static {
 47  
 
 48  
     // todo : ensure that LocalEnvironment knows about the current Locale and pass it on to here.
 49  
     //
 50  466
     currentLocale = Locale.ENGLISH;
 51  466
   }
 52  
 
 53  6237
   private String errorCode = null;
 54  
 
 55  
   /**
 56  
    * Creates an error code. Error codes must comply to the following pattern : <code>[A-Z]{3}-\d{5}</code>. Examples are:
 57  
    * <code>MAN-00001</code>, <code>CMD-10020</code>.
 58  
    *
 59  
    * @param errorCode
 60  
    */
 61  6237
   public ErrorCode(String errorCode) {
 62  
 
 63  6237
     if (!errorCode.matches("[A-Z]{3}-\\d{5}")) {
 64  52
       throw new IllegalArgumentException("Illegal error code format.");
 65  
     }
 66  6185
     this.errorCode = errorCode;
 67  6185
   }
 68  
 
 69  
   public void setMessageBundle(ResourceBundle messageBundle) {
 70  52
     this.messageBundle = messageBundle;
 71  52
   }
 72  
 
 73  
   /**
 74  
    * Assigns message arguments to this error code as per the <code>MessageFormat</code> definition.
 75  
    *
 76  
    * @param messageArguments An Object array (currently only <code>String</code> instances are supported).
 77  
    */
 78  
   public final void setMessageArguments(Object[] messageArguments) {
 79  271
     this.messageArguments = messageArguments;
 80  271
   }
 81  
 
 82  
   /**
 83  
    * <p>Gets a localized error message for the <code>ErrorCode</code> instance. Error messages are defined in a
 84  
    * <code>error-messages-&lt;locale&gt;.properties</code> (e.g. <code>error-messages-NL.properties</code>). A message
 85  
    * text is identified by a key <code>message.</code> concatenated with {@link #getErrorCodeString}.
 86  
    * <p/>
 87  
    * </p>When no resource bundle can be found for <code>locale</code>, the default locale <code>Locale.ENGLISH</code> is
 88  
    * used.
 89  
    *
 90  
    * @param locale A locale object (e.g. representing the current locale of the user environment).
 91  
    * @return A localized error message or {@link #getErrorCodeString} when no message was found for this errorcode or the
 92  
    *   resourcebundle could not be found for <code>locale</code>.
 93  
    */
 94  
   public String getErrorMessage(Locale locale) {
 95  
 
 96  538
     if (locale == null) {
 97  0
       locale = currentLocale;
 98  
     }
 99  
 
 100  538
     String message = "";
 101  
 
 102  538
     if (messageBundle == null) {
 103  
       try {
 104  183
         locale = Locale.ENGLISH;
 105  183
         messageBundle = ResourceBundle.getBundle("error-messages", locale);
 106  0
       } catch (MissingResourceException m) {
 107  0
         logger.error("No default resource bundle available for locale " + locale);
 108  0
         return getErrorCodeString();
 109  183
       }
 110  
     }
 111  
 
 112  
     try {
 113  538
       message = messageBundle.getString("message." + getErrorCodeString());
 114  
 
 115  538
       if (getMessageArguments().length != 0) {
 116  271
         MessageFormat messageFormat = new MessageFormat(message);
 117  271
         message = messageFormat.format(getMessageArguments());
 118  
       }
 119  
 
 120  0
     } catch (RuntimeException r) {
 121  0
       logger.error("No message found for errorcode : " + getErrorCodeString());
 122  0
       message = getErrorCodeString();
 123  538
     }
 124  
 
 125  538
     if (message.startsWith(getErrorCodeString())) {
 126  0
       return message;
 127  
     }
 128  
 
 129  538
     return getErrorCodeString() + " : " + message;
 130  
   }
 131  
 
 132  
   /**
 133  
    * Gets the error message for the current locale.
 134  
    *
 135  
    * @return The error message for the error code.
 136  
    */
 137  
   public String getErrorMessage() {
 138  538
     return getErrorMessage(currentLocale);
 139  
   }
 140  
 
 141  
   /**
 142  
    * Gets this instance' error code.
 143  
    *
 144  
    * @return This instance' error code.
 145  
    */
 146  
   public String getErrorCodeString() {
 147  1640
     return errorCode;
 148  
   }
 149  
 
 150  
   private Object[] getMessageArguments() {
 151  809
     return (messageArguments == null ? new Object[0] : messageArguments);
 152  
   }
 153  
 
 154  
   public boolean equals(Object o) {
 155  69
     if (this == o) return true;
 156  0
     if (!(o instanceof ErrorCode)) return false;
 157  
 
 158  0
     final ErrorCode errorCode1 = (ErrorCode) o;
 159  
 
 160  0
     if (errorCode != null ? !errorCode.equals(errorCode1.errorCode) : errorCode1.errorCode != class="keyword">null) return false;
 161  
 
 162  0
     return true;
 163  
   }
 164  
 
 165  
   public int hashCode() {
 166  0
     return (errorCode != null ? errorCode.hashCode() : 0);
 167  
   }
 168  
 
 169  
   public String toString() {
 170  0
     return errorCode;
 171  
   }
 172  
 
 173  
 }
 174  
 

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.