Coverage Report - org.apache.commons.i18n.ResourceBundleMessageProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceBundleMessageProvider
100%
29/29
100%
8/8
4
 
 1  
 /*
 2  
 *
 3  
 * ====================================================================
 4  
 *
 5  
 * Licensed to the Apache Software Foundation (ASF) under one or more
 6  
 * contributor license agreements.  See the NOTICE file distributed with
 7  
 * this work for additional information regarding copyright ownership.
 8  
 * The ASF licenses this file to You under the Apache License, Version 2.0
 9  
 * (the "License"); you may not use this file except in compliance with
 10  
 * the License.  You may obtain a copy of the License at
 11  
 *
 12  
 *     http://www.apache.org/licenses/LICENSE-2.0
 13  
 *
 14  
 * Unless required by applicable law or agreed to in writing, software
 15  
 * distributed under the License is distributed on an "AS IS" BASIS,
 16  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17  
 * See the License for the specific language governing permissions and
 18  
 * limitations under the License.
 19  
 *
 20  
 */
 21  
 package org.apache.commons.i18n;
 22  
 
 23  
 import java.text.MessageFormat;
 24  
 import java.util.Enumeration;
 25  
 import java.util.HashMap;
 26  
 import java.util.Locale;
 27  
 import java.util.Map;
 28  
 import java.util.MissingResourceException;
 29  
 import java.util.ResourceBundle;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 
 33  
 /**
 34  
  * The <code>ResourceBundleMessageProvider</code> deals with messages defined in 
 35  
  * resource bundles. Messages defined in resource bundles can be grouped together
 36  
  * by adding the entry key at the end of the message key separated by a dot.
 37  
  */
 38  
 public class ResourceBundleMessageProvider implements MessageProvider {
 39  2
     private static Logger logger = Logger.getLogger(ResourceBundleMessageProvider.class.getName());
 40  
 
 41  
     private final String baseName;
 42  
 
 43  
     /**
 44  
      *
 45  
      * @throws MessageNotFoundException Thrown if the resource bundle does not exist.
 46  
      */
 47  9
     public ResourceBundleMessageProvider(String baseName) throws MessageNotFoundException {
 48  9
         this.baseName = baseName;
 49  
         // Test if resource exists
 50  
         try {
 51  9
             ResourceBundle.getBundle(baseName);
 52  
         }
 53  2
         catch ( MissingResourceException e ) {
 54  2
             String msg = MessageFormat.format(
 55  
                     I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.RESOURCE_BUNDLE_NOT_FOUND),
 56  
                     new String[]{baseName});
 57  2
             logger.log(Level.WARNING, msg);
 58  2
             throw new MessageNotFoundException(msg);
 59  7
         }
 60  7
     }
 61  
 
 62  
     /* (non-Javadoc)
 63  
      * @see org.apache.commons.i18n.MessageProvider#getText(java.lang.String, java.lang.String, java.util.Locale)
 64  
      */
 65  
     public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
 66  
         try {
 67  24
             ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
 68  24
              return resourceBundle.getObject(id+"."+entry).toString();
 69  
         }
 70  2
         catch ( MissingResourceException e ) {
 71  2
             logger.log(
 72  
                     Level.WARNING,
 73  
                     MessageFormat.format(
 74  
                             I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND),
 75  
                             new String[] { id }));
 76  2
             return null;
 77  
         }
 78  
     }
 79  
 
 80  
     /* (non-Javadoc)
 81  
      * @see org.apache.commons.i18n.MessageProvider#getEntries(java.lang.String, java.util.Locale)
 82  
      */
 83  
     public Map getEntries(String id, Locale locale) {
 84  4
         String messageIdentifier = id+".";
 85  4
         Map entries = null;
 86  4
         ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
 87  4
         Enumeration keys = resourceBundle.getKeys();
 88  16
         while ( keys.hasMoreElements() ) {
 89  12
             String key = (String)keys.nextElement();
 90  12
             if ( key.startsWith(messageIdentifier) ) {
 91  9
                 if ( entries == null ) {
 92  3
                     entries = new HashMap();
 93  
                 }
 94  9
                 entries.put(key.substring(messageIdentifier.length()), resourceBundle.getString(key));
 95  
             }
 96  12
         }
 97  4
         if ( entries == null ) {
 98  1
             throw new MessageNotFoundException(MessageFormat.format(
 99  
                     I18nUtils.INTERNAL_MESSAGES.getString(I18nUtils.NO_MESSAGE_ENTRIES_FOUND),
 100  
                     new String[] { id })); 
 101  
         }
 102  3
         return entries;
 103  
     }
 104  
 }