LoadResourceBundle.java

  1. package org.jbehave.core.steps.needle.configuration;

  2. import java.util.Enumeration;
  3. import java.util.MissingResourceException;
  4. import java.util.ResourceBundle;

  5. /**
  6.  * Null safe Resource Loader. If ResourceBundle does not exist, an empty Bundle is returned.
  7.  *
  8.  * @author Jan Galinski, Holisticon AG (jan.galinski@holisticon.de)
  9.  * @author Simon Zambrovski, Holisticon AG (simon.zambrovski@holisticon.de)
  10.  */
  11. public enum LoadResourceBundle {
  12.     INSTANCE;

  13.     public static final ResourceBundle EMPTY_RESOURCE_BUNDLE = new ResourceBundle() {

  14.         @Override
  15.         public Enumeration<String> getKeys() {
  16.             return new Enumeration<String>() {

  17.                 @Override
  18.                 public boolean hasMoreElements() {
  19.                     return false;
  20.                 }

  21.                 @Override
  22.                 public String nextElement() {
  23.                     return null;
  24.                 }
  25.             };
  26.         }

  27.         @Override
  28.         protected Object handleGetObject(final String key) {
  29.             return "";
  30.         }
  31.     };

  32.     public final ResourceBundle apply(final String resourceName) {
  33.         if (resourceName == null || "".equals(resourceName.trim())) {
  34.             throw new IllegalArgumentException("resourceName must not be null or empty!");
  35.         }

  36.         try {
  37.             return ResourceBundle.getBundle(resourceName);
  38.         } catch (final MissingResourceException e) {
  39.             return EMPTY_RESOURCE_BUNDLE;
  40.         }
  41.     }

  42. }