View Javadoc

1   /*
2    * This file is part of hyphenType. hyphenType is free software: you can
3    * redistribute it and/or modify it under the terms of the GNU General Public
4    * License as published by the Free Software Foundation, either version 3 of the
5    * License, or (at your option) any later version. hyphenType is distributed in
6    * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
8    * the GNU General Public License for more details. You should have received a
9    * copy of the GNU General Public License along with hyphenType. If not, see
10   * <http://www.gnu.org/licenses/>.
11   */
12  package org.hyphenType.util.resourcebundles;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  import java.util.ResourceBundle;
17  import java.util.regex.Matcher;
18  
19  /**
20   * @author Aurelio Akira M. Matsui
21   */
22  public final class AliasResourceBundle {
23  
24      /**
25       * Makes is a utility class.
26       */
27      private AliasResourceBundle() {
28      }
29  
30      public static final String ALIAS_DECLARATION_PREFIX = "alias.";
31      
32      /**
33       * Takes a resource bundle and creates a new one with the aliases replaced.
34       * Will search for properties in the format "alias.<i>name</i>", where
35       * <i>name</i> is the name of the alias. Whenever an alias is found,
36       * replaces this alias as a prefix, whenever the prefix is found as the
37       * start of any property. For example, takes the following
38       * 
39       * <pre>
40       * alias.x = blablablablablablablabla
41       * alias.y = yyyy
42       * ${x} = A
43       * ${x}.b = Z
44       * ${x}.${y} = W
45       * </pre>
46       * 
47       * and converts into the following:
48       * 
49       * <pre>
50       * blablablablablablablabla = A
51       * blablablablablablablabla.b = Z
52       * blablablablablablablabla.yyyy = W
53       * </pre>
54       * 
55       * @param rb
56       *            The resource bundle to convert.
57       * @return The converted resource bundle.
58       */
59      public static ResourceBundle convert(final ResourceBundle rb) {
60  
61          Map<String, String> aliases = new HashMap<String, String>();
62          for (String key : rb.keySet()) {
63              if (key.startsWith(ALIAS_DECLARATION_PREFIX)) {
64                  aliases.put(key.substring(ALIAS_DECLARATION_PREFIX.length()), rb.getString(key));
65              }
66          }
67          
68          Map<String, String> newRBMap = new HashMap<String, String>();
69          for (String key : rb.keySet()) {
70              if (!key.startsWith(ALIAS_DECLARATION_PREFIX)) {
71                  String newKey = key;
72                  for (String alias : aliases.keySet()) {
73                      if (newKey.contains("${" + alias + "}")) {
74                          newKey = newKey.replaceAll("\\$\\{" + alias + "\\}", Matcher.quoteReplacement(aliases.get(alias)));
75                      }
76                  }
77                  newRBMap.put(newKey, rb.getString(key));
78              }
79          }
80  
81          return new ConfigurableListResourceBundle(newRBMap);
82      }
83  }