Coverage Report - de.jollyday.util.ResourceUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceUtil
81%
26/32
75%
9/12
2,1
 
 1  
 /**
 2  
  * Copyright 2010 Sven Diedrichsen 
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); 
 5  
  * you may not use this file except in compliance with the License. 
 6  
  * You may obtain a copy of the License at 
 7  
  * 
 8  
  * http://www.apache.org/licenses/LICENSE-2.0 
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software 
 11  
  * distributed under the License is distributed on an 
 12  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 13  
  * express or implied. See the License for the specific language 
 14  
  * governing permissions and limitations under the License. 
 15  
  */
 16  
 package de.jollyday.util;
 17  
 
 18  
 import java.net.URL;
 19  
 import java.util.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.Locale;
 23  
 import java.util.Map;
 24  
 import java.util.ResourceBundle;
 25  
 import java.util.Set;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * ResourceUtil class.
 30  
  * </p>
 31  
  * 
 32  
  * @author Sven
 33  
  * @version $Id: $
 34  
  */
 35  5017
 public class ResourceUtil {
 36  
 
 37  
         /**
 38  
          * Property prefix for country descriptions.
 39  
          */
 40  
         private static final String COUNTRY_PROPERTY_PREFIX = "country.description";
 41  
         /**
 42  
          * Property prefix for holiday descriptions.
 43  
          */
 44  
         private static final String HOLIDAY_PROPERTY_PREFIX = "holiday.description";
 45  
         /**
 46  
          * The prefix of the country description file.
 47  
          */
 48  
         private static final String COUNTRY_DESCRIPTIONS_FILE_PREFIX = "descriptions.country_descriptions";
 49  
         /**
 50  
          * The prefix of the holiday descriptions file.
 51  
          */
 52  
         private static final String HOLIDAY_DESCRIPTIONS_FILE_PREFIX = "descriptions.holiday_descriptions";
 53  
         /**
 54  
          * Unknown constant will be returned when there is no description
 55  
          * configured.
 56  
          */
 57  
         public static final String UNDEFINED = "undefined";
 58  
         /**
 59  
          * Cache for the holiday descriptions resource bundles.
 60  
          */
 61  1
         private static final Map<Locale, ResourceBundle> HOLIDAY_DESCRIPTION_CACHE = new HashMap<>();
 62  
         /**
 63  
          * Cache for the country descriptions resource bundles.
 64  
          */
 65  1
         private final static Map<Locale, ResourceBundle> COUNTRY_DESCRIPTIONS_CACHE = new HashMap<>();
 66  
         /**
 67  
          * Util class to provide the correct classloader.
 68  
          */
 69  5021
         private transient ClassLoadingUtil classLoadingUtil = new ClassLoadingUtil();
 70  
 
 71  
         /**
 72  
          * The description read with the default locale.
 73  
          * 
 74  
          * @return holiday description using default locale.
 75  
          * @param key
 76  
          *            a {@link java.lang.String} object.
 77  
          */
 78  
         public String getHolidayDescription(String key) {
 79  0
                 return getHolidayDescription(Locale.getDefault(), key);
 80  
         }
 81  
 
 82  
         /**
 83  
          * The description read with the provided locale.
 84  
          * 
 85  
          * @param locale
 86  
          *            a {@link java.util.Locale} object.
 87  
          * @return holiday description using the provided locale.
 88  
          * @param key
 89  
          *            a {@link java.lang.String} object.
 90  
          */
 91  
         public String getHolidayDescription(Locale locale, String key) {
 92  1293
                 return getDescription(HOLIDAY_PROPERTY_PREFIX + "." + key, getHolidayDescriptions(locale));
 93  
         }
 94  
 
 95  
         /**
 96  
          * <p>
 97  
          * getCountryDescription.
 98  
          * </p>
 99  
          * 
 100  
          * @return the description
 101  
          * @param key
 102  
          *            a {@link java.lang.String} object.
 103  
          */
 104  
         public String getCountryDescription(String key) {
 105  0
                 return getCountryDescription(Locale.getDefault(), key);
 106  
         }
 107  
 
 108  
         /**
 109  
          * Returns the hierarchys description text from the resource bundle.
 110  
          * 
 111  
          * @param l
 112  
          *            Locale to return the description text for.
 113  
          * @return Description text
 114  
          * @param key
 115  
          *            a {@link java.lang.String} object.
 116  
          */
 117  
         public String getCountryDescription(Locale l, String key) {
 118  673
                 if (key != null) {
 119  673
                         return getDescription(COUNTRY_PROPERTY_PREFIX + "." + key.toLowerCase(), getCountryDescriptions(l));
 120  
                 }
 121  0
                 return ResourceUtil.UNDEFINED;
 122  
         }
 123  
 
 124  
         /**
 125  
          * Returns a list of ISO codes.
 126  
          * 
 127  
          * @return 2-digit ISO codes.
 128  
          */
 129  
         public Set<String> getISOCodes() {
 130  3
                 Set<String> codes = new HashSet<>();
 131  3
                 ResourceBundle countryDescriptions = getCountryDescriptions(Locale.getDefault());
 132  3
                 for (String property : Collections.list(countryDescriptions.getKeys())) {
 133  1596
                         String[] split = property.split("\\.");
 134  1596
                         if (split != null && split.length > 2) {
 135  1596
                                 codes.add(split[2].toLowerCase());
 136  
                         }
 137  1596
                 }
 138  3
                 return codes;
 139  
         }
 140  
 
 141  
         /**
 142  
          * Returns the description from the resource bundle if the key is contained.
 143  
          * It will return 'undefined' otherwise.
 144  
          * 
 145  
          * @param key
 146  
          * @param bundle
 147  
          * @return description
 148  
          */
 149  
         private String getDescription(String key, final ResourceBundle bundle) {
 150  1966
                 if (!Collections.list(bundle.getKeys()).contains(key)) {
 151  1345
                         return UNDEFINED;
 152  
                 }
 153  621
                 return bundle.getString(key);
 154  
         }
 155  
 
 156  
         /**
 157  
          * Returns the eventually cached ResourceBundle for the holiday
 158  
          * descriptions.
 159  
          * 
 160  
          * @param l
 161  
          *            Locale to retrieve the descriptions for.
 162  
          * @return ResourceBundle containing the descriptions for the locale.
 163  
          */
 164  
         private ResourceBundle getHolidayDescriptions(Locale l) {
 165  1293
                 return getResourceBundle(l, HOLIDAY_DESCRIPTION_CACHE, HOLIDAY_DESCRIPTIONS_FILE_PREFIX);
 166  
         }
 167  
 
 168  
         /**
 169  
          * Returns the eventually cached ResourceBundle for the holiday
 170  
          * descriptions.
 171  
          * 
 172  
          * @param l
 173  
          *            Locale to retrieve the descriptions for.
 174  
          * @return ResourceBundle containing the descriptions for the locale.
 175  
          */
 176  
         private ResourceBundle getCountryDescriptions(Locale l) {
 177  676
                 return getResourceBundle(l, COUNTRY_DESCRIPTIONS_CACHE, COUNTRY_DESCRIPTIONS_FILE_PREFIX);
 178  
         }
 179  
 
 180  
         /**
 181  
          * Returns the eventually cached ResourceBundle for the descriptions.
 182  
          * 
 183  
          * @param l
 184  
          *            Locale to retrieve the descriptions for.
 185  
          * @return ResourceBundle containing the descriptions for the locale.
 186  
          */
 187  
         private ResourceBundle getResourceBundle(Locale l, Map<Locale, ResourceBundle> resourceCache, String filePrefix) {
 188  1969
                 synchronized (resourceCache) {
 189  1969
                         if (!resourceCache.containsKey(l)) {
 190  9
                                 ResourceBundle bundle = ResourceBundle.getBundle(filePrefix, l, classLoadingUtil.getClassloader());
 191  9
                                 resourceCache.put(l, bundle);
 192  
                         }
 193  1969
                         return resourceCache.get(l);
 194  0
                 }
 195  
         }
 196  
 
 197  
         /**
 198  
          * Returns the resource by URL.
 199  
          * 
 200  
          * @param resourceName
 201  
          *            the name/path of the resource to load
 202  
          * @return the URL to the resource
 203  
          */
 204  
         public URL getResource(String resourceName) {
 205  
                 try {
 206  1338
                         return classLoadingUtil.getClassloader().getResource(resourceName);
 207  0
                 } catch (Exception e) {
 208  0
                         throw new IllegalStateException("Cannot load resource: " + resourceName, e);
 209  
                 }
 210  
         }
 211  
 
 212  
 }