View Javadoc
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  
18  package org.apache.commons.text.lookup;
19  
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  
23  /**
24   * Looks up keys from resource bundles.
25   * <p>
26   * Looks up the value for a given key in the format "BundleName:BundleKey".
27   * </p>
28   * <p>
29   * For example: "com.domain.messages:MyKey".
30   * </p>
31   *
32   * @see ResourceBundle
33   * @since 1.3
34   */
35  final class ResourceBundleStringLookup extends AbstractStringLookup {
36  
37      /**
38       * Defines the singleton for this class.
39       */
40      static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup();
41  
42      /**
43       * The name of the resource bundle from which to look something up.
44       */
45      private final String bundleName;
46  
47      /**
48       * Constructs a blank instance.
49       */
50      private ResourceBundleStringLookup() {
51          this(null);
52      }
53  
54      /**
55       * Constructs an instance that only works for the given bundle.
56       *
57       * @param bundleName the name of the resource bundle from which we will look keys up.
58       * @since 1.5
59       */
60      ResourceBundleStringLookup(final String bundleName) {
61          this.bundleName = bundleName;
62      }
63  
64      ResourceBundle getBundle(final String keyBundleName) {
65          // The ResourceBundle class caches bundles, no need to cache here.
66          return ResourceBundle.getBundle(keyBundleName);
67      }
68  
69      String getString(final String keyBundleName, final String bundleKey) {
70          return getBundle(keyBundleName).getString(bundleKey);
71      }
72  
73      /**
74       * Looks up the value for the key in the format "BundleName:BundleKey".
75       *
76       * For example: "com.domain.messages:MyKey".
77       *
78       * @param key the key to be looked up, may be null
79       * @return The value associated with the key.
80       * @see ResourceBundle
81       * @see ResourceBundle#getBundle(String)
82       * @see ResourceBundle#getString(String)
83       */
84      @Override
85      public String lookup(final String key) {
86          if (key == null) {
87              return null;
88          }
89          final String[] keys = key.split(SPLIT_STR);
90          final int keyLen = keys.length;
91          final boolean anyBundle = bundleName == null;
92          if (anyBundle && keyLen != 2) {
93              throw IllegalArgumentExceptions
94                  .format("Bad resource bundle key format [%s]; expected format is BundleName:KeyName.", key);
95          }
96          if (bundleName != null && keyLen != 1) {
97              throw IllegalArgumentExceptions.format("Bad resource bundle key format [%s]; expected format is KeyName.",
98                  key);
99          }
100         final String keyBundleName = anyBundle ? keys[0] : bundleName;
101         final String bundleKey = anyBundle ? keys[1] : keys[0];
102         try {
103             return getString(keyBundleName, bundleKey);
104         } catch (final MissingResourceException e) {
105             // The key is missing, return null such that an interpolator can supply a default value.
106             return null;
107         } catch (final Exception e) {
108             // Should only be a ClassCastException
109             throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].",
110                 keyBundleName, bundleKey);
111         }
112     }
113 
114     @Override
115     public String toString() {
116         return super.toString() + " [bundleName=" + bundleName + "]";
117     }
118 
119 }