ResourceBundleStringLookup.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements. See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache license, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the license for the specific language governing permissions and
  15.  * limitations under the license.
  16.  */

  17. package org.apache.commons.text.lookup;

  18. import java.util.MissingResourceException;
  19. import java.util.ResourceBundle;

  20. /**
  21.  * Looks up keys from resource bundles.
  22.  * <p>
  23.  * Looks up the value for a given key in the format "BundleName:BundleKey".
  24.  * </p>
  25.  * <p>
  26.  * For example: "com.domain.messages:MyKey".
  27.  * </p>
  28.  *
  29.  * @see ResourceBundle
  30.  * @since 1.3
  31.  */
  32. final class ResourceBundleStringLookup extends AbstractStringLookup {

  33.     /**
  34.      * Defines the singleton for this class.
  35.      */
  36.     static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup();

  37.     /**
  38.      * The name of the resource bundle from which to look something up.
  39.      */
  40.     private final String bundleName;

  41.     /**
  42.      * Constructs a blank instance.
  43.      */
  44.     private ResourceBundleStringLookup() {
  45.         this(null);
  46.     }

  47.     /**
  48.      * Constructs an instance that only works for the given bundle.
  49.      *
  50.      * @param bundleName the name of the resource bundle from which we will look keys up.
  51.      * @since 1.5
  52.      */
  53.     ResourceBundleStringLookup(final String bundleName) {
  54.         this.bundleName = bundleName;
  55.     }

  56.     ResourceBundle getBundle(final String keyBundleName) {
  57.         // The ResourceBundle class caches bundles, no need to cache here.
  58.         return ResourceBundle.getBundle(keyBundleName);
  59.     }

  60.     String getString(final String keyBundleName, final String bundleKey) {
  61.         return getBundle(keyBundleName).getString(bundleKey);
  62.     }

  63.     /**
  64.      * Looks up the value for the key in the format "BundleName:BundleKey".
  65.      *
  66.      * For example: "com.domain.messages:MyKey".
  67.      *
  68.      * @param key the key to be looked up, may be null
  69.      * @return The value associated with the key.
  70.      * @see ResourceBundle
  71.      * @see ResourceBundle#getBundle(String)
  72.      * @see ResourceBundle#getString(String)
  73.      */
  74.     @Override
  75.     public String lookup(final String key) {
  76.         if (key == null) {
  77.             return null;
  78.         }
  79.         final String[] keys = key.split(SPLIT_STR);
  80.         final int keyLen = keys.length;
  81.         final boolean anyBundle = bundleName == null;
  82.         if (anyBundle && keyLen != 2) {
  83.             throw IllegalArgumentExceptions
  84.                 .format("Bad resource bundle key format [%s]; expected format is BundleName:KeyName.", key);
  85.         }
  86.         if (bundleName != null && keyLen != 1) {
  87.             throw IllegalArgumentExceptions.format("Bad resource bundle key format [%s]; expected format is KeyName.",
  88.                 key);
  89.         }
  90.         final String keyBundleName = anyBundle ? keys[0] : bundleName;
  91.         final String bundleKey = anyBundle ? keys[1] : keys[0];
  92.         try {
  93.             return getString(keyBundleName, bundleKey);
  94.         } catch (final MissingResourceException e) {
  95.             // The key is missing, return null such that an interpolator can supply a default value.
  96.             return null;
  97.         } catch (final Exception e) {
  98.             // Should only be a ClassCastException
  99.             throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].",
  100.                 keyBundleName, bundleKey);
  101.         }
  102.     }

  103.     @Override
  104.     public String toString() {
  105.         return super.toString() + " [bundleName=" + bundleName + "]";
  106.     }

  107. }